Skip to content

Commit

Permalink
fix(profile/update): update active profile by default not the 'user' …
Browse files Browse the repository at this point in the history
…profile (#857)

* fix(profile/update): display profile being updated

* fix(profile/update): update active profile by default not the 'user' profile
  • Loading branch information
Integralist authored Mar 7, 2023
1 parent c30cc56 commit 6e6f68d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
37 changes: 27 additions & 10 deletions pkg/commands/profile/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,39 @@ func NewUpdateCommand(parent cmd.Registerer, cf APIClientFactory, g *global.Data
var c UpdateCommand
c.Globals = g
c.CmdClause = parent.Command("update", "Update user profile")
c.CmdClause.Arg("profile", "Profile to update (default 'user')").Default("user").Short('p').StringVar(&c.profile)
c.CmdClause.Arg("profile", "Profile to update (defaults to the currently active profile)").Short('p').StringVar(&c.profile)
c.clientFactory = cf
return &c
}

// Exec invokes the application logic for the command.
func (c *UpdateCommand) Exec(in io.Reader, out io.Writer) error {
name, p := profile.Get(c.profile, c.Globals.Config.Profiles)
if name == "" {
msg := fmt.Sprintf(profile.DoesNotExist, c.profile)
return fsterr.RemediationError{
Inner: fmt.Errorf(msg),
Remediation: fsterr.ProfileRemediation,
var (
name string
p *config.Profile
)

if c.profile == "" {
name, p = profile.Default(c.Globals.Config.Profiles)
if name == "" {
return fsterr.RemediationError{
Inner: fmt.Errorf("no active profile"),
Remediation: profile.NoDefaults,
}
}
} else {
name, p = profile.Get(c.profile, c.Globals.Config.Profiles)
if name == "" {
msg := fmt.Sprintf(profile.DoesNotExist, c.profile)
return fsterr.RemediationError{
Inner: fmt.Errorf(msg),
Remediation: fsterr.ProfileRemediation,
}
}
}

text.Info(out, "Profile being updated: '%s'.", name)

opts := []profile.EditOption{}

text.Break(out)
Expand Down Expand Up @@ -103,9 +120,9 @@ func (c *UpdateCommand) Exec(in io.Reader, out io.Writer) error {

var ok bool

ps, ok := profile.Edit(c.profile, c.Globals.Config.Profiles, opts...)
ps, ok := profile.Edit(name, c.Globals.Config.Profiles, opts...)
if !ok {
msg := fmt.Sprintf(profile.DoesNotExist, c.profile)
msg := fmt.Sprintf(profile.DoesNotExist, name)
return fsterr.RemediationError{
Inner: fmt.Errorf(msg),
Remediation: fsterr.ProfileRemediation,
Expand All @@ -118,7 +135,7 @@ func (c *UpdateCommand) Exec(in io.Reader, out io.Writer) error {
return fmt.Errorf("error saving config file: %w", err)
}

text.Success(out, "Profile '%s' updated", c.profile)
text.Success(out, "Profile '%s' updated", name)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
const DoesNotExist = "the profile '%s' does not exist"

// NoDefaults describes an output warning message.
const NoDefaults = "At least one account profile should be set as the 'default'. Run `fastly profile update <NAME>`."
const NoDefaults = "At least one account profile should be set as the 'default'. Run `fastly profile update <NAME>` and ensure the profile is set to be the default."

// Exist reports whether the given profile exists.
func Exist(name string, p config.Profiles) bool {
Expand All @@ -29,7 +29,7 @@ func Exist(name string, p config.Profiles) bool {
return false
}

// Default returns the default profile.
// Default returns the default profile (which is the active profile).
func Default(p config.Profiles) (string, *config.Profile) {
for k, v := range p {
if v.Default {
Expand Down

0 comments on commit 6e6f68d

Please sign in to comment.