Skip to content

Commit

Permalink
cmd/env: Refactor mode checking
Browse files Browse the repository at this point in the history
This commit moves the checking of the mode arguments of the env command
into an AfterParse function. It also adds tests for the error condition.
  • Loading branch information
tkw1536 committed Dec 3, 2024
1 parent 2e58f8c commit 358a463
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ var errModesIncompatible = exit.Error{
ExitCode: exit.ExitCommandArguments,
}

func (e _env) Run(context ggman.Context) error {
// check that at most one mode was provided
func (e _env) AfterParse() error {
// check that at most one mode was given
count := 0
if e.Describe {
count++
Expand All @@ -80,7 +80,10 @@ func (e _env) Run(context ggman.Context) error {
if count > 1 {
return errModesIncompatible
}
return nil
}

func (e _env) Run(context ggman.Context) error {
variables, err := e.variables()
if err != nil {
return err
Expand Down
27 changes: 27 additions & 0 deletions cmd/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ func TestCommandEnv(t *testing.T) {
"${GGROOT}\n",
"",
},
{
"more than one mode",
"",
[]string{"env", "--list", "--raw"},

4,
"",
"At most one of `--raw`, `--list` and `--describe` may be given\n",
},
{
"more than one mode (2)",
"",
[]string{"env", "--list", "--describe"},

4,
"",
"At most one of `--raw`, `--list` and `--describe` may be given\n",
},
{
"more than one mode (3)",
"",
[]string{"env", "--raw", "--describe"},

4,
"",
"At most one of `--raw`, `--list` and `--describe` may be given\n",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 358a463

Please sign in to comment.