Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upstash shell plugin added #316

Merged
merged 6 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions plugins/upstash/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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{
arunsathiya marked this conversation as resolved.
Show resolved Hide resolved
Lowercase: true,
Digits: true,
Symbols: 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 == "" && config.Email == "" {
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"`
}
59 changes: 59 additions & 0 deletions plugins/upstash/api_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package upstash
arunsathiya marked this conversation as resolved.
Show resolved Hide resolved

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-qxe9pubcmjnqfgyexample",
fieldname.Email: "wendy@appleseed.com",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"UPSTASH_API_KEY": "d68850db-69f7-qxe9pubcmjnqfgyexample",
"UPSTASH_EMAIL": "wendy@appleseed.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-qxe9pubcmjnqfgyexample",
"UPSTASH_EMAIL": "wendy@appleseed.com",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "d68850db-69f7-qxe9pubcmjnqfgyexample",
fieldname.Email: "wendy@appleseed.com",
},
},
},
},

"config file": {
Files: map[string]string{
"~/.upstash.json": plugintest.LoadFixture(t, ".upstash.json"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "d68850db-69f7-qxe9pubcmjnqfgyexample",
fieldname.Email: "wendy@appleseed.com",
},
},
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/upstash/plugin.go
Original file line number Diff line number Diff line change
@@ -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(),
},
}
}
4 changes: 4 additions & 0 deletions plugins/upstash/test-fixtures/.upstash.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"apiKey":"d68850db-69f7-qxe9pubcmjnqfgyexample",
"email" : "wendy@appleseed.com"
}
28 changes: 28 additions & 0 deletions plugins/upstash/upstash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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(
hculea marked this conversation as resolved.
Show resolved Hide resolved
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
needsauth.NotWhenContainsArgs("--config"),
needsauth.NotWhenContainsArgs("-c"),
needsauth.NotWhenContainsArgs("auth"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIKey,
},
},
}
}