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

feat: add vercel plugin #273

Merged
merged 1 commit into from
Jun 22, 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/vercel/api_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package vercel

import (
"context"

"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/importer"
"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://vercel.com/docs/rest-api#authentication"),
ManagementURL: sdk.URL("https://vercel.com/account/tokens"),
Fields: []schema.CredentialField{
{
Name: fieldname.Token,
MarkdownDescription: "Token used to authenticate to Vercel.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 24,
Charset: schema.Charset{
Lowercase: true,
Uppercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: vercelProvisioner{},
Importer: importer.TryAll(
importer.MacOnly(TryVercelConfigFile("~/Library/Application Support/com.vercel.cli/auth.json")),
importer.LinuxOnly(TryVercelConfigFile("~/.config/com.vercel.cli/auth.json")),
),
}
}

type vercelProvisioner struct{}

func (v vercelProvisioner) Description() string {
return "Vercel cli token provisioner"
}

func (v vercelProvisioner) Provision(ctx context.Context, input sdk.ProvisionInput, output *sdk.ProvisionOutput) {
hculea marked this conversation as resolved.
Show resolved Hide resolved
output.AddArgs("--token", input.ItemFields[fieldname.Token])
}

func (v vercelProvisioner) Deprovision(ctx context.Context, input sdk.DeprovisionInput, output *sdk.DeprovisionOutput) {
// No-op
}

func TryVercelConfigFile(path string) sdk.Importer {
return importer.TryFile(
path,
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"`
}
57 changes: 57 additions & 0 deletions plugins/vercel/api_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package vercel

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: "tZk79pLyPLGgUVlkHbnLeXgl",
},
ExpectedOutput: sdk.ProvisionOutput{
CommandLine: []string{"--token", "tZk79pLyPLGgUVlkHbnLeXgl"},
},
},
},
)
}

func TestAPITokenImporter(t *testing.T) {
plugintest.TestImporter(
t, APIToken().Importer, map[string]plugintest.ImportCase{
"config file (macOS)": {
OS: "darwin",
Files: map[string]string{
"~/Library/Application Support/com.vercel.cli/auth.json": plugintest.LoadFixture(t, "auth.json"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "tZk79pLyPLGgUVlkHbnLeXgl",
},
},
},
},
"config file (Linux)": {
OS: "linux",
Files: map[string]string{
"~/.config/com.vercel.cli/auth.json": plugintest.LoadFixture(t, "auth.json"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "tZk79pLyPLGgUVlkHbnLeXgl",
},
},
},
},
},
)
}
22 changes: 22 additions & 0 deletions plugins/vercel/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package vercel

import (
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/schema"
)

func New() schema.Plugin {
return schema.Plugin{
Name: "vercel",
Platform: schema.PlatformInfo{
Name: "Vercel",
Homepage: sdk.URL("https://vercel.com"),
},
Credentials: []schema.CredentialType{
APIToken(),
},
Executables: []schema.Executable{
VercelCLI(),
},
}
}
3 changes: 3 additions & 0 deletions plugins/vercel/test-fixtures/auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"token": "tZk79pLyPLGgUVlkHbnLeXgl"
}
28 changes: 28 additions & 0 deletions plugins/vercel/vercel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package vercel

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 VercelCLI() schema.Executable {
return schema.Executable{
Name: "Vercel CLI",
Runs: []string{"vercel"},
DocsURL: sdk.URL("https://vercel.com/docs/cli"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
j178 marked this conversation as resolved.
Show resolved Hide resolved
needsauth.NotWhenContainsArgs("login"),
needsauth.NotWhenContainsArgs("-t"),
needsauth.NotWhenContainsArgs("--token"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIToken,
},
},
}
}