|
| 1 | +package civo |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + |
| 7 | + "github.com/1Password/shell-plugins/sdk" |
| 8 | + "github.com/1Password/shell-plugins/sdk/importer" |
| 9 | + "github.com/1Password/shell-plugins/sdk/provision" |
| 10 | + "github.com/1Password/shell-plugins/sdk/schema" |
| 11 | + "github.com/1Password/shell-plugins/sdk/schema/credname" |
| 12 | + "github.com/1Password/shell-plugins/sdk/schema/fieldname" |
| 13 | +) |
| 14 | + |
| 15 | +func APIKey() schema.CredentialType { |
| 16 | + return schema.CredentialType{ |
| 17 | + Name: credname.APIKey, |
| 18 | + DocsURL: sdk.URL("https://www.civo.com/docs/account/api-keys"), |
| 19 | + ManagementURL: sdk.URL("https://dashboard.civo.com/security"), |
| 20 | + Fields: []schema.CredentialField{ |
| 21 | + { |
| 22 | + Name: fieldname.APIKey, |
| 23 | + MarkdownDescription: "API Key used to authenticate to Civo.", |
| 24 | + Secret: true, |
| 25 | + Composition: &schema.ValueComposition{ |
| 26 | + Length: 50, |
| 27 | + Charset: schema.Charset{ |
| 28 | + Uppercase: true, |
| 29 | + Lowercase: true, |
| 30 | + Digits: true, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + Name: fieldname.APIKeyID, |
| 36 | + MarkdownDescription: "API Name to identify the API Key.", |
| 37 | + }, |
| 38 | + { |
| 39 | + Name: fieldname.DefaultRegion, |
| 40 | + MarkdownDescription: "The default region to use for this API Key.", |
| 41 | + Optional: true, |
| 42 | + }, |
| 43 | + }, |
| 44 | + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), |
| 45 | + Importer: importer.TryAll( |
| 46 | + importer.TryEnvVarPair(defaultEnvVarMapping), |
| 47 | + TryCivoConfigFile(), |
| 48 | + )} |
| 49 | +} |
| 50 | + |
| 51 | +var defaultEnvVarMapping = map[string]sdk.FieldName{ |
| 52 | + "CIVO_API_KEY_NAME": fieldname.APIKeyID, |
| 53 | + "CIVO_API_KEY": fieldname.APIKey, |
| 54 | +} |
| 55 | + |
| 56 | +func TryCivoConfigFile() sdk.Importer { |
| 57 | + |
| 58 | + return importer.TryFile("~/.civo.json", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { |
| 59 | + var config Config |
| 60 | + if err := contents.ToJSON(&config); err != nil { |
| 61 | + out.AddError(err) |
| 62 | + return |
| 63 | + |
| 64 | + } |
| 65 | + if len(config.Properties) == 0 && config.Meta.CurrentAPIKey == "" { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + var apiKey string |
| 70 | + for key, value := range config.Properties { |
| 71 | + if key == config.Meta.CurrentAPIKey { |
| 72 | + err := json.Unmarshal(value, &apiKey) |
| 73 | + if err != nil { |
| 74 | + out.AddError(err) |
| 75 | + return |
| 76 | + } |
| 77 | + } |
| 78 | + break |
| 79 | + } |
| 80 | + |
| 81 | + out.AddCandidate(sdk.ImportCandidate{ |
| 82 | + Fields: map[sdk.FieldName]string{ |
| 83 | + fieldname.APIKey: apiKey, |
| 84 | + fieldname.APIKeyID: config.Meta.CurrentAPIKey, |
| 85 | + fieldname.DefaultRegion: config.Meta.DefaultRegion, |
| 86 | + }, |
| 87 | + }) |
| 88 | + |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +// { |
| 93 | +// "apikeys": { |
| 94 | +// "newspidey": "Vdi1GHFqXLG47VcfdvfvfvfvfvfvfvfvEgOd", |
| 95 | +// }, |
| 96 | +// "meta": { |
| 97 | +// "admin": false, |
| 98 | +// "current_apikey": "newspidey", |
| 99 | +// "default_region": "LON1", |
| 100 | +// "latest_release_check": "2023-06-11T20:25:06.916682112+05:30", |
| 101 | +// "url": "https://api.civo.com", |
| 102 | +// "last_command_executed": "2023-06-11T20:25:06.916237569+05:30" |
| 103 | +// } |
| 104 | +// } |
| 105 | + |
| 106 | +type Config struct { |
| 107 | + Properties map[string]json.RawMessage `json:"apikeys"` |
| 108 | + |
| 109 | + Meta struct { |
| 110 | + Admin bool `json:"admin"` |
| 111 | + CurrentAPIKey string `json:"current_apikey"` |
| 112 | + DefaultRegion string `json:"default_region"` |
| 113 | + LatestReleaseCheck string `json:"latest_release_check"` |
| 114 | + URL string `json:"url"` |
| 115 | + LastCommandExecuted string `json:"last_command_executed"` |
| 116 | + } `json:"meta"` |
| 117 | +} |
0 commit comments