diff --git a/plugins/civo/api_key.go b/plugins/civo/api_key.go index 82651f5ec..f9791552f 100644 --- a/plugins/civo/api_key.go +++ b/plugins/civo/api_key.go @@ -3,7 +3,6 @@ package civo import ( "context" "encoding/json" - "os" "github.com/1Password/shell-plugins/sdk" "github.com/1Password/shell-plugins/sdk/importer" @@ -11,7 +10,6 @@ import ( "github.com/1Password/shell-plugins/sdk/schema" "github.com/1Password/shell-plugins/sdk/schema/credname" "github.com/1Password/shell-plugins/sdk/schema/fieldname" - "github.com/mitchellh/go-homedir" ) func APIKey() schema.CredentialType { @@ -43,114 +41,15 @@ func APIKey() schema.CredentialType { Optional: true, }, }, - // DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), - DefaultProvisioner: provision.TempFile(configFile, - provision.Filename(".civo.json"), - provision.AddArgs( - "--config", "{{.Path}}", - ), - ), + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), TryCivoConfigFile(), )} } -func configFile(in sdk.ProvisionInput) ([]byte, error) { - apiKey := in.ItemFields[fieldname.APIKey] - apiKeyID := in.ItemFields[fieldname.APIKeyID] - defaultRegion := in.ItemFields[fieldname.DefaultRegion] - - // Check if the file already exists - // filePath := "~/.civo.json" - // exists, err := fileExists(filePath) - // if err != nil { - // return nil, err - // } - - // if exists { - // // Read the existing file - // contents, err := ioutil.ReadFile(filePath) - // if err != nil { - // return nil, err - // } - - // var config Config - // if err := json.Unmarshal(contents, &config); err != nil { - // return nil, err - // } - - // // Update the config with the new values - // config.Properties[apiKeyID] = json.RawMessage(`"` + apiKey + `"`) - // config.Meta.CurrentAPIKey = apiKeyID - // config.Meta.DefaultRegion = defaultRegion - - // return json.MarshalIndent(config, "", " ") - // } - - // Create a new config - config := Config{ - Properties: map[string]json.RawMessage{ - apiKeyID: json.RawMessage(`"` + apiKey + `"`), - }, - Meta: struct { - Admin bool `json:"admin"` - CurrentAPIKey string `json:"current_apikey"` - DefaultRegion string `json:"default_region"` - LatestReleaseCheck string `json:"latest_release_check"` - URL string `json:"url"` - LastCommandExecuted string `json:"last_command_executed"` - }{ - CurrentAPIKey: apiKeyID, - DefaultRegion: defaultRegion, - }, - } - - return json.MarshalIndent(config, "", " ") -} - -func fileExists(filePath string) (bool, error) { - expandedPath, err := homedir.Expand(filePath) - if err != nil { - return false, err - } - - info, err := os.Stat(expandedPath) - if os.IsNotExist(err) { - return false, nil - } - if err != nil { - return false, err - } - - return !info.IsDir(), nil -} - -// func configFile(in sdk.ProvisionInput) ([]byte, error) { -// apiKey := in.ItemFields[fieldname.APIKey] -// apiKeyID := in.ItemFields[fieldname.APIKeyID] -// defaultRegion := in.ItemFields[fieldname.DefaultRegion] - -// config := Config{ -// Properties: map[string]json.RawMessage{ -// apiKeyID: json.RawMessage(`"` + apiKey + `"`), -// }, -// Meta: struct { -// Admin bool `json:"admin"` -// CurrentAPIKey string `json:"current_apikey"` -// DefaultRegion string `json:"default_region"` -// LatestReleaseCheck string `json:"latest_release_check"` -// URL string `json:"url"` -// LastCommandExecuted string `json:"last_command_executed"` -// }{ -// CurrentAPIKey: apiKeyID, -// DefaultRegion: defaultRegion, -// }, -// } - -// return json.MarshalIndent(config, "", " ") -// } var defaultEnvVarMapping = map[string]sdk.FieldName{ + "CIVO_TOKEN": fieldname.APIKey, "CIVO_API_KEY_NAME": fieldname.APIKeyID, "CIVO_API_KEY": fieldname.APIKey, } @@ -169,26 +68,26 @@ func TryCivoConfigFile() sdk.Importer { return } - var apiKey string for key, value := range config.Properties { - if key == config.Meta.CurrentAPIKey { - err := json.Unmarshal(value, &apiKey) - if err != nil { - out.AddError(err) - return - } + var apiKey string + + err := json.Unmarshal(value, &apiKey) + if err != nil { + out.AddError(err) + return } + out.AddCandidate(sdk.ImportCandidate{ + NameHint: key, + Fields: map[sdk.FieldName]string{ + fieldname.APIKey: apiKey, + fieldname.APIKeyID: config.Meta.CurrentAPIKey, + fieldname.DefaultRegion: config.Meta.DefaultRegion, + }, + }) + break } - out.AddCandidate(sdk.ImportCandidate{ - Fields: map[sdk.FieldName]string{ - fieldname.APIKey: apiKey, - fieldname.APIKeyID: config.Meta.CurrentAPIKey, - fieldname.DefaultRegion: config.Meta.DefaultRegion, - }, - }) - }) } @@ -196,11 +95,7 @@ type Config struct { Properties map[string]json.RawMessage `json:"apikeys"` Meta struct { - Admin bool `json:"admin"` - CurrentAPIKey string `json:"current_apikey"` - DefaultRegion string `json:"default_region"` - LatestReleaseCheck string `json:"latest_release_check"` - URL string `json:"url"` - LastCommandExecuted string `json:"last_command_executed"` + CurrentAPIKey string `json:"current_apikey"` + DefaultRegion string `json:"default_region"` } `json:"meta"` } diff --git a/plugins/civo/api_key_test.go b/plugins/civo/api_key_test.go index 453a75c3c..a28cf5ae9 100644 --- a/plugins/civo/api_key_test.go +++ b/plugins/civo/api_key_test.go @@ -17,6 +17,7 @@ func TestAPIKeyProvisioner(t *testing.T) { }, ExpectedOutput: sdk.ProvisionOutput{ Environment: map[string]string{ + "CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", "CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", "CIVO_API_KEY_NAME": "testdemoname", }, @@ -29,6 +30,7 @@ func TestAPIKeyImporter(t *testing.T) { plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{ "environment": { Environment: map[string]string{ + "CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", "CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", "CIVO_API_KEY_NAME": "testdemoname", }, diff --git a/plugins/civo/test-fixtures/.civo.json b/plugins/civo/test-fixtures/.civo.json new file mode 100644 index 000000000..2790389b8 --- /dev/null +++ b/plugins/civo/test-fixtures/.civo.json @@ -0,0 +1,11 @@ +{ + "apikeys": { + "newspidey": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE" + }, + "meta": { + + "current_apikey": "newspidey1", + "default_region": "LON1" + + } + } \ No newline at end of file