Skip to content

Commit a326d47

Browse files
committed
feat(switch): use command in a separated folder
1 parent f523bc8 commit a326d47

3 files changed

Lines changed: 40 additions & 52 deletions

File tree

cmd/switch.go

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,24 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
6-
"github.com/RewriteToday/cli/internal/profile"
7-
"github.com/RewriteToday/cli/internal/style"
4+
"github.com/RewriteToday/cli/internal/commands/profiles"
85
"github.com/spf13/cobra"
96
)
107

118
var switchCmd = &cobra.Command{
129
Use: "switch [profile-name]",
1310
Short: "Switch the active profile",
1411
Args: cobra.MaximumNArgs(1),
15-
RunE: runSwitchCommand,
16-
}
17-
18-
func runSwitchCommand(cmd *cobra.Command, args []string) error {
19-
interactive, _ := cmd.Flags().GetBool("interactive")
20-
format, _ := cmd.Flags().GetString("output")
21-
noColor, _ := cmd.Flags().GetBool("no-color")
22-
23-
name, err := resolveSwitchProfileName(args, interactive)
24-
if err != nil {
25-
return err
26-
}
27-
28-
if err := profile.SetActive(name); err != nil {
29-
return err
30-
}
31-
32-
return style.Print(fmt.Sprintf("Switched to profile '%s'", name), format, noColor)
33-
}
34-
35-
func resolveSwitchProfileName(args []string, interactive bool) (string, error) {
36-
var name string
37-
if len(args) > 0 {
38-
name = args[0]
39-
}
40-
41-
if name == "" && interactive {
42-
profiles, err := profile.List()
43-
if err != nil {
44-
return "", err
45-
}
46-
47-
if len(profiles) == 0 {
48-
return "", fmt.Errorf("no profiles to switch")
49-
}
50-
51-
name, err = style.SelectString("Select a profile", profiles)
52-
if err != nil {
53-
return "", err
54-
}
55-
}
56-
57-
if name == "" {
58-
return "", fmt.Errorf("profile name required (or use -i for interactive mode)")
59-
}
60-
61-
return name, nil
12+
RunE: func(cmd *cobra.Command, args []string) error {
13+
format, _ := cmd.Flags().GetString("output")
14+
noColor, _ := cmd.Flags().GetBool("no-color")
15+
16+
return profiles.Switch(profiles.SwitchOpts{
17+
Args: args,
18+
Format: format,
19+
NoColor: noColor,
20+
})
21+
},
6222
}
6323

6424
func init() {

cmd/whoami.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
var whoamiCmd = &cobra.Command{
99
Use: "whoami",
1010
Short: "Show the current active profile",
11-
RunE: func(cmd *cobra.Command, args []string) error {
11+
RunE: func(cmd *cobra.Command, args []string) error {
1212
format, _ := cmd.Flags().GetString("output")
1313
noColor, _ := cmd.Flags().GetBool("no-color")
1414

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
type SwitchOpts struct {
11+
NoColor, Interactive bool
12+
Format string
13+
Args []string
14+
}
15+
16+
func Switch(opts SwitchOpts) error {
17+
name, err := resolveName(opts.Args)
18+
19+
if err != nil {
20+
return err
21+
}
22+
23+
if err := profile.SetActive(name); err != nil {
24+
return err
25+
}
26+
27+
return style.Print(fmt.Sprintf("Switched to profile '%s'", name), opts.Format, opts.NoColor)
28+
}

0 commit comments

Comments
 (0)