Package Cobra:

- fix configure config file : bad function TrimRight instead of TrimSuffix
- allow specify an alias for configure command
This commit is contained in:
Nicolas JUHEL
2023-11-29 16:01:13 +01:00
parent 9851a8caac
commit 4a47f39dc0
2 changed files with 11 additions and 5 deletions

View File

@@ -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
@@ -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",
@@ -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)
}

View File

@@ -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