-
Notifications
You must be signed in to change notification settings - Fork 427
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
feat: add profile flag to root init command #642
Conversation
This commit adds a profile flag to `ecs-preview init` which is passed into the implicit call to `ecs-preview env init`. If the user does not specify a profile, ask them to select a profile.
More unit tests forthcoming once I dig more into how mocks work |
internal/pkg/cli/init.go
Outdated
@@ -221,6 +227,9 @@ func (o *initOpts) deployEnv() error { | |||
// User chose not to deploy the application, exit. | |||
return nil | |||
} | |||
if err := o.initEnv.Ask(); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we need this. initEnv.Ask()
is used to get EnvName
and EnvProfile
in an interactive way. However, init
by default (if not pass in value using flags) is supposed to provide an easy way for users to spin up their apps easily. That's the reason why we default EnvName
to be "test" and EnvProfile
to be "default" since in most cases for new projects they are set to be these values.
Also we always have value for EnvName
, the Ask()
command is just used to get the profile name (asking for env name will be skipped).
internal/pkg/cli/init.go
Outdated
@@ -274,6 +283,7 @@ func BuildInitCmd() *cobra.Command { | |||
return nil | |||
}), | |||
} | |||
cmd.Flags().StringVar(&vars.profile, profileFlag, "", profileFlagDescription) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be nice to give default value for the flag as "default" so that we can remove the initEnv.Ask()
and simplify the workflow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, we can set that default in init
but leave it empty in env init
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if we set the default value for the flag to be "default" then in the initEnv.Ask()
it won't ask user to select since o.EnvProfile
will not be an empty string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can make this change, but I'd like to make sure we document it somewhere. Is there a good spot that we can stick a line in the env init calls that will remind the user "hey, this will deploy to your default profile; if that's not cool, choose no and then run env deploy later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could simply modify the initShouldDeployPrompt to the following:
"Would you like to deploy a test environment? (This will deploy to your default profile if not otherwise specified with the --profile flag)"
but that's not dynamic, and it seems like we could do better by only printing off a warning line when the user hasn't specified a profile.
internal/pkg/cli/init.go
Outdated
@@ -77,10 +78,15 @@ func newInitOpts(vars initVars) (*initOpts, error) { | |||
if err != nil { | |||
return nil, err | |||
} | |||
profileSess, err := sessProvider.FromProfile(vars.profile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the reason why this doesn't error when vars.profile=""
is this:
// If not set and environment variables are not set the "default"
// (DefaultSharedConfigProfile) will be used as the profile to load the
// session config from.
So right now it's creating a session with the default profile which is slightly misleading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intuition says that we should just not set this field until o.initEnv.Ask()
is executed.
@@ -221,6 +227,9 @@ func (o *initOpts) deployEnv() error { | |||
// User chose not to deploy the application, exit. | |||
return nil | |||
} | |||
if err := o.initEnv.Ask(); err != nil { | |||
return err | |||
} | |||
return o.initEnv.Execute() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While trying to understand how come this worked, I realized that we have a bug in "env init"
:|
Right now the value from askEnvProfile
in env_init.go
gets ignored inside Execute
. So even if you choose a different profile than "default", we pick up only "default".
https://github.com/aws/amazon-ecs-cli-v2/blob/b223142e566e3bd123b541ff8cc500d4b3a113fb/internal/pkg/cli/env_init.go#L120
We should change the code so that we initialize envDeployer
and envIdentity
only after the EnvProfile
value is set.
Here is a sample way of how to do it in env_delete.go
:
https://github.com/aws/amazon-ecs-cli-v2/blob/b223142e566e3bd123b541ff8cc500d4b3a113fb/internal/pkg/cli/env_delete.go#L60
Behavior has been updated to not ask for a profile when running I've also refactored the existing env deploy code to only initialize profile clients AFTER the user is prompted for a profile to deploy to, right at the beginning of Execute. This seems to work; I was able to get Is there a way to avoid repeating myself in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall LGTM except that we need to find a good way to avoid the repetition.
internal/pkg/cli/init.go
Outdated
profileConfig: cfg, | ||
prog: spin, | ||
identity: id, | ||
|
||
initProfileClients: func(o *initEnvOpts) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite sure but maybe we can define this function outside of the struct to make it a variable so that we can reuse that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 yeah like @iamhopaul123 says, we can do:
var initEnvProfileClients = func(o *initEnvOpts) error {
// your function
}
and then while initializing the structs:
initProfileClients: initEnvProfileClients,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in the latest commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good thank you!
internal/pkg/cli/env_init.go
Outdated
initProfileClients: func(o *initEnvOpts) error { | ||
profileSess, err := session.NewProvider().FromProfile(o.EnvProfile) | ||
if err != nil { | ||
return fmt.Errorf("Cannot create session from profile %s: %w", o.EnvProfile, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return fmt.Errorf("Cannot create session from profile %s: %w", o.EnvProfile, err) | |
return fmt.Errorf("create session from profile %s: %w", o.EnvProfile, err) |
The recommended guideline for error strings from Go:
Error strings should not be capitalized (unless beginning with proper nouns or acronyms) or end with punctuation, since they are usually printed following other context.
https://github.com/golang/go/wiki/CodeReviewComments#error-strings
Another one that we follow is avoiding words like "failed" or "cannot" in error strings as usually they're redundant in the context of errors :)
https://github.com/uber-go/guide/blob/master/style.md#error-wrapping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, thanks for the links!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolves #317
Adds a profile flag to root init command to be used by the implicit call to env init.
If no profile is specified and the user opts to deploy a test env, prompt them to select from the list of available profiles. If a profile is specified, deploy the test env into that profile.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.