Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add hidden --manifest flag to env show #3597

Merged
merged 7 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/pkg/cli/env_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestDeployEnvOpts_Ask(t *testing.T) {
setUpMocks: func(m *deployEnvAskMocks) {
m.store.EXPECT().GetApplication("mockApp").Return(&config.Application{}, nil)
m.store.EXPECT().GetEnvironment("mockApp", "mockEnv").AnyTimes()
m.sel.EXPECT().WSEnvironment(gomock.Any(), gomock.Any()).Return("", errors.New("some error"))
m.sel.EXPECT().LocalEnvironment(gomock.Any(), gomock.Any()).Return("", errors.New("some error"))
},
wantedError: errors.New("select environment: some error"),
},
Expand All @@ -70,7 +70,7 @@ func TestDeployEnvOpts_Ask(t *testing.T) {
setUpMocks: func(m *deployEnvAskMocks) {
m.store.EXPECT().GetApplication("mockApp").Return(&config.Application{}, nil)
m.store.EXPECT().GetEnvironment("mockApp", "mockEnv").Return(&config.Environment{}, nil)
m.sel.EXPECT().WSEnvironment(gomock.Any(), gomock.Any()).Times(0)
m.sel.EXPECT().LocalEnvironment(gomock.Any(), gomock.Any()).Times(0)
},
wantedEnvName: "mockEnv",
},
Expand All @@ -79,7 +79,7 @@ func TestDeployEnvOpts_Ask(t *testing.T) {
setUpMocks: func(m *deployEnvAskMocks) {
m.store.EXPECT().GetApplication("mockApp").Return(&config.Application{}, nil)
m.store.EXPECT().GetEnvironment(gomock.Any(), gomock.Any()).Times(0)
m.sel.EXPECT().WSEnvironment(gomock.Any(), gomock.Any()).Return("mockEnv", nil)
m.sel.EXPECT().LocalEnvironment(gomock.Any(), gomock.Any()).Return("mockEnv", nil)
},
wantedEnvName: "mockEnv",
},
Expand Down
66 changes: 44 additions & 22 deletions internal/pkg/cli/env_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type showEnvVars struct {
name string
shouldOutputJSON bool
shouldOutputResources bool
shouldOutputManifest bool
}

type showEnvOpts struct {
Expand Down Expand Up @@ -82,55 +83,53 @@ func newShowEnvOpts(vars showEnvVars) (*showEnvOpts, error) {
return opts, nil
}

// Validate returns an error if the values provided by the user are invalid.
// Validate returns an error if any optional flags are invalid.
func (o *showEnvOpts) Validate() error {
if o.appName == "" {
return nil
}
if _, err := o.store.GetApplication(o.appName); err != nil {
return err
if o.shouldOutputManifest && o.shouldOutputResources {
return fmt.Errorf("--%s and --%s cannot be specified together", manifestFlag, resourcesFlag)
}
if o.name != "" {
if _, err := o.store.GetEnvironment(o.appName, o.name); err != nil {
return err
}
if o.shouldOutputManifest && o.shouldOutputJSON {
return fmt.Errorf("--%s and --%s cannot be specified together", manifestFlag, jsonFlag)
}
return nil
}

// Ask asks for fields that are required but not passed in.
// Ask validates required fields that users passed in, otherwise it prompts for them.
func (o *showEnvOpts) Ask() error {
if err := o.askApp(); err != nil {
if err := o.validateOrAskApp(); err != nil {
return err
}
return o.askEnvName()
return o.validateOrAskEnv()
}

// Execute shows the environments through the prompt.
func (o *showEnvOpts) Execute() error {
if err := o.initEnvDescriber(); err != nil {
return err
}
if o.shouldOutputManifest {
return o.writeManifest()
}

env, err := o.describer.Describe()
if err != nil {
return fmt.Errorf("describe environment %s: %w", o.name, err)
}
content := env.HumanString()
if o.shouldOutputJSON {
data, err := env.JSONString()
if err != nil {
return err
}
fmt.Fprint(o.w, data)
} else {
fmt.Fprint(o.w, env.HumanString())
content = data
}

fmt.Fprint(o.w, content)
return nil
}

func (o *showEnvOpts) askApp() error {
func (o *showEnvOpts) validateOrAskApp() error {
if o.appName != "" {
return nil
return o.validateApp()
}
app, err := o.sel.Application(envShowAppNamePrompt, envShowAppNameHelpPrompt)
if err != nil {
Expand All @@ -140,17 +139,38 @@ func (o *showEnvOpts) askApp() error {
return nil
}

func (o *showEnvOpts) askEnvName() error {
//return if env name is set by flag
func (o *showEnvOpts) validateApp() error {
if _, err := o.store.GetApplication(o.appName); err != nil {
return fmt.Errorf("validate application name '%s': %v", o.appName, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel the reason why we need this strange pattern for error wrapping (validate ... but the actual API we call is GetApplication) is because we probably need a validate package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you think so? maybe a cli/validate package isn't a bad idea for us to move all of our validate.go logic

}
return nil
}

func (o *showEnvOpts) validateOrAskEnv() error {
if o.name != "" {
return nil
return o.validateEnv()
}
env, err := o.sel.Environment(fmt.Sprintf(envShowNamePrompt, color.HighlightUserInput(o.appName)), envShowHelpPrompt, o.appName)
if err != nil {
return fmt.Errorf("select environment for application %s: %w", o.appName, err)
}
o.name = env
return nil
}

func (o *showEnvOpts) validateEnv() error {
if _, err := o.store.GetEnvironment(o.appName, o.name); err != nil {
return fmt.Errorf("validate environment name '%s' in application '%s': %v", o.name, o.appName, err)
}
return nil
}

func (o *showEnvOpts) writeManifest() error {
out, err := o.describer.Manifest()
if err != nil {
return fmt.Errorf("fetch manifest for environment %s: %v", o.name, err)
}
fmt.Fprintln(o.w, string(out))
return nil
}

Expand All @@ -177,5 +197,7 @@ func buildEnvShowCmd() *cobra.Command {
cmd.Flags().StringVarP(&vars.name, nameFlag, nameFlagShort, "", envFlagDescription)
cmd.Flags().BoolVar(&vars.shouldOutputJSON, jsonFlag, false, jsonFlagDescription)
cmd.Flags().BoolVar(&vars.shouldOutputResources, resourcesFlag, false, envResourcesFlagDescription)
cmd.Flags().BoolVar(&vars.shouldOutputManifest, manifestFlag, false, manifestFlagDescription)
_ = cmd.Flags().MarkHidden(manifestFlag)
return cmd
}
Loading