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

Netlify Shell Plugin #330

Closed
Closed
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
25 changes: 25 additions & 0 deletions plugins/netlify/netlify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package netlify

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 NetlifyCLI() schema.Executable {
return schema.Executable{
Name: "Netlify CLI",
Runs: []string{"netlify"},
DocsURL: sdk.URL("https://docs.netlify.com/cli/get-started/"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
),
Uses: []schema.CredentialUsage{
{
Name: credname.PersonalAPIToken,
},
},
}
}
68 changes: 68 additions & 0 deletions plugins/netlify/personal_api_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package netlify

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 PersonalAPIToken() schema.CredentialType {
return schema.CredentialType{
Name: credname.PersonalAPIToken,
DocsURL: sdk.URL("https://docs.netlify.com/cli/get-started/#obtain-a-token-in-the-netlify-ui"),
ManagementURL: sdk.URL("https://app.netlify.com/USER/applications"),
Fields: []schema.CredentialField{
{
Name: fieldname.Token,
MarkdownDescription: "Token used to authenticate to Netlify.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 13,
Charset: schema.Charset{
Lowercase: true,
},
},
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryNetlifyConfigFile(),
)}
}

var defaultEnvVarMapping = map[string]sdk.FieldName{
"NETLIFY_AUTH_TOKEN": fieldname.Token, // TODO: Check if this is correct
}

// TODO: Check if the platform stores the Personal API Token in a local config file, and if so,
// implement the function below to add support for importing it.
func TryNetlifyConfigFile() sdk.Importer {
return importer.TryFile("~/path/to/config/file.yml", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
// var config Config
// if err := contents.ToYAML(&config); err != nil {
// out.AddError(err)
// return
// }

// if config.Token == "" {
// return
// }

// out.AddCandidate(sdk.ImportCandidate{
// Fields: map[sdk.FieldName]string{
// fieldname.Token: config.Token,
// },
// })
})
}

// TODO: Implement the config file schema
// type Config struct {
// Token string
// }
55 changes: 55 additions & 0 deletions plugins/netlify/personal_api_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package netlify

import (
"testing"

"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/plugintest"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
)

func TestPersonalAPITokenProvisioner(t *testing.T) {
plugintest.TestProvisioner(t, PersonalAPIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{
"default": {
ItemFields: map[sdk.FieldName]string{ // TODO: Check if this is correct
fieldname.Token: "qaywqkexample",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"NETLIFY_AUTH_TOKEN": "qaywqkexample",
},
},
},
})
}

func TestPersonalAPITokenImporter(t *testing.T) {
plugintest.TestImporter(t, PersonalAPIToken().Importer, map[string]plugintest.ImportCase{
"environment": {
Environment: map[string]string{ // TODO: Check if this is correct
"NETLIFY_AUTH_TOKEN": "qaywqkexample",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "qaywqkexample",
},
},
},
},
// TODO: If you implemented a config file importer, add a test file example in netlify/test-fixtures
// and fill the necessary details in the test template below.
"config file": {
Files: map[string]string{
// "~/path/to/config.yml": plugintest.LoadFixture(t, "config.yml"),
},
ExpectedCandidates: []sdk.ImportCandidate{
// {
// Fields: map[sdk.FieldName]string{
// fieldname.Token: "qaywqkexample",
// },
// },
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/netlify/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package netlify

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

func New() schema.Plugin {
return schema.Plugin{
Name: "netlify",
Platform: schema.PlatformInfo{
Name: "Netlify",
Homepage: sdk.URL("https://app.netlify.com/"),
},
Credentials: []schema.CredentialType{
PersonalAPIToken(),
},
Executables: []schema.Executable{
NetlifyCLI(),
},
}
}