|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | 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" |
8 | 5 | "github.com/spf13/cobra" |
9 | 6 | ) |
10 | 7 |
|
11 | 8 | var switchCmd = &cobra.Command{ |
12 | 9 | Use: "switch [profile-name]", |
13 | 10 | Short: "Switch the active profile", |
14 | 11 | 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 | + }, |
62 | 22 | } |
63 | 23 |
|
64 | 24 | func init() { |
|
0 commit comments