Skip to content

Commit f8416d0

Browse files
committed
command/login: Require "yes" to confirm
This is for consistency with other commands which use prompts, all of which require "yes" rather than "y" to confirm.
1 parent 9f455bf commit f8416d0

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

command/login.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ func (c *LoginCommand) interactiveGetTokenByUI(hostname svchost.Hostname, credsC
494494
return "", diags
495495
}
496496

497+
c.Ui.Output("\n---------------------------------------------------------------------------------\n")
498+
497499
tokensURL := url.URL{
498500
Scheme: "https",
499501
Host: service.Hostname(),
@@ -508,6 +510,7 @@ func (c *LoginCommand) interactiveGetTokenByUI(hostname svchost.Hostname, credsC
508510
c.Ui.Output(fmt.Sprintf("Terraform must now open a web browser to the tokens page for %s.\n", hostname.ForDisplay()))
509511
c.Ui.Output(fmt.Sprintf("If a browser does not open this automatically, open the following URL to proceed:\n %s\n", tokensURL.String()))
510512
} else {
513+
log.Printf("[DEBUG] error opening web browser: %s", err)
511514
// Assume we're on a platform where opening a browser isn't possible.
512515
launchBrowserManually = true
513516
}
@@ -590,20 +593,15 @@ func (c *LoginCommand) interactiveContextConsent(hostname svchost.Hostname, gran
590593
}
591594
}
592595

593-
v, err := c.Ui.Ask("Do you want to proceed? (y/n)")
596+
v, err := c.Ui.Ask("Do you want to proceed? Only 'yes' will be accepted to confirm.")
594597
if err != nil {
595598
// Should not happen because this command checks that input is enabled
596599
// before we get to this point.
597600
diags = diags.Append(err)
598601
return false, diags
599602
}
600603

601-
switch strings.ToLower(v) {
602-
case "y", "yes":
603-
return true, diags
604-
default:
605-
return false, diags
606-
}
604+
return strings.ToLower(v) == "yes", diags
607605
}
608606

609607
func (c *LoginCommand) listenerForCallback(minPort, maxPort uint16) (net.Listener, string, error) {

command/login_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,30 @@ func TestLogin(t *testing.T) {
186186
t.Fatalf("missing expected error message\nwant: %s\nfull output:\n%s", want, got)
187187
}
188188
}))
189+
190+
t.Run("answering no cancels", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi, inp func(string)) {
191+
// Enter "no" at the consent prompt
192+
inp("no\n")
193+
status := c.Run(nil)
194+
if status != 1 {
195+
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
196+
}
197+
198+
if got, want := ui.ErrorWriter.String(), "Login cancelled"; !strings.Contains(got, want) {
199+
t.Fatalf("missing expected error message\nwant: %s\nfull output:\n%s", want, got)
200+
}
201+
}))
202+
203+
t.Run("answering y cancels", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi, inp func(string)) {
204+
// Enter "y" at the consent prompt
205+
inp("y\n")
206+
status := c.Run(nil)
207+
if status != 1 {
208+
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
209+
}
210+
211+
if got, want := ui.ErrorWriter.String(), "Login cancelled"; !strings.Contains(got, want) {
212+
t.Fatalf("missing expected error message\nwant: %s\nfull output:\n%s", want, got)
213+
}
214+
}))
189215
}

0 commit comments

Comments
 (0)