Skip to content

Commit

Permalink
optimize: adjust command priority for the config mode
Browse files Browse the repository at this point in the history
  • Loading branch information
justlorain committed Jan 26, 2025
1 parent 0e7eac2 commit e55f7fe
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
1 change: 1 addition & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var addCmd = &cobra.Command{
Use: "add",
Short: "add license headers to files",
Long: `Common Command | Add license headers to files
EXAMPLE: nwa add -l apache -c Lorain -m "**/*.go"`,
GroupID: _common,
Args: cobra.MinimumNArgs(1),
Expand Down
2 changes: 2 additions & 0 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ var checkCmd = &cobra.Command{
Use: "check",
Short: "check license headers of files",
Long: `Common Command | Check license headers of files
EXAMPLE: nwa check -t tmpl.txt "src/**"
NOTE: Do not use --mute (-m) flag with the command`,
GroupID: _common,
Args: cobra.MinimumNArgs(1),
Expand Down
51 changes: 36 additions & 15 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,26 @@ var configCmd = &cobra.Command{
Use: "config",
Short: "edit the files according to the configuration file",
Long: `Config Command | Edit files according to the configuration file
EXAMPLE: nwa config config.yaml -c check
NOTE: This command only supports the command flag;
You can only specify the path of the configuration file, and everything depends on the configuration file;
If some configuration are not configured, the default configuration will be used;
The command can be set on the command line
SAMPLE CONFIGURATION FILE(YAML):
EXAMPLE: nwa config -c check config.yaml
NOTE: This command only supports the --command (-c) flag;
You can only specify the path of the configuration file, and everything depends on the configuration file.
If some configuration are not configured, the default configuration will be used.
The command can also be set on the command line.
command priority:
1. --command (-c) flag
2. value configured for cmd in the configuration file
3. default value of cmd in the configuration file (add)
SAMPLE CONFIGURATION FILE (YAML):
nwa:
cmd: "add"
holder: "RHINE LAB.LLC."
year: "2077"
license: "apache"
spdxids: ""
mute: false
path: ["server/**", "client/**", "pkg/**"]
skip: ["**.py"]
tmpl: "nwa.txt"
path: ["server/**/*.go", "client/**/*.go", "pkg/**"]
skip: ["**/*.py"]
`,
GroupID: _config,
Args: cobra.ExactArgs(1),
Expand All @@ -56,6 +60,15 @@ nwa:
cobra.CheckErr(err)
}

// command priority:
// 1. --command (-c) flag
// 2. value configured for `cmd` in the configuration file
// 3. default value of `cmd` in the configuration file (add)
operation := internal.Operation(defaultConfig.Nwa.Cmd)
if defaultConfigFlags.Command != "" {
operation = internal.Operation(defaultConfigFlags.Command)
}

slog.SetLogLoggerLevel(slog.LevelWarn)
if defaultConfig.Nwa.Verbose {
slog.SetLogLoggerLevel(slog.LevelInfo)
Expand Down Expand Up @@ -102,7 +115,7 @@ nwa:
cobra.CheckErr(err)
}
// determine files need to be added
internal.PrepareTasks(defaultConfig.Nwa.Path, renderedTmpl, internal.Operation(defaultConfig.Nwa.Cmd), defaultConfig.Nwa.Skip, rawTmpl, defaultConfig.Nwa.Fuzzy)
internal.PrepareTasks(defaultConfig.Nwa.Path, renderedTmpl, operation, defaultConfig.Nwa.Skip, rawTmpl, defaultConfig.Nwa.Fuzzy)
} else {
content, err := os.ReadFile(defaultConfig.Nwa.Tmpl)
if err != nil {
Expand All @@ -112,16 +125,24 @@ nwa:
if rawTmpl {
_, _ = fmt.Fprintln(buf)
}
internal.PrepareTasks(defaultConfig.Nwa.Path, buf.Bytes(), internal.Operation(defaultConfig.Nwa.Cmd), defaultConfig.Nwa.Skip, rawTmpl, defaultConfig.Nwa.Fuzzy)
internal.PrepareTasks(defaultConfig.Nwa.Path, buf.Bytes(), operation, defaultConfig.Nwa.Skip, rawTmpl, defaultConfig.Nwa.Fuzzy)
}
internal.ExecuteTasks(internal.Operation(defaultConfig.Nwa.Cmd), defaultConfig.Nwa.Mute)
internal.ExecuteTasks(operation, defaultConfig.Nwa.Mute)
},
}

func init() {
setupConfigCmd(configCmd)
}

type ConfigFlags struct {
Command string
}

var defaultConfigFlags = ConfigFlags{
Command: "", // empty if user not specified
}

type Config struct {
Nwa NwaConfig `yaml:"nwa"`
}
Expand Down
1 change: 1 addition & 0 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var removeCmd = &cobra.Command{
Use: "remove",
Short: "remove license headers of files",
Long: `Common Command | Remove licenses headers of files
EXAMPLE: nwa remove -i "Apache and MIT" "src/**/*.go"`,
GroupID: _common,
Args: cobra.MinimumNArgs(1),
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ func setupCommonCmd(common *cobra.Command) {
}

func setupConfigCmd(config *cobra.Command) {
config.Flags().StringVarP(&defaultConfig.Nwa.Cmd, "command", "c", defaultConfig.Nwa.Cmd, "command")
rootCmd.AddCommand(config)

config.Flags().StringVarP(&defaultConfigFlags.Command, "command", "c", defaultConfigFlags.Command, "command to execute")
}

func executeCommonCmd(_ *cobra.Command, args []string, flags CommonFlags, operation internal.Operation) {
Expand Down
2 changes: 2 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ var updateCmd = &cobra.Command{
Use: "update",
Short: "update license headers of files",
Long: `Common Command | Update license headers of files
EXAMPLE: nwa update -l mit -c Anmory "**/*.py"
NOTE: Update identifies the content before the first blank line as a license header;
If your file does not meet the requirements, please use remove + add`,
GroupID: _common,
Expand Down

0 comments on commit e55f7fe

Please sign in to comment.