From a23c53a99c63cea4e9c775d030100d03587324f8 Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Thu, 25 May 2023 09:54:43 +0100 Subject: [PATCH] feat: add Oh Dear CLI --- plugins/ohdear/api_token.go | 40 +++++++++++++++++++++++++++++++ plugins/ohdear/api_token_test.go | 41 ++++++++++++++++++++++++++++++++ plugins/ohdear/ohdear.go | 25 +++++++++++++++++++ plugins/ohdear/plugin.go | 22 +++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 plugins/ohdear/api_token.go create mode 100644 plugins/ohdear/api_token_test.go create mode 100644 plugins/ohdear/ohdear.go create mode 100644 plugins/ohdear/plugin.go diff --git a/plugins/ohdear/api_token.go b/plugins/ohdear/api_token.go new file mode 100644 index 000000000..4a3c3626b --- /dev/null +++ b/plugins/ohdear/api_token.go @@ -0,0 +1,40 @@ +package ohdear + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/importer" + "github.com/1Password/shell-plugins/sdk/provision" + "github.com/1Password/shell-plugins/sdk/schema" + "github.com/1Password/shell-plugins/sdk/schema/credname" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +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"), + ManagementURL: sdk.URL("https://ohdear.app/user/api-tokens"), + Fields: []schema.CredentialField{ + { + Name: fieldname.Token, + MarkdownDescription: "Token used to authenticate to Oh Dear.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 40, + Charset: schema.Charset{ + Uppercase: true, + Lowercase: true, + Digits: true, + }, + }, + }, + }, + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryAll( + importer.TryEnvVarPair(defaultEnvVarMapping), + )} +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "OHDEAR_API_TOKEN": fieldname.Token, +} diff --git a/plugins/ohdear/api_token_test.go b/plugins/ohdear/api_token_test.go new file mode 100644 index 000000000..bc104c248 --- /dev/null +++ b/plugins/ohdear/api_token_test.go @@ -0,0 +1,41 @@ +package ohdear + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestAPITokenProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + fieldname.Token: "SZ5rluwzbtMyyQFQNoeqEFbpVbTL0ItsXEXAMPLE", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "OHDEAR_API_TOKEN": "SZ5rluwzbtMyyQFQNoeqEFbpVbTL0ItsXEXAMPLE", + }, + }, + }, + }) +} + +func TestAPITokenImporter(t *testing.T) { + plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ + "environment": { + Environment: map[string]string{ + "OHDEAR_API_TOKEN": "SZ5rluwzbtMyyQFQNoeqEFbpVbTL0ItsXEXAMPLE", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "SZ5rluwzbtMyyQFQNoeqEFbpVbTL0ItsXEXAMPLE", + }, + }, + }, + }, + }) +} diff --git a/plugins/ohdear/ohdear.go b/plugins/ohdear/ohdear.go new file mode 100644 index 000000000..839ce2425 --- /dev/null +++ b/plugins/ohdear/ohdear.go @@ -0,0 +1,25 @@ +package ohdear + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/needsauth" + "github.com/1Password/shell-plugins/sdk/schema" + "github.com/1Password/shell-plugins/sdk/schema/credname" +) + +func OhDearCLI() schema.Executable { + return schema.Executable{ + Name: "Oh Dear CLI", + Runs: []string{"ohdear"}, + DocsURL: sdk.URL("https://ohdear.app/docs/integrations/our-cli-tool"), + NeedsAuth: needsauth.IfAll( + needsauth.NotForHelpOrVersion(), + needsauth.NotWithoutArgs(), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.APIToken, + }, + }, + } +} diff --git a/plugins/ohdear/plugin.go b/plugins/ohdear/plugin.go new file mode 100644 index 000000000..f7442a254 --- /dev/null +++ b/plugins/ohdear/plugin.go @@ -0,0 +1,22 @@ +package ohdear + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "ohdear", + Platform: schema.PlatformInfo{ + Name: "Oh Dear", + Homepage: sdk.URL("https://ohdear.app"), + }, + Credentials: []schema.CredentialType{ + APIToken(), + }, + Executables: []schema.Executable{ + OhDearCLI(), + }, + } +}