Skip to content

Commit 0f38427

Browse files
authored
fix(sso): update output to reflect default profile behaviour (#1232)
1 parent aac31d1 commit 0f38427

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

pkg/commands/sso/root.go

+46-36
Original file line numberDiff line numberDiff line change
@@ -72,41 +72,6 @@ func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand {
7272
func (c *RootCommand) Exec(in io.Reader, out io.Writer) error {
7373
profileName, _ := c.identifyProfileAndFlow()
7474

75-
// We need to prompt the user, so they know we're about to open their web
76-
// browser, but we also need to handle the scenario where the `sso` command is
77-
// invoked indirectly via ../../app/run.go as that package will have its own
78-
// (similar) prompt before invoking this command. So to avoid a double prompt,
79-
// the app package will set `SkipAuthPrompt: true`.
80-
if !c.Globals.SkipAuthPrompt && !c.Globals.Flags.AutoYes && !c.Globals.Flags.NonInteractive {
81-
msg := fmt.Sprintf("We're going to authenticate the '%s' profile", profileName)
82-
text.Important(out, "%s. We need to open your browser to authenticate you.", msg)
83-
text.Break(out)
84-
cont, err := text.AskYesNo(out, text.BoldYellow("Do you want to continue? [y/N]: "), in)
85-
text.Break(out)
86-
if err != nil {
87-
return err
88-
}
89-
if !cont {
90-
return fsterr.SkipExitError{
91-
Skip: true,
92-
Err: fsterr.ErrDontContinue,
93-
}
94-
}
95-
}
96-
97-
var serverErr error
98-
go func() {
99-
err := c.Globals.AuthServer.Start()
100-
if err != nil {
101-
serverErr = err
102-
}
103-
}()
104-
if serverErr != nil {
105-
return serverErr
106-
}
107-
108-
text.Info(out, "Starting a local server to handle the authentication flow.")
109-
11075
// For creating/updating a profile we set `prompt` because we want to ensure
11176
// that another session (from a different profile) doesn't cause unexpected
11277
// errors for the user flow. This forces a re-auth.
@@ -122,12 +87,18 @@ func (c *RootCommand) Exec(in io.Reader, out io.Writer) error {
12287
c.Globals.AuthServer.SetParam("account_hint", c.ProfileSwitchCustomerID)
12388
}
12489
default:
90+
if c.profile != "" {
91+
profileName = c.profile
92+
}
12593
// Handle `fastly sso` being invoked directly.
12694
p := profile.Get(profileName, c.Globals.Config.Profiles)
12795
if p == nil {
12896
err := fmt.Errorf(profile.DoesNotExist, profileName)
12997
c.Globals.ErrLog.Add(err)
130-
return err
98+
return fsterr.RemediationError{
99+
Inner: err,
100+
Remediation: fsterr.ProfileRemediation,
101+
}
131102
}
132103
c.Globals.AuthServer.SetParam("prompt", "login")
133104
c.Globals.AuthServer.SetParam("login_hint", p.Email)
@@ -142,6 +113,45 @@ func (c *RootCommand) Exec(in io.Reader, out io.Writer) error {
142113
c.InvokedFromSSO = true
143114
}
144115

116+
// We need to prompt the user, so they know we're about to open their web
117+
// browser, but we also need to handle the scenario where the `sso` command is
118+
// invoked indirectly via ../../app/run.go as that package will have its own
119+
// (similar) prompt before invoking this command. So to avoid a double prompt,
120+
// the app package will set `SkipAuthPrompt: true`.
121+
if !c.Globals.SkipAuthPrompt && !c.Globals.Flags.AutoYes && !c.Globals.Flags.NonInteractive {
122+
var defaultMsg string
123+
if c.InvokedFromSSO {
124+
defaultMsg = " and make it the default"
125+
}
126+
msg := fmt.Sprintf("We're going to authenticate the '%s' profile%s", profileName, defaultMsg)
127+
text.Important(out, "%s. We need to open your browser to authenticate you.", msg)
128+
text.Break(out)
129+
cont, err := text.AskYesNo(out, text.BoldYellow("Do you want to continue? [y/N]: "), in)
130+
text.Break(out)
131+
if err != nil {
132+
return err
133+
}
134+
if !cont {
135+
return fsterr.SkipExitError{
136+
Skip: true,
137+
Err: fsterr.ErrDontContinue,
138+
}
139+
}
140+
}
141+
142+
var serverErr error
143+
go func() {
144+
err := c.Globals.AuthServer.Start()
145+
if err != nil {
146+
serverErr = err
147+
}
148+
}()
149+
if serverErr != nil {
150+
return serverErr
151+
}
152+
153+
text.Info(out, "Starting a local server to handle the authentication flow.")
154+
145155
authorizationURL, err := c.Globals.AuthServer.AuthURL()
146156
if err != nil {
147157
return fsterr.RemediationError{

pkg/commands/sso/sso_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func TestSSO(t *testing.T) {
8787
TestScenario: testutil.TestScenario{
8888
Args: args("sso"),
8989
WantOutputs: []string{
90-
"We're going to authenticate the 'user' profile.",
90+
"We're going to authenticate the 'user' profile",
9191
"We need to open your browser to authenticate you.",
9292
"Session token (persisted to your local configuration): 123",
9393
},
@@ -108,7 +108,7 @@ func TestSSO(t *testing.T) {
108108
TestScenario: testutil.TestScenario{
109109
Args: args("sso test_user"),
110110
WantOutputs: []string{
111-
"We're going to authenticate the 'test_user' profile.",
111+
"We're going to authenticate the 'test_user' profile",
112112
"We need to open your browser to authenticate you.",
113113
"Session token (persisted to your local configuration): 123",
114114
},

0 commit comments

Comments
 (0)