Skip to content

Commit

Permalink
fix(profile/update): put back the flow to how it was prior to sso
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Sep 21, 2023
1 parent 361e1c3 commit af581f0
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions pkg/commands/profile/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,22 @@ func (c *UpdateCommand) Exec(in io.Reader, out io.Writer) error {
return fmt.Errorf("failed to identify the profile to update: %w", err)
}
text.Info(out, "Profile being updated: '%s'.", profileName)
text.Break(out)

err = c.updateToken(profileName, p, in, out)
if err != nil {
return fmt.Errorf("failed to update token: %w", err)
}

// Set to true for --auto-yes/--non-interactive flags, otherwise prompt user.
makeDefault := true
updateToken := true

if !c.Globals.Flags.AutoYes && !c.Globals.Flags.NonInteractive {
text.Break(out)
makeDefault, err = text.AskYesNo(out, text.BoldYellow("Make profile the default? [y/N] "), in)
text.Break(out)
if err != nil {
return err
}

updateToken, err = text.AskYesNo(out, text.BoldYellow("Update the token associated with this profile? [y/N]: "), in)
if err != nil {
return err
}
}

if updateToken {
err := c.updateToken(profileName, makeDefault, p, in, out)
if err != nil {
return fmt.Errorf("failed to update token: %w", err)
}
}

if makeDefault {
Expand Down Expand Up @@ -125,7 +117,7 @@ func (c *UpdateCommand) identifyProfile() (string, *config.Profile, error) {
return profileName, p, nil
}

func (c *UpdateCommand) updateToken(profileName string, makeDefault bool, p *config.Profile, in io.Reader, out io.Writer) error {
func (c *UpdateCommand) updateToken(profileName string, p *config.Profile, in io.Reader, out io.Writer) error {
if !c.sso && !isSSOToken(p) {
text.Info(out, "When updating a profile you can either paste in a long-lived token or allow the Fastly CLI to generate a short-lived token that can be automatically refreshed. To update this profile to use an SSO-based token, pass the `--sso` flag: `fastly profile update --sso`.")
}
Expand All @@ -137,7 +129,7 @@ func (c *UpdateCommand) updateToken(profileName string, makeDefault bool, p *con
// the specific profile.
c.authCmd.InvokedFromProfileUpdate = true
c.authCmd.ProfileUpdateName = profileName
c.authCmd.ProfileDefault = makeDefault
c.authCmd.ProfileDefault = false // set to false, as later we prompt for this

// NOTE: The `sso` command already handles writing config back to disk.
// So unlike `c.staticTokenFlow` (below) we don't have to do that here.
Expand All @@ -147,7 +139,7 @@ func (c *UpdateCommand) updateToken(profileName string, makeDefault bool, p *con
}
text.Break(out)
} else {
if err := c.staticTokenFlow(profileName, makeDefault, p, in, out); err != nil {
if err := c.staticTokenFlow(profileName, p, in, out); err != nil {
return fmt.Errorf("failed to process the static token flow: %w", err)
}
// Write the in-memory representation back to disk.
Expand Down Expand Up @@ -246,7 +238,7 @@ func (c *UpdateCommand) validateToken(token, endpoint string, spinner text.Spinn
return user.Login, nil
}

func (c *UpdateCommand) staticTokenFlow(profileName string, makeDefault bool, p *config.Profile, in io.Reader, out io.Writer) error {
func (c *UpdateCommand) staticTokenFlow(profileName string, p *config.Profile, in io.Reader, out io.Writer) error {
opts := []profile.EditOption{}

text.Break(out)
Expand All @@ -268,7 +260,7 @@ func (c *UpdateCommand) staticTokenFlow(profileName string, makeDefault bool, p
text.Break(out)

opts = append(opts, func(p *config.Profile) {
p.Default = makeDefault
p.Default = false // set to false, as later we prompt for this
})

text.Break(out)
Expand Down

0 comments on commit af581f0

Please sign in to comment.