diff --git a/plugins/ohdear/api_token.go b/plugins/ohdear/api_token.go index 4a3c3626b..6ba8748af 100644 --- a/plugins/ohdear/api_token.go +++ b/plugins/ohdear/api_token.go @@ -1,6 +1,9 @@ package ohdear import ( + "context" + "encoding/json" + "github.com/1Password/shell-plugins/sdk" "github.com/1Password/shell-plugins/sdk/importer" "github.com/1Password/shell-plugins/sdk/provision" @@ -12,7 +15,7 @@ import ( func APIToken() schema.CredentialType { return schema.CredentialType{ Name: credname.APIToken, - DocsURL: sdk.URL("https://ohdear.app/docs/integrations/the-oh-dear-api#get-your-api-token"), + DocsURL: sdk.URL("https://ohdear.app/docs/api/introduction#get-your-api-token"), ManagementURL: sdk.URL("https://ohdear.app/user/api-tokens"), Fields: []schema.CredentialField{ { @@ -29,12 +32,53 @@ func APIToken() schema.CredentialType { }, }, }, - DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + DefaultProvisioner: provision.TempFile(configFile, + provision.Filename("config.json"), + provision.SetPathAsEnvVar("OHDEAR_CONFIG_FILE_PATH"), + ), Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), + TryOhdearConfigFile(), )} } var defaultEnvVarMapping = map[string]sdk.FieldName{ "OHDEAR_API_TOKEN": fieldname.Token, } + +func configFile(in sdk.ProvisionInput) ([]byte, error) { + config := Config{ + Token: in.ItemFields[fieldname.Token], + } + + contents, err := json.Marshal(config) + if err != nil { + return nil, err + } + + return []byte(contents), nil +} + +func TryOhdearConfigFile() sdk.Importer { + return importer.TryFile("~/.ohdear/config.json", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { + var config Config + if err := contents.ToJSON(&config); err != nil { + out.AddError(err) + return + } + + if config.Token == "" { + return + } + + out.AddCandidate(sdk.ImportCandidate{ + Fields: map[sdk.FieldName]string{ + fieldname.Token: config.Token, + }, + }) + }) +} + +type Config struct { + Token string `json:"token"` +} diff --git a/plugins/ohdear/api_token_test.go b/plugins/ohdear/api_token_test.go index bc104c248..7dd047003 100644 --- a/plugins/ohdear/api_token_test.go +++ b/plugins/ohdear/api_token_test.go @@ -16,7 +16,12 @@ func TestAPITokenProvisioner(t *testing.T) { }, ExpectedOutput: sdk.ProvisionOutput{ Environment: map[string]string{ - "OHDEAR_API_TOKEN": "SZ5rluwzbtMyyQFQNoeqEFbpVbTL0ItsXEXAMPLE", + "OHDEAR_CONFIG_FILE_PATH": "/tmp/config.json", + }, + Files: map[string]sdk.OutputFile{ + "/tmp/config.json": { + Contents: []byte(plugintest.LoadFixture(t, "import_or_provision")), + }, }, }, }, @@ -24,6 +29,20 @@ func TestAPITokenProvisioner(t *testing.T) { } func TestAPITokenImporter(t *testing.T) { + plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ + "config file": { + Files: map[string]string{ + "~/.ohdear/config.json": plugintest.LoadFixture(t, "import_or_provision"), + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "SZ5rluwzbtMyyQFQNoeqEFbpVbTL0ItsXEXAMPLE", + }, + }, + }, + }, + }) plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ "environment": { Environment: map[string]string{ diff --git a/plugins/ohdear/test-fixtures/import_or_provision b/plugins/ohdear/test-fixtures/import_or_provision new file mode 100644 index 000000000..ced421b25 --- /dev/null +++ b/plugins/ohdear/test-fixtures/import_or_provision @@ -0,0 +1 @@ +{"token":"SZ5rluwzbtMyyQFQNoeqEFbpVbTL0ItsXEXAMPLE"} \ No newline at end of file