-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add support to set the GC Stack the CLI should use by default #4940
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f2b14fd
Add stack ID and slug support to cloud login command
dgzlopes 12ff28d
Implement default stack in interactive mode
dgzlopes 2813fd8
Don't run the migration twice
dgzlopes 671c401
Update examples, other flags and hide the ID from users
dgzlopes 12fc907
Add support for retrieving default projectID based on stackID
dgzlopes ff92bc4
Add warning when default projectID is used
dgzlopes db0134a
Handle lack of stack info
dgzlopes 6f23366
Add support for setting slug in the script
dgzlopes 2f92dc5
Move shared logic to common
dgzlopes d048793
Improve response handling in GetDefaultProject (errcheck)
dgzlopes 408f03b
Keep making linter happy
dgzlopes f0cd139
Don't use underscores
dgzlopes 79806d7
Don't use logrus directly
dgzlopes dc427dd
Comment GetDefaultProject function
dgzlopes c619442
Remove nesting
dgzlopes 44b495a
Remove breaking change
dgzlopes 217e011
Make login code cleaner
dgzlopes 35b4015
Make it work with stack tokens
dgzlopes 3f6df55
Keep token input as a StringField
dgzlopes 154b3ed
Change warning text
dgzlopes e19b61f
Only display login message if token is valid
dgzlopes 547f356
Improve error handling in getDefaultStack
dgzlopes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "syscall" | ||
|
|
||
| "github.com/fatih/color" | ||
|
|
@@ -59,7 +60,9 @@ the "k6 run -o cloud" command. | |
|
|
||
| loginCloudCommand.Flags().StringP("token", "t", "", "specify `token` to use") | ||
| loginCloudCommand.Flags().BoolP("show", "s", false, "display saved token and exit") | ||
| loginCloudCommand.Flags().BoolP("reset", "r", false, "reset stored token") | ||
| loginCloudCommand.Flags().BoolP("reset", "r", false, "reset stored token and stack info") | ||
| loginCloudCommand.Flags().String("stack-id", "", "set stack ID (cannot be used with --stack-slug)") | ||
| loginCloudCommand.Flags().String("stack-slug", "", "set stack slug (cannot be used with --stack-id)") | ||
|
|
||
| return loginCloudCommand | ||
| } | ||
|
|
@@ -92,23 +95,49 @@ func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error { | |
| show := getNullBool(cmd.Flags(), "show") | ||
| reset := getNullBool(cmd.Flags(), "reset") | ||
| token := getNullString(cmd.Flags(), "token") | ||
| stackIDStr := cmd.Flags().Lookup("stack-id").Value.String() | ||
| stackSlug := getNullString(cmd.Flags(), "stack-slug") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the pull request's description outdated, or they are intentionally different? |
||
|
|
||
| var stackIDInt int64 | ||
| var stackIDValid bool | ||
| if stackIDStr != "" { | ||
| var parseErr error | ||
| stackIDInt, parseErr = parseStackID(stackIDStr) | ||
ankur22 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if parseErr != nil { | ||
| return fmt.Errorf("invalid --stack-id: %w", parseErr) | ||
| } | ||
| stackIDValid = true | ||
| } | ||
| if stackIDValid && stackSlug.Valid { | ||
| return errors.New("only one of --stack-id or --stack-slug can be specified") | ||
| } | ||
|
|
||
| switch { | ||
| case reset.Valid: | ||
| newCloudConf.Token = null.StringFromPtr(nil) | ||
| printToStdout(c.globalState, " token reset\n") | ||
| newCloudConf.StackID = null.StringFromPtr(nil) | ||
| newCloudConf.StackSlug = null.StringFromPtr(nil) | ||
| printToStdout(c.globalState, " token and stack info reset\n") | ||
| return nil | ||
| case show.Bool: | ||
| valueColor := getColor(c.globalState.Flags.NoColor || !c.globalState.Stdout.IsTTY, color.FgCyan) | ||
| printToStdout(c.globalState, fmt.Sprintf(" token: %s\n", valueColor.Sprint(newCloudConf.Token.String))) | ||
| if newCloudConf.StackID.Valid { | ||
| printToStdout(c.globalState, fmt.Sprintf(" stack-id: %s\n", valueColor.Sprint(newCloudConf.StackID.String))) | ||
| } | ||
| if newCloudConf.StackSlug.Valid { | ||
| printToStdout(c.globalState, fmt.Sprintf(" stack-slug: %s\n", valueColor.Sprint(newCloudConf.StackSlug.String))) | ||
| } | ||
| return nil | ||
| case token.Valid: | ||
| newCloudConf.Token = token | ||
| default: | ||
| form := ui.Form{ | ||
| tokenForm := ui.Form{ | ||
| Banner: "Enter your token to authenticate with Grafana Cloud k6.\n" + | ||
| "Please, consult the Grafana Cloud k6 documentation for instructions on how to generate one:\n" + | ||
| "https://grafana.com/docs/grafana-cloud/testing/k6/author-run/tokens-and-cli-authentication", | ||
| Fields: []ui.Field{ | ||
| ui.PasswordField{ | ||
| ui.StringField{ | ||
dgzlopes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Key: "Token", | ||
| Label: "Token", | ||
| }, | ||
|
|
@@ -117,12 +146,60 @@ func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error { | |
| if !term.IsTerminal(int(syscall.Stdin)) { //nolint:unconvert | ||
| c.globalState.Logger.Warn("Stdin is not a terminal, falling back to plain text input") | ||
| } | ||
| var vals map[string]string | ||
| vals, err = form.Run(c.globalState.Stdin, c.globalState.Stdout) | ||
| tokenVals, err := tokenForm.Run(c.globalState.Stdin, c.globalState.Stdout) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| newCloudConf.Token = null.StringFrom(vals["Token"]) | ||
| tokenValue := tokenVals["Token"] | ||
ankur22 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| newCloudConf.Token = null.StringFrom(tokenValue) | ||
|
|
||
| if newCloudConf.Token.Valid { | ||
| if err := validateToken(c.globalState, currentJSONConfigRaw, newCloudConf.Token.String); err != nil { | ||
| return fmt.Errorf("token validation failed: %w", err) | ||
| } | ||
| } | ||
|
|
||
| defaultStack := func(token string) string { | ||
| return "slug" | ||
| }(tokenValue) | ||
|
|
||
| slugForm := ui.Form{ | ||
| Banner: "\nConfigure the stack where your tests will run by default.\n" + | ||
| "Please, consult the Grafana Cloud k6 documentation for instructions on how to find your stack slug:\n" + | ||
| // TODO: Update the link when the documentation is ready | ||
| "FIXME", | ||
| Fields: []ui.Field{ | ||
| ui.StringField{ | ||
| Key: "StackSlug", | ||
| Label: "Stack", | ||
| Default: defaultStack, | ||
| }, | ||
| }, | ||
| } | ||
| slugVals, err := slugForm.Run(c.globalState.Stdin, c.globalState.Stdout) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| stackValue := slugVals["StackSlug"] | ||
| stackSlugInput := null.StringFrom(stackValue) | ||
|
|
||
| if stackSlugInput.Valid { | ||
| stackSlug = stackSlugInput | ||
| } else { | ||
| return errors.New("stack cannot be empty") | ||
| } | ||
| } | ||
|
|
||
| if stackIDValid { | ||
| newCloudConf.StackID = null.StringFrom(fmt.Sprintf("%d", stackIDInt)) | ||
| newCloudConf.StackSlug = null.StringFromPtr(nil) | ||
| } else if stackSlug.Valid { | ||
| newCloudConf.StackSlug = stackSlug | ||
| newCloudConf.StackID = null.StringFromPtr(nil) | ||
| } else { | ||
| newCloudConf.StackID = null.StringFromPtr(nil) | ||
| newCloudConf.StackSlug = null.StringFromPtr(nil) | ||
| } | ||
|
|
||
| if newCloudConf.Token.Valid { | ||
|
|
@@ -144,9 +221,20 @@ func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error { | |
| } | ||
|
|
||
| if newCloudConf.Token.Valid { | ||
| valueColor := getColor(c.globalState.Flags.NoColor || !c.globalState.Stdout.IsTTY, color.FgCyan) | ||
| printToStdout(c.globalState, fmt.Sprintf( | ||
| "Logged in successfully, token saved in %s\n", c.globalState.Flags.ConfigFilePath, | ||
| "\nLogged in successfully, token and stack info saved in %s\n", c.globalState.Flags.ConfigFilePath, | ||
| )) | ||
| if !c.globalState.Flags.Quiet { | ||
| printToStdout(c.globalState, fmt.Sprintf(" token: %s\n", valueColor.Sprint(newCloudConf.Token.String))) | ||
|
|
||
| if newCloudConf.StackID.Valid { | ||
| printToStdout(c.globalState, fmt.Sprintf(" stack-id: %s\n", valueColor.Sprint(newCloudConf.StackID.String))) | ||
| } | ||
| if newCloudConf.StackSlug.Valid { | ||
| printToStdout(c.globalState, fmt.Sprintf(" stack-slug: %s\n", valueColor.Sprint(newCloudConf.StackSlug.String))) | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
@@ -186,3 +274,7 @@ func validateToken(gs *state.GlobalState, jsonRawConf json.RawMessage, token str | |
|
|
||
| return nil | ||
| } | ||
|
|
||
| func parseStackID(val string) (int64, error) { | ||
| return strconv.ParseInt(val, 10, 64) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
Shamefully, i don't know the difference between
StackIDandStackSlug. Looks like we can't work with both here. If someone did provide both, could k6 return an error (or log a warning)? The user might want to work withStackIDand instead it gets overwritten byStackSlugas they forgot to undefineStackSlug.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.
StackID is a number. StackSlug is a string. You can spot it easily on the URL of your Grafana Cloud instance.
e.g. .grafana.com
StackIDs can't change. StackSlugs only change on edge cases.
IDs are pretty hard to find, while Slugs are well-known. I wanted to focus the UX on Slugs because they seem way easier to understand, find, and reference. But yeah, this is quite confusing, I know 😄