From d1e05591163d3c47211293d0c950b8b224617339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Denoix?= Date: Thu, 1 Sep 2022 11:06:48 +0200 Subject: [PATCH] fix(config) Remove profile without default (#44) --- pkg/cmd/profile/remove/remove.go | 14 +++++++++++--- pkg/cmd/profile/remove/remove_test.go | 8 +++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/profile/remove/remove.go b/pkg/cmd/profile/remove/remove.go index 208b89fd..6f850ae9 100644 --- a/pkg/cmd/profile/remove/remove.go +++ b/pkg/cmd/profile/remove/remove.go @@ -18,7 +18,8 @@ type RemoveOptions struct { config config.IConfig IO *iostreams.IOStreams - Profile string + Profile string + DefaultProfile string DoConfirm bool } @@ -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") } @@ -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 { diff --git a/pkg/cmd/profile/remove/remove_test.go b/pkg/cmd/profile/remove/remove_test.go index 7790f680..c1151d20 100644 --- a/pkg/cmd/profile/remove/remove_test.go +++ b/pkg/cmd/profile/remove/remove_test.go @@ -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 {