11package cmd
22
33import (
4+ "fmt"
5+ "io"
46 "os"
7+ "sort"
58
9+ "charm.land/huh/v2"
610 "github.com/spf13/cobra"
711)
812
913// Del returns `del` command
1014func Del (cfg storage ) * cobra.Command {
15+ return delCommand (cfg , promptDeleteProfile )
16+ }
17+
18+ //nolint:funlen
19+ func delCommand (cfg storage , prompt func (storage , io.Writer ) (string , error )) * cobra.Command {
1120 return & cobra.Command {
1221 Use : "del [profile] [key]" ,
1322 Aliases : []string {"rm" },
@@ -17,27 +26,49 @@ func Del(cfg storage) *cobra.Command {
1726 `Enter the "key" argument to remove the exact key from the profile.` ,
1827 ),
1928 Example : multiline (
29+ `git-profile del` ,
2030 `git-profile del my-profile (delete the entire profile)` ,
2131 `git-profile del my-profile user.name (deleting only a certain key)` ,
2232 ),
23- Args : cobra .RangeArgs (1 , 2 ),
33+ Args : func (_ * cobra.Command , args []string ) error {
34+ if len (args ) <= 2 { //nolint:mnd
35+ return nil
36+ }
37+
38+ return fmt .Errorf ("accepts at most 2 arguments" )
39+ },
2440 Run : func (cmd * cobra.Command , args []string ) {
25- profile := args [0 ]
2641 filename , _ := cmd .Flags ().GetString ("config" )
2742
28- if len (args ) == 1 { //nolint
29- if ok := cfg .DeleteProfile (profile ); ! ok {
30- cmd .PrintErrln ("There is no profile with given name" )
43+ if len (args ) == 0 {
44+ profile , err := prompt (cfg , cmd .OutOrStdout ())
45+ if err != nil {
46+ if err == huh .ErrUserAborted {
47+ cmd .Println ("Interactive delete cancelled." )
48+ return
49+ }
50+
51+ cmd .PrintErrln (err )
3152 os .Exit (1 )
3253 }
3354
34- err := cfg . Save ( filename )
55+ err = deleteProfile ( cmd , cfg , filename , profile )
3556 if err != nil {
3657 cmd .PrintErrln ("Unable to save config file:" , err )
3758 os .Exit (1 )
3859 }
3960
40- cmd .Printf ("Successfully removed `%s` profile.\n " , profile )
61+ return
62+ }
63+
64+ profile := args [0 ]
65+
66+ if len (args ) == 1 {
67+ err := deleteProfile (cmd , cfg , filename , profile )
68+ if err != nil {
69+ cmd .PrintErrln ("Unable to save config file:" , err )
70+ os .Exit (1 )
71+ }
4172 } else {
4273 if ok := cfg .Delete (profile , args [1 ]); ! ok {
4374 cmd .PrintErrln ("There is no profile with given name" )
@@ -55,3 +86,42 @@ func Del(cfg storage) *cobra.Command {
5586 },
5687 }
5788}
89+
90+ func promptDeleteProfile (cfg storage , _ io.Writer ) (string , error ) {
91+ if cfg .Len () == 0 {
92+ return "" , fmt .Errorf ("There are no available profiles" )
93+ }
94+
95+ names := cfg .Names ()
96+ sort .Strings (names )
97+
98+ var profile string
99+
100+ err := huh .NewSelect [string ]().
101+ Title ("Select a profile to delete" ).
102+ Options (huh .NewOptions (names ... )... ).
103+ Value (& profile ).
104+ WithKeyMap (huh .NewDefaultKeyMap ()).
105+ Run ()
106+ if err != nil {
107+ return "" , err
108+ }
109+
110+ return profile , nil
111+ }
112+
113+ func deleteProfile (cmd * cobra.Command , cfg storage , filename string , profile string ) error {
114+ if ok := cfg .DeleteProfile (profile ); ! ok {
115+ cmd .PrintErrln ("There is no profile with given name" )
116+ os .Exit (1 )
117+ }
118+
119+ err := cfg .Save (filename )
120+ if err != nil {
121+ return err
122+ }
123+
124+ cmd .Printf ("Successfully removed `%s` profile.\n " , profile )
125+
126+ return nil
127+ }
0 commit comments