Skip to content

Commit

Permalink
fix(config) Remove profile without default (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
clemfromspace authored Sep 1, 2022
1 parent 7943a08 commit d1e0559
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 11 additions & 3 deletions pkg/cmd/profile/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type RemoveOptions struct {
config config.IConfig
IO *iostreams.IOStreams

Profile string
Profile string
DefaultProfile string

DoConfirm bool
}
Expand All @@ -44,7 +45,14 @@ func NewRemoveCmd(f *cmdutil.Factory, runF func(*RemoveOptions) error) *cobra.Co
`),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Profile = args[0]
if !confirm && opts.Profile == opts.config.Default().Name {

if opts.config.Default() != nil {
opts.DefaultProfile = opts.config.Default().Name
} else {
opts.DefaultProfile = ""
}

if !confirm && opts.Profile == opts.DefaultProfile {
if !opts.IO.CanPrompt() {
return cmdutil.FlagErrorf("--confirm required when non-interactive shell is detected")
}
Expand Down Expand Up @@ -89,7 +97,7 @@ func runRemoveCmd(opts *RemoveOptions) error {
cs := opts.IO.ColorScheme()
if opts.IO.IsStdoutTTY() {
extra := "."
if opts.config.Default().Name == opts.Profile {
if opts.DefaultProfile == opts.Profile {
extra = ". Set a new default profile with 'algolia profile setdefault'."
}
if len(opts.config.ConfiguredProfiles()) == 0 {
Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/profile/remove/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,17 @@ func Test_runRemoveCmd(t *testing.T) {
wantsErr: "the specified profile does not exist: 'bar'",
},
{
name: "only one profile",
name: "only one profile (default)",
cli: "default --confirm",
profiles: map[string]bool{"default": true},
wantOut: "✓ 'default' removed successfully. Add a profile with 'algolia profile add'.\n",
},
{
name: "only one profile (non-default)",
cli: "foo --confirm",
profiles: map[string]bool{"foo": false},
wantOut: "✓ 'foo' removed successfully. Add a profile with 'algolia profile add'.\n",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit d1e0559

Please # to comment.