|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "slices" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/G-core/gcore-cli/internal/config" |
| 10 | + "github.com/G-core/gcore-cli/internal/core" |
| 11 | + "github.com/G-core/gcore-cli/internal/output" |
| 12 | +) |
| 13 | + |
| 14 | +func Commands() *cobra.Command { |
| 15 | + var cmd = &cobra.Command{ |
| 16 | + Use: "config", |
| 17 | + Short: "Config file management", |
| 18 | + GroupID: "configuration", |
| 19 | + Run: func(cmd *cobra.Command, args []string) { |
| 20 | + cmd.Help() |
| 21 | + }, |
| 22 | + } |
| 23 | + |
| 24 | + cmd.AddCommand(info(), get(), set(), unset(), dump(), profileCmd()) |
| 25 | + return cmd |
| 26 | +} |
| 27 | + |
| 28 | +func profileCmd() *cobra.Command { |
| 29 | + var cmd = &cobra.Command{ |
| 30 | + Use: "profile", |
| 31 | + Short: "Commands to manage profiles from the config", |
| 32 | + Run: func(cmd *cobra.Command, args []string) { |
| 33 | + cmd.Help() |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + cmd.AddCommand(listProfiles(), switchProfileCmd(), deleteProfileCmd()) |
| 38 | + return cmd |
| 39 | +} |
| 40 | + |
| 41 | +func deleteProfileCmd() *cobra.Command { |
| 42 | + cmd := &cobra.Command{ |
| 43 | + Use: "delete <profile>", |
| 44 | + Aliases: []string{"d"}, |
| 45 | + ValidArgsFunction: core.ProfileCompletion, |
| 46 | + Short: "Delete profile from the config", |
| 47 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 48 | + if len(args) == 0 { |
| 49 | + cmd.Help() |
| 50 | + |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + profileName := args[0] |
| 55 | + ctx := cmd.Context() |
| 56 | + cfg := core.ExtractConfig(ctx) |
| 57 | + active := core.ExtractProfile(ctx) |
| 58 | + |
| 59 | + _, exist := cfg.Profiles[profileName] |
| 60 | + if exist { |
| 61 | + delete(cfg.Profiles, profileName) |
| 62 | + } else { |
| 63 | + return fmt.Errorf("profile '%s' doesn't exist", profileName) |
| 64 | + } |
| 65 | + |
| 66 | + if active == profileName { |
| 67 | + cfg.ActiveProfile = config.DefaultProfile |
| 68 | + } |
| 69 | + |
| 70 | + path, err := core.ExtractConfigPath(ctx) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + return cfg.Save(path) |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + return cmd |
| 80 | +} |
| 81 | + |
| 82 | +func listProfiles() *cobra.Command { |
| 83 | + cmd := &cobra.Command{ |
| 84 | + Use: "list", |
| 85 | + Aliases: []string{"ls"}, |
| 86 | + Short: "Display list of available profiles in the config", |
| 87 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 88 | + ctx := cmd.Context() |
| 89 | + cfg := core.ExtractConfig(ctx) |
| 90 | + |
| 91 | + profiles := append([]profileView{}, toProfileView(config.DefaultProfile, &cfg.Profile)) |
| 92 | + |
| 93 | + var names []string |
| 94 | + for name, _ := range cfg.Profiles { |
| 95 | + names = append(names, name) |
| 96 | + } |
| 97 | + slices.Sort(names) |
| 98 | + |
| 99 | + for _, name := range names { |
| 100 | + pv := toProfileView(name, cfg.Profiles[name]) |
| 101 | + |
| 102 | + profiles = append(profiles, pv) |
| 103 | + } |
| 104 | + |
| 105 | + output.Print(profiles) |
| 106 | + |
| 107 | + return nil |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + return cmd |
| 112 | +} |
| 113 | + |
| 114 | +func switchProfileCmd() *cobra.Command { |
| 115 | + cmd := &cobra.Command{ |
| 116 | + Use: "switch <profile>", |
| 117 | + ValidArgsFunction: core.ProfileCompletion, |
| 118 | + Short: "Make selected profile active", |
| 119 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 120 | + if len(args) == 0 { |
| 121 | + cmd.Help() |
| 122 | + |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + profileName := args[0] |
| 127 | + ctx := cmd.Context() |
| 128 | + cfg := core.ExtractConfig(ctx) |
| 129 | + |
| 130 | + _, exist := cfg.Profiles[profileName] |
| 131 | + if exist { |
| 132 | + cfg.ActiveProfile = profileName |
| 133 | + } else if profileName != config.DefaultProfile { |
| 134 | + return fmt.Errorf("profile '%s' doesn't exist", profileName) |
| 135 | + } else { |
| 136 | + cfg.ActiveProfile = config.DefaultProfile |
| 137 | + } |
| 138 | + |
| 139 | + path, err := core.ExtractConfigPath(ctx) |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + return cfg.Save(path) |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + return cmd |
| 149 | +} |
0 commit comments