Skip to content

Commit 772ebec

Browse files
committed
feat(profiles): add remove command in a separated folder
1 parent 7575727 commit 772ebec

6 files changed

Lines changed: 122 additions & 97 deletions

File tree

cmd/profile_delete.go

Lines changed: 0 additions & 94 deletions
This file was deleted.

cmd/profile_remove.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import (
4+
"github.com/RewriteToday/cli/internal/commands/profiles"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var profileDelCmd = &cobra.Command{
9+
Use: "remove <name>",
10+
Aliases: []string{"rm", "del", "delete"},
11+
Short: "Remove a logged-in profile",
12+
Args: cobra.MaximumNArgs(1),
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
noColor, _ := cmd.Flags().GetBool("no-color")
15+
interactive, _ := cmd.Flags().GetBool("interactive")
16+
17+
return profiles.Remove(profiles.RemoveOpts{
18+
Args: args,
19+
NoColor: noColor,
20+
Interactive: interactive,
21+
})
22+
},
23+
}
24+
25+
func init() {
26+
profileCmd.AddCommand(profileDelCmd)
27+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/rhysd/go-github-selfupdate v1.2.3
1010
github.com/spf13/cobra v1.10.2
1111
github.com/zalando/go-keyring v0.2.6
12+
golang.org/x/term v0.40.0
1213
)
1314

1415
require (
@@ -53,7 +54,6 @@ require (
5354
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288 // indirect
5455
golang.org/x/sync v0.19.0 // indirect
5556
golang.org/x/sys v0.41.0 // indirect
56-
golang.org/x/term v0.40.0 // indirect
5757
golang.org/x/text v0.32.0 // indirect
5858
google.golang.org/appengine v1.3.0 // indirect
5959
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
146146
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
147147
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
148148
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
149-
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
150-
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
151149
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
152150
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
153151
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package profiles
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/RewriteToday/cli/internal/profile"
7+
"github.com/RewriteToday/cli/internal/render"
8+
"github.com/RewriteToday/cli/internal/style"
9+
)
10+
11+
type RemoveOpts struct {
12+
Args []string
13+
Interactive, NoColor bool
14+
}
15+
16+
func Remove(opts RemoveOpts) error {
17+
name, err := resolveName(opts.Args)
18+
19+
if err != nil {
20+
return err
21+
}
22+
23+
if opts.Interactive {
24+
confirmed, err := confirmRemoval(name)
25+
26+
if err != nil {
27+
return err
28+
}
29+
30+
if !confirmed {
31+
fmt.Println("Cancelled successfully.")
32+
33+
return nil
34+
}
35+
}
36+
37+
if err := profile.Delete(name); err != nil {
38+
return err
39+
}
40+
41+
fmt.Printf("Your profile %s was deleted successfully", render.Paint(name, render.Gray, opts.NoColor))
42+
43+
return nil
44+
}
45+
46+
func confirmRemoval(name string) (bool, error) {
47+
confirmed, err := style.Confirm("Are you sure you want to remove this profile?")
48+
49+
if err != nil {
50+
return false, err
51+
}
52+
53+
return confirmed, err
54+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package profiles
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/RewriteToday/cli/internal/profile"
7+
"github.com/RewriteToday/cli/internal/style"
8+
)
9+
10+
func resolveName(args []string) (string, error) {
11+
var name string
12+
13+
if len(args) > 0 {
14+
name = args[0]
15+
}
16+
17+
if name == "" {
18+
profiles, err := profile.List()
19+
20+
if err != nil {
21+
return "", err
22+
}
23+
24+
if len(profiles) == 0 {
25+
return "", fmt.Errorf("we did not find any profile")
26+
}
27+
28+
name, err := style.SelectString("Select a profile", profiles)
29+
30+
if err != nil {
31+
return "", err
32+
}
33+
34+
if name == "" {
35+
return "", fmt.Errorf("a profile name is required")
36+
}
37+
}
38+
39+
return name, nil
40+
}

0 commit comments

Comments
 (0)