-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve fix tests improve fix test Update docs url Co-authored-by: Floris van der Grinten <floris@grinten.com> not need auth if providing one
- Loading branch information
Showing
5 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"token": "tZk79pLyPLGgUVlkHbnLeXgl" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
needsauth.NotWhenContainsArgs("login"), | ||
needsauth.NotWhenContainsArgs("-t"), | ||
needsauth.NotWhenContainsArgs("--token"), | ||
), | ||
Uses: []schema.CredentialUsage{ | ||
{ | ||
Name: credname.APIToken, | ||
}, | ||
}, | ||
} | ||
} |