|
| 1 | +package info |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + |
| 9 | + "github.com/scaleway/scaleway-cli/internal/core" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | +) |
| 12 | + |
| 13 | +func GetCommands() *core.Commands { |
| 14 | + return core.NewCommands( |
| 15 | + infosRoot(), |
| 16 | + ) |
| 17 | +} |
| 18 | + |
| 19 | +type setting struct { |
| 20 | + Key string |
| 21 | + Value string |
| 22 | + Origin string |
| 23 | +} |
| 24 | + |
| 25 | +func infosRoot() *core.Command { |
| 26 | + type infoArgs struct { |
| 27 | + ShowSecret bool |
| 28 | + } |
| 29 | + |
| 30 | + return &core.Command{ |
| 31 | + Short: `Get settings about current settings`, |
| 32 | + Namespace: "info", |
| 33 | + AllowAnonymousClient: true, |
| 34 | + ArgsType: reflect.TypeOf(infoArgs{}), |
| 35 | + ArgSpecs: core.ArgSpecs{ |
| 36 | + { |
| 37 | + Name: "show-secret", |
| 38 | + Short: `Reveal secret`, |
| 39 | + Required: false, |
| 40 | + Default: core.DefaultValueSetter("false"), |
| 41 | + }, |
| 42 | + }, |
| 43 | + Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) { |
| 44 | + req := argsI.(*infoArgs) |
| 45 | + config, _ := scw.LoadConfigFromPath(core.ExtractConfigPath(ctx)) |
| 46 | + profileName := core.ExtractProfileName(ctx) |
| 47 | + return []*setting{ |
| 48 | + configPath(ctx), |
| 49 | + defaultRegion(ctx, config, profileName), |
| 50 | + defaultZone(ctx, config, profileName), |
| 51 | + defaultOrganizationId(ctx, config, profileName), |
| 52 | + accessKey(ctx, config, profileName), |
| 53 | + secretKey(ctx, config, profileName, req.ShowSecret), |
| 54 | + profile(ctx), |
| 55 | + }, nil |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func configPath(ctx context.Context) *setting { |
| 61 | + setting := &setting{ |
| 62 | + Key: "config_path", |
| 63 | + Value: core.ExtractConfigPath(ctx), |
| 64 | + } |
| 65 | + switch { |
| 66 | + case core.ExtractConfigPathFlag(ctx) != "": |
| 67 | + setting.Origin = "flag --config/-c" |
| 68 | + case os.Getenv(scw.ScwConfigPathEnv) != "": |
| 69 | + setting.Origin = fmt.Sprintf("env (%s)", scw.ScwConfigPathEnv) |
| 70 | + default: |
| 71 | + setting.Origin = "default" |
| 72 | + } |
| 73 | + return setting |
| 74 | +} |
| 75 | + |
| 76 | +func defaultRegion(ctx context.Context, config *scw.Config, profileName string) *setting { |
| 77 | + setting := &setting{Key: "default_region"} |
| 78 | + client := core.ExtractClient(ctx) |
| 79 | + defaultRegion, exists := client.GetDefaultRegion() |
| 80 | + if exists { |
| 81 | + setting.Value = defaultRegion.String() |
| 82 | + } |
| 83 | + switch { |
| 84 | + // Environment variable check |
| 85 | + case os.Getenv(scw.ScwDefaultRegionEnv) != "": |
| 86 | + setting.Origin = fmt.Sprintf("env (%s)", scw.ScwDefaultRegionEnv) |
| 87 | + // There is no config file |
| 88 | + case config == nil: |
| 89 | + setting.Origin = "default" |
| 90 | + // Config file with profile name |
| 91 | + case config.Profiles[profileName] != nil && config.Profiles[profileName].DefaultRegion != nil: |
| 92 | + setting.Origin = fmt.Sprintf("profile (%s)", profileName) |
| 93 | + // Default config |
| 94 | + case config.Profile.DefaultRegion != nil: |
| 95 | + setting.Origin = "default profile" |
| 96 | + default: |
| 97 | + setting.Origin = "default" |
| 98 | + } |
| 99 | + return setting |
| 100 | +} |
| 101 | + |
| 102 | +func defaultZone(ctx context.Context, config *scw.Config, profileName string) *setting { |
| 103 | + setting := &setting{Key: "default_zone"} |
| 104 | + client := core.ExtractClient(ctx) |
| 105 | + defaultZone, exists := client.GetDefaultZone() |
| 106 | + if exists { |
| 107 | + setting.Value = defaultZone.String() |
| 108 | + } |
| 109 | + switch { |
| 110 | + // Environment variable check |
| 111 | + case os.Getenv(scw.ScwDefaultZoneEnv) != "": |
| 112 | + setting.Origin = fmt.Sprintf("env (%s)", scw.ScwDefaultZoneEnv) |
| 113 | + // There is no config file |
| 114 | + case config == nil: |
| 115 | + setting.Origin = "default" |
| 116 | + // Config file with profile name |
| 117 | + case config.Profiles[profileName] != nil && config.Profiles[profileName].DefaultZone != nil: |
| 118 | + setting.Origin = fmt.Sprintf("profile (%s)", profileName) |
| 119 | + // Default config |
| 120 | + case config.Profile.DefaultZone != nil: |
| 121 | + setting.Origin = "default profile" |
| 122 | + default: |
| 123 | + setting.Origin = "default" |
| 124 | + } |
| 125 | + return setting |
| 126 | +} |
| 127 | + |
| 128 | +func defaultOrganizationId(ctx context.Context, config *scw.Config, profileName string) *setting { |
| 129 | + setting := &setting{Key: "default_organization_id"} |
| 130 | + client := core.ExtractClient(ctx) |
| 131 | + defaultOrganizationId, exists := client.GetDefaultOrganizationID() |
| 132 | + if exists { |
| 133 | + setting.Value = defaultOrganizationId |
| 134 | + } |
| 135 | + switch { |
| 136 | + // Environment variable check |
| 137 | + case os.Getenv(scw.ScwDefaultOrganizationIDEnv) != "": |
| 138 | + setting.Origin = fmt.Sprintf("env (%s)", scw.ScwDefaultOrganizationIDEnv) |
| 139 | + // There is no config file |
| 140 | + case config == nil: |
| 141 | + setting.Origin = "" |
| 142 | + // Config file with profile name |
| 143 | + case config.Profiles[profileName] != nil && config.Profiles[profileName].DefaultOrganizationID != nil: |
| 144 | + setting.Origin = fmt.Sprintf("profile (%s)", profileName) |
| 145 | + // Default config |
| 146 | + case config.Profile.DefaultOrganizationID != nil: |
| 147 | + setting.Origin = "default profile" |
| 148 | + default: |
| 149 | + setting.Origin = "unknown" |
| 150 | + } |
| 151 | + return setting |
| 152 | +} |
| 153 | + |
| 154 | +func accessKey(ctx context.Context, config *scw.Config, profileName string) *setting { |
| 155 | + setting := &setting{Key: "access_key"} |
| 156 | + client := core.ExtractClient(ctx) |
| 157 | + aK, exists := client.GetAccessKey() |
| 158 | + if exists { |
| 159 | + setting.Value = aK |
| 160 | + } |
| 161 | + switch { |
| 162 | + // Environment variable check |
| 163 | + case os.Getenv(scw.ScwAccessKeyEnv) != "": |
| 164 | + setting.Origin = fmt.Sprintf("env (%s)", scw.ScwAccessKeyEnv) |
| 165 | + // There is no config file |
| 166 | + case config == nil: |
| 167 | + setting.Origin = "" |
| 168 | + // Config file with profile name |
| 169 | + case config.Profiles[profileName] != nil && config.Profiles[profileName].AccessKey != nil: |
| 170 | + setting.Origin = fmt.Sprintf("profile (%s)", profileName) |
| 171 | + // Default config |
| 172 | + case config.Profile.AccessKey != nil: |
| 173 | + setting.Origin = "default profile" |
| 174 | + default: |
| 175 | + setting.Origin = "unknown" |
| 176 | + } |
| 177 | + return setting |
| 178 | +} |
| 179 | + |
| 180 | +func hideSecretKey(k string) string { |
| 181 | + switch { |
| 182 | + case len(k) == 0: |
| 183 | + return "" |
| 184 | + case len(k) > 8: |
| 185 | + return k[0:8] + "-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 186 | + default: |
| 187 | + return "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +func secretKey(ctx context.Context, config *scw.Config, profileName string, showSecret bool) *setting { |
| 192 | + setting := &setting{Key: "secret_key"} |
| 193 | + client := core.ExtractClient(ctx) |
| 194 | + sK, exists := client.GetSecretKey() |
| 195 | + if exists { |
| 196 | + if showSecret { |
| 197 | + setting.Value = sK |
| 198 | + } else { |
| 199 | + setting.Value = hideSecretKey(sK) |
| 200 | + } |
| 201 | + } |
| 202 | + switch { |
| 203 | + // Environment variable check |
| 204 | + case os.Getenv(scw.ScwSecretKeyEnv) != "": |
| 205 | + setting.Origin = fmt.Sprintf("env (%s)", scw.ScwSecretKeyEnv) |
| 206 | + // There is no config file |
| 207 | + case config == nil: |
| 208 | + setting.Origin = "" |
| 209 | + // Config file with profile name |
| 210 | + case config.Profiles[profileName] != nil && config.Profiles[profileName].SecretKey != nil: |
| 211 | + setting.Origin = fmt.Sprintf("profile (%s)", profileName) |
| 212 | + // Default config |
| 213 | + case config.Profile.SecretKey != nil: |
| 214 | + setting.Origin = "default profile" |
| 215 | + default: |
| 216 | + setting.Origin = "unknown" |
| 217 | + } |
| 218 | + |
| 219 | + return setting |
| 220 | +} |
| 221 | + |
| 222 | +func profile(ctx context.Context) *setting { |
| 223 | + setting := &setting{ |
| 224 | + Key: "profile", |
| 225 | + Value: core.ExtractProfileName(ctx), |
| 226 | + } |
| 227 | + switch { |
| 228 | + case core.ExtractProfileFlag(ctx) != "": |
| 229 | + setting.Origin = "flag --profile/-p" |
| 230 | + case os.Getenv(scw.ScwActiveProfileEnv) != "": |
| 231 | + setting.Origin = fmt.Sprintf("env (%s)", scw.ScwActiveProfileEnv) |
| 232 | + default: |
| 233 | + setting.Origin = "" |
| 234 | + } |
| 235 | + return setting |
| 236 | +} |
0 commit comments