From 7861adff33b6fd2182796a211ea791967593d6be Mon Sep 17 00:00:00 2001 From: siddhikhapare Date: Mon, 26 Jun 2023 22:13:46 +0530 Subject: [PATCH] Upstash Shell Plugin added --- plugins/upstash/api_key.go | 81 ++++++++++++++++++++++ plugins/upstash/api_key_test.go | 59 ++++++++++++++++ plugins/upstash/plugin.go | 22 ++++++ plugins/upstash/test-fixtures/upstash.json | 4 ++ plugins/upstash/upstash.go | 26 +++++++ 5 files changed, 192 insertions(+) create mode 100644 plugins/upstash/api_key.go create mode 100644 plugins/upstash/api_key_test.go create mode 100644 plugins/upstash/plugin.go create mode 100755 plugins/upstash/test-fixtures/upstash.json create mode 100644 plugins/upstash/upstash.go diff --git a/plugins/upstash/api_key.go b/plugins/upstash/api_key.go new file mode 100644 index 000000000..a7eea2c84 --- /dev/null +++ b/plugins/upstash/api_key.go @@ -0,0 +1,81 @@ +package upstash + +import ( + "context" + + "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 APIKey() schema.CredentialType { + return schema.CredentialType{ + Name: credname.APIKey, + DocsURL: sdk.URL("https://docs.upstash.com/redis/account/developerapi#create-an-api-key"), + ManagementURL: sdk.URL("https://console.upstash.com/account/api"), + Fields: []schema.CredentialField{ + { + Name: fieldname.APIKey, + MarkdownDescription: "API Key used to authenticate to Upstash.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 36, + Charset: schema.Charset{ + Lowercase: true, + Digits: true, + }, + }, + }, + { + Name: fieldname.Email, + MarkdownDescription: "Email used to authenticate to Upstash.", + Composition: &schema.ValueComposition{ + Charset: schema.Charset{ + Uppercase: true, + Lowercase: true, + Digits: true, + Symbols: true, + }, + }, + }, + }, + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryAll( + importer.TryEnvVarPair(defaultEnvVarMapping), + TryUpstashConfigFile(), + )} +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "UPSTASH_API_KEY": fieldname.APIKey, + "UPSTASH_EMAIL" : fieldname.Email, +} + +func TryUpstashConfigFile() sdk.Importer { + return importer.TryFile("~/.upstash.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.APIKey == "" { + return + } + + out.AddCandidate(sdk.ImportCandidate{ + Fields: map[sdk.FieldName]string{ + fieldname.APIKey: config.APIKey, + fieldname.Email : config.Email, + }, + }) + }) +} + +type Config struct { + APIKey string `json:"apiKey"` + Email string `json:"email"` +} diff --git a/plugins/upstash/api_key_test.go b/plugins/upstash/api_key_test.go new file mode 100644 index 000000000..a5c25dfc2 --- /dev/null +++ b/plugins/upstash/api_key_test.go @@ -0,0 +1,59 @@ +package upstash + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestAPIKeyProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, APIKey().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + fieldname.APIKey: "d68850db-69f7-rr97jk7wcvshbkuexample", + fieldname.Email : "fakememail12@gmail.com" + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "UPSTASH_API_KEY": "d68850db-69f7-rr97jk7wcvshbkuexample", + "UPSTASH_EMAIL" : "fakememail12@gmail.com" + }, + }, + }, + }) +} + +func TestAPIKeyImporter(t *testing.T) { + plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{ + "environment": { + Environment: map[string]string{ + "UPSTASH_API_KEY": "d68850db-69f7-rr97jk7wcvshbkuexample", + "UPSTASH_EMAIL" : "fakememail12@gmail.com" + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.APIKey: "d68850db-69f7-rr97jk7wcvshbkuexample", + fieldname.Email : "fakememail12@gmail.com" + }, + }, + }, + }, + + "config file": { + Files: map[string]string{ + "~/.upstash.json": plugintest.LoadFixture(t, "upstash.json"), + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "d68850db-69f7-rr97jk7wcvshbkuexample", + fieldname.Email : "fakememail12@gmail.com" + }, + }, + }, + }, + }) +} diff --git a/plugins/upstash/plugin.go b/plugins/upstash/plugin.go new file mode 100644 index 000000000..61dcbca6d --- /dev/null +++ b/plugins/upstash/plugin.go @@ -0,0 +1,22 @@ +package upstash + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "upstash", + Platform: schema.PlatformInfo{ + Name: "Upstash", + Homepage: sdk.URL("https://upstash.com"), + }, + Credentials: []schema.CredentialType{ + APIKey(), + }, + Executables: []schema.Executable{ + UpstashCLI(), + }, + } +} diff --git a/plugins/upstash/test-fixtures/upstash.json b/plugins/upstash/test-fixtures/upstash.json new file mode 100755 index 000000000..5fd59cc0b --- /dev/null +++ b/plugins/upstash/test-fixtures/upstash.json @@ -0,0 +1,4 @@ +{ + "apiKey":"d68850db-69f7-2b87x3f5u7sxinsexample", + "email" : "fakeemail12@gmail.com" +} \ No newline at end of file diff --git a/plugins/upstash/upstash.go b/plugins/upstash/upstash.go new file mode 100644 index 000000000..a16bfba38 --- /dev/null +++ b/plugins/upstash/upstash.go @@ -0,0 +1,26 @@ +package upstash + +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 UpstashCLI() schema.Executable { + return schema.Executable{ + Name: "Upstash CLI", + Runs: []string{"upstash"}, + DocsURL: sdk.URL("https://github.com/upstash/cli"), + NeedsAuth: needsauth.IfAll( + needsauth.NotForHelpOrVersion(), + needsauth.NotWithoutArgs(), + needsauth.NotWhenContainsArgs("auth","login"), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.APIKey, + }, + }, + } +}