diff --git a/packages/cli/internal/pkg/cli/context_describe.go b/packages/cli/internal/pkg/cli/context_describe.go index d5164d43..8818d994 100644 --- a/packages/cli/internal/pkg/cli/context_describe.go +++ b/packages/cli/internal/pkg/cli/context_describe.go @@ -4,6 +4,8 @@ package cli import ( + "fmt" + "github.com/aws/amazon-genomics-cli/internal/pkg/cli/clierror" "github.com/aws/amazon-genomics-cli/internal/pkg/cli/context" "github.com/aws/amazon-genomics-cli/internal/pkg/cli/format" @@ -28,7 +30,17 @@ func newDescribeContextOpts(vars describeContextVars) (*describeContextOpts, err }, nil } -func (o *describeContextOpts) Validate() error { +func (o *describeContextOpts) Validate(args []string) error { + if len(args) == 0 && o.ContextName == "" { + return fmt.Errorf("a context must be provided") + } else if len(args) == 1 && o.ContextName != "" { + return fmt.Errorf("either the '-c' flag or a context must be provided, but not both") + } + + if len(args) == 1 { + o.ContextName = args[0] + } + return nil } @@ -69,19 +81,16 @@ func BuildContextDescribeCommand() *cobra.Command { ` + DescribeOutput(types.Context{}), Example: ` /code agc context describe myCtx`, - Args: cobra.ExactArgs(1), + Args: cobra.ArbitraryArgs, RunE: runCmdE(func(cmd *cobra.Command, args []string) error { - if len(args) > 0 { - vars.ContextName = args[0] - } opts, err := newDescribeContextOpts(vars) if err != nil { return err } - log.Info().Msgf("Describing context '%s'", opts.ContextName) - if err := opts.Validate(); err != nil { + if err := opts.Validate(args); err != nil { return err } + log.Info().Msgf("Describing context '%s'", opts.ContextName) ctx, err := opts.Execute() if err != nil { return clierror.New("context describe", vars, err) @@ -90,5 +99,6 @@ func BuildContextDescribeCommand() *cobra.Command { return nil }), } + cmd.Flags().StringVarP(&vars.ContextName, contextFlag, contextFlagShort, "", deployContextDescription) return cmd } diff --git a/packages/cli/internal/pkg/cli/context_describe_test.go b/packages/cli/internal/pkg/cli/context_describe_test.go index 4627b71b..a2cfd13f 100644 --- a/packages/cli/internal/pkg/cli/context_describe_test.go +++ b/packages/cli/internal/pkg/cli/context_describe_test.go @@ -71,3 +71,52 @@ func TestDescribeContextOpts_Execute(t *testing.T) { }) } } + +func TestDescribeContextOpts_Validate(t *testing.T) { + testCases := map[string]struct { + contextName string + expectedContext string + contextNameArgs []string + expectedErr error + }{ + "valid single context name": { + contextName: testContextName1, + expectedContext: testContextName1, + expectedErr: nil, + }, + "valid single context arg": { + contextNameArgs: []string{testContextName2}, + expectedContext: testContextName2, + expectedErr: nil, + }, + "both context and context arg throws error": { + contextName: testContextName1, + contextNameArgs: []string{testContextName2}, + expectedErr: fmt.Errorf("either the '-c' flag or a context must be provided, but not both"), + }, + "no contexts supplied": { + expectedErr: fmt.Errorf("a context must be provided"), + }, + } + + for name, tt := range testCases { + t.Run(name, func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + mockCtxManager := contextmocks.NewMockContextManager(ctrl) + opts := &describeContextOpts{ + ctxManager: mockCtxManager, + describeContextVars: describeContextVars{tt.contextName}, + } + + err := opts.Validate(tt.contextNameArgs) + + if tt.expectedErr != nil { + assert.EqualError(t, err, tt.expectedErr.Error()) + } else { + require.NoError(t, err) + assert.Equal(t, tt.expectedContext, opts.ContextName) + } + }) + } +} diff --git a/site/content/en/docs/Tutorials/walkthrough.md b/site/content/en/docs/Tutorials/walkthrough.md index 8a634899..1dd1acc2 100644 --- a/site/content/en/docs/Tutorials/walkthrough.md +++ b/site/content/en/docs/Tutorials/walkthrough.md @@ -89,7 +89,7 @@ agc context deploy --all Contexts have read-write access to a context specific prefix in the S3 bucket Amazon Genomics CLI creates during account activation. You can check this for the `myContext` context with: ```shell -agc context describe -c myContext +agc context describe myContext ``` You should see something like: