Skip to content

Commit

Permalink
Merge pull request #263 from go-aah/auto-envprofile-flag-in-cli
Browse files Browse the repository at this point in the history
Auto envprofile flag in cli command and first level subcommands
  • Loading branch information
jeevatkm committed Oct 10, 2019
2 parents 71be3bb + 911d844 commit 883bde9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ branches:
- /^v[0-9.]+$/

go:
- 1.11.x
- 1.12.x
- 1.13.x
- tip

go_import_path: aahframe.work
Expand Down
34 changes: 32 additions & 2 deletions aah.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" {
Expand All @@ -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:
Expand Down
35 changes: 34 additions & 1 deletion aah_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 883bde9

Please sign in to comment.