diff --git a/.travis.yml b/.travis.yml index e3d86ac..ceb6925 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,8 @@ branches: - /^v[0-9.]+$/ go: - - 1.11.x - 1.12.x + - 1.13.x - tip go_import_path: aahframe.work diff --git a/aah.go b/aah.go index 42ee6c4..c13d4ca 100644 --- a/aah.go +++ b/aah.go @@ -30,7 +30,7 @@ import ( "aahframe.work/cache" "aahframe.work/config" "aahframe.work/console" - "aahframe.work/essentials" + ess "aahframe.work/essentials" "aahframe.work/i18n" "aahframe.work/internal/settings" "aahframe.work/log" @@ -443,6 +443,7 @@ func (a *Application) AddValueParser(typ reflect.Type, parser valpar.Parser) err // AddCommand method adds the aah application CLI commands. Introduced in v0.12.0 release // aah application binary fully compliant using module console and POSIX flags. func (a *Application) AddCommand(cmds ...console.Command) error { + pCmds := cmds[:0] for _, cmd := range cmds { name := strings.ToLower(cmd.Name) if name == "run" || name == "vfs" || name == "help" { @@ -453,11 +454,40 @@ func (a *Application) AddCommand(cmds ...console.Command) error { return fmt.Errorf("aah: command name '%s' already exists", name) } } - a.cli.Commands = append(a.cli.Commands, cmd) + + // GH#244 add envprofile to command and first level subcommands by default + cmd.Flags = addEnvProfileFlag(cmd.Flags) + subCommands := cmd.Subcommands[:0] + for _, sc := range cmd.Subcommands { + sc.Flags = addEnvProfileFlag(sc.Flags) + subCommands = append(subCommands, sc) + } + pCmds = append(pCmds, cmd) } + a.cli.Commands = append(a.cli.Commands, pCmds...) return nil } +func addEnvProfileFlag(cmdFlags []console.Flag) []console.Flag { + cf := cmdFlags[:0] + flagFound := false + for _, f := range cmdFlags { + if f.GetName() == "envprofile" { + flagFound = true + break + } + } + + if !flagFound { + cf = append(cf, console.StringFlag{ + Name: "envprofile, e", + Value: "dev", + Usage: "Environment profile name to activate (e.g: dev, qa, prod)", + }) + } + return cf +} + // Validate method is to validate struct via underneath validator. // // Returns: diff --git a/aah_test.go b/aah_test.go index b1504cb..825c90c 100644 --- a/aah_test.go +++ b/aah_test.go @@ -26,7 +26,7 @@ import ( "aahframe.work/ainsp" "aahframe.work/config" "aahframe.work/console" - "aahframe.work/essentials" + ess "aahframe.work/essentials" "aahframe.work/log" "github.com/stretchr/testify/assert" ) @@ -192,6 +192,39 @@ func TestAppMisc(t *testing.T) { er = a.AddCommand(console.Command{Name: "test2"}) assert.Equal(t, errors.New("aah: command name 'test2' already exists"), er) + // GH#244 + testCmds := []console.Command{ + console.Command{Name: "newcmd1"}, + console.Command{ + Name: "newcmd2", + Flags: []console.Flag{ + console.StringFlag{ + Name: "envprofile, e", + Value: "dev", + Usage: "Environment profile name to activate (e.g: dev, qa, prod)", + }, + }, + }, + console.Command{ + Name: "newcmd3sub", + Subcommands: []console.Command{ + console.Command{Name: "newcmd3sub1"}, + console.Command{ + Name: "newcmd3sub2", + Flags: []console.Flag{ + console.StringFlag{ + Name: "envprofile, e", + Value: "dev", + Usage: "Environment profile name to activate (e.g: dev, qa, prod)", + }, + }, + }, + }, + }, + } + er = a.AddCommand(testCmds...) + assert.Nil(t, er) + ll := a.NewChildLogger(log.Fields{"key1": "value1"}) assert.NotNil(t, ll)