From 358a463502f8921da1ef1c2667d3ab68de2899a6 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Tue, 3 Dec 2024 22:21:40 +0100 Subject: [PATCH] cmd/env: Refactor mode checking 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. --- cmd/env.go | 7 +++++-- cmd/env_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cmd/env.go b/cmd/env.go index 176865d..b803045 100644 --- a/cmd/env.go +++ b/cmd/env.go @@ -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++ @@ -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 diff --git a/cmd/env_test.go b/cmd/env_test.go index 305b453..99cd86f 100644 --- a/cmd/env_test.go +++ b/cmd/env_test.go @@ -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 {