Skip to content

Commit

Permalink
Package Cobra:
Browse files Browse the repository at this point in the history
- fix configure config file : bad function TrimRight instead of TrimSuffix
- allow specify an alias for configure command
  • Loading branch information
nabbar committed Nov 29, 2023
1 parent 9851a8c commit 4a47f39
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions cobra/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (c *cobra) ConfigureWriteConfig(basename string, defaultConfig func() io.Re
}
default:
buf = defaultConfig()
cfgFile = strings.TrimRight(cfgFile, ext) + ".json"
cfgFile = strings.TrimSuffix(cfgFile, ext) + ".json"
}

// #nosec
Expand Down Expand Up @@ -189,14 +189,14 @@ func (c *cobra) jsonToYaml(r io.Reader) (io.Reader, error) {
}
}

func (c *cobra) AddCommandConfigure(basename string, defaultConfig func() io.Reader) {
func (c *cobra) AddCommandConfigure(alias, basename string, defaultConfig func() io.Reader) {
pkg := c.getPackageName()

if basename == "" && pkg != "" {
basename = "." + strings.ToLower(pkg)
}

c.c.AddCommand(&spfcbr.Command{
cmd := &spfcbr.Command{
Use: "configure <file path with valid extension (json, yaml, toml, ...) to be generated>",
Example: "configure ~/." + strings.ToLower(pkg) + ".yml",
Short: "Generate config file",
Expand All @@ -210,5 +210,11 @@ override by passed flag in command line and completed with default for non exist
Args: func(cmd *spfcbr.Command, args []string) error {
return c.ConfigureCheckArgs(basename, args)
},
})
}

if len(alias) > 0 {
cmd.Aliases = []string{alias}
}

c.c.AddCommand(cmd)
}
2 changes: 1 addition & 1 deletion cobra/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type Cobra interface {
AddCommand(subCmd ...*spfcbr.Command)

AddCommandCompletion()
AddCommandConfigure(basename string, defaultConfig func() io.Reader)
AddCommandConfigure(alias, basename string, defaultConfig func() io.Reader)
AddCommandPrintErrorCode(fct FuncPrintErrorCode)

Execute() error
Expand Down

0 comments on commit 4a47f39

Please sign in to comment.