Skip to content

Commit 0b604df

Browse files
committed
Refactor to reduce the Cognitive complexity
1 parent 473f762 commit 0b604df

File tree

1 file changed

+62
-47
lines changed

1 file changed

+62
-47
lines changed

internal/cmd/cloud_login.go

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -108,58 +108,20 @@ func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error {
108108
if !tokenInput.Valid || tokenInput.String == "" {
109109
return errors.New("Token value is required but it was not passed or is empty")
110110
}
111-
err := validateInputs(c.globalState, &newCloudConf, currentJSONConfigRaw, tokenInput, stackInput)
111+
err := authenticateUserToken(c.globalState, &newCloudConf, currentJSONConfigRaw, tokenInput.String, stackInput.String)
112112
if err != nil {
113113
return err
114114
}
115115
default:
116116
gs := c.globalState
117117

118-
/* Token form */
119-
tokenForm := ui.Form{
120-
Banner: "Enter your token to authenticate with Grafana Cloud.\n" +
121-
"Please, consult the documentation for instructions on how to generate one:\n" +
122-
"https://grafana.com/docs/grafana-cloud/testing/k6/author-run/tokens-and-cli-authentication",
123-
Fields: []ui.Field{
124-
ui.PasswordField{
125-
Key: "Token",
126-
Label: "Token",
127-
},
128-
},
129-
}
130-
if !term.IsTerminal(int(syscall.Stdin)) { //nolint:unconvert
131-
gs.Logger.Warn("Stdin is not a terminal, falling back to plain text input")
132-
}
133-
tokenVals, err := tokenForm.Run(gs.Stdin, gs.Stdout)
134-
if err != nil {
135-
return err
136-
}
137-
tokenInput := null.StringFrom(tokenVals["Token"])
138-
if tokenInput.String == "" {
139-
return errors.New("Token cannot be empty")
140-
}
141-
142-
/* Stack form */
143-
stackForm := ui.Form{
144-
Banner: "\nEnter the stack where you want to run k6's commands by default.\n" +
145-
"You can enter a full URL (e.g. https://my-team.grafana.net) or just the slug (e.g. my-team):",
146-
Fields: []ui.Field{
147-
ui.StringField{
148-
Key: "Stack",
149-
Label: "Stack",
150-
},
151-
},
152-
}
153-
stackVals, err := stackForm.Run(gs.Stdin, gs.Stdout)
118+
userinfo, err := promptUserAuthForm(gs)
154119
if err != nil {
155120
return err
156121
}
157-
stackInput := null.StringFrom(strings.TrimSpace(stackVals["Stack"]))
158-
if stackInput.String == "" {
159-
return errors.New("Stack cannot be empty")
160-
}
161122

162-
err = validateInputs(gs, &newCloudConf, currentJSONConfigRaw, tokenInput, stackInput)
123+
err = authenticateUserToken(gs, &newCloudConf, currentJSONConfigRaw,
124+
userinfo.token, userinfo.stack)
163125
if err != nil {
164126
return err
165127
}
@@ -190,6 +152,59 @@ func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error {
190152
return nil
191153
}
192154

155+
type userAuthForm struct {
156+
stack string
157+
token string
158+
}
159+
160+
func promptUserAuthForm(gs *state.GlobalState) (userAuthForm, error) {
161+
/* Token form */
162+
tokenForm := ui.Form{
163+
Banner: "Enter your token to authenticate with Grafana Cloud.\n" +
164+
"Please, consult the documentation for instructions on how to generate one:\n" +
165+
"https://grafana.com/docs/grafana-cloud/testing/k6/author-run/tokens-and-cli-authentication",
166+
Fields: []ui.Field{
167+
ui.PasswordField{
168+
Key: "Token",
169+
Label: "Token",
170+
},
171+
},
172+
}
173+
if !term.IsTerminal(int(syscall.Stdin)) { //nolint:unconvert
174+
gs.Logger.Warn("Stdin is not a terminal, falling back to plain text input")
175+
}
176+
tokenVals, err := tokenForm.Run(gs.Stdin, gs.Stdout)
177+
if err != nil {
178+
return userAuthForm{}, err
179+
}
180+
token := strings.TrimSpace(tokenVals["Token"])
181+
if token == "" {
182+
return userAuthForm{}, errors.New("Token cannot be empty")
183+
}
184+
185+
/* Stack form */
186+
stackForm := ui.Form{
187+
Banner: "\nEnter the stack where you want to run k6's commands by default.\n" +
188+
"You can enter a full URL (e.g. https://my-team.grafana.net) or just the slug (e.g. my-team):",
189+
Fields: []ui.Field{
190+
ui.StringField{
191+
Key: "Stack",
192+
Label: "Stack",
193+
},
194+
},
195+
}
196+
stackVals, err := stackForm.Run(gs.Stdin, gs.Stdout)
197+
if err != nil {
198+
return userAuthForm{}, err
199+
}
200+
stack := strings.TrimSpace(stackVals["Stack"])
201+
if stack == "" {
202+
return userAuthForm{}, errors.New("Stack cannot be empty")
203+
}
204+
205+
return userAuthForm{token: token, stack: stack}, nil
206+
}
207+
193208
func printConfig(gs *state.GlobalState, cloudConf cloudapi.Config) {
194209
const notSet = "<not set>"
195210
token, stackID, stackURL, defProj := notSet, notSet, notSet, notSet
@@ -218,15 +233,15 @@ func printConfig(gs *state.GlobalState, cloudConf cloudapi.Config) {
218233
printToStdout(gs, fmt.Sprintf(" default-project-id: %s\n", valueColor.Sprint(defProj)))
219234
}
220235

221-
// validateInputs validates a token and a stack if provided
236+
// tokenAuthentication validates a token and a stack
222237
// and update the config with the given inputs
223-
func validateInputs(
238+
func authenticateUserToken(
224239
gs *state.GlobalState,
225240
config *cloudapi.Config,
226241
rawConfig json.RawMessage,
227-
token, stack null.String,
242+
token, stack string,
228243
) error {
229-
config.Token = token
244+
config.Token = null.StringFrom(token)
230245
consolidatedCurrentConfig, warn, err := cloudapi.GetConsolidatedConfig(
231246
rawConfig, gs.Env, "", nil)
232247
if err != nil {
@@ -237,7 +252,7 @@ func validateInputs(
237252
}
238253

239254
stackURL, stackID, defaultProjectID, err := validateTokenV6(
240-
gs, consolidatedCurrentConfig, token.String, stack.String)
255+
gs, consolidatedCurrentConfig, token, stack)
241256
if err != nil {
242257
return fmt.Errorf(
243258
"Authentication failed as provided token or stack might not be valid."+

0 commit comments

Comments
 (0)