Skip to content

Commit

Permalink
introduce netlify plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
joqim committed Jun 29, 2023
1 parent a39d64a commit 25791ad
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
26 changes: 26 additions & 0 deletions plugins/netlify/netlify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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://netlify.com/docs/cli"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
needsauth.NotForExactArgs("config"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.PersonalAccessToken,
},
},
}
}
87 changes: 87 additions & 0 deletions plugins/netlify/personal_access_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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 PersonalAccessToken() schema.CredentialType {
return schema.CredentialType{
Name: credname.PersonalAccessToken,
DocsURL: sdk.URL("https://docs.netlify.com/cli/get-started/#authentication"),
ManagementURL: sdk.URL("https://app.netlify.com/user/applications#personal-access-tokens"),
Fields: []schema.CredentialField{
{
Name: fieldname.Token,
MarkdownDescription: "Token used to authenticate to Netlify.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 43,
Prefix: "tGtp-",
Charset: schema.Charset{
Uppercase: true,
Lowercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryNetlifyConfigFile(),
)}
}

var defaultEnvVarMapping = map[string]sdk.FieldName {
"NETLIFY_AUTH_TOKEN": fieldname.Token,
}

func TryNetlifyConfigFile() sdk.Importer {
return importer.TryFile("~/Library/Preferences/netlify/config.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.Users != nil {
for _, user := range config.Users {
if user.Auth != nil && user.Auth.Token != "" {
out.AddCandidate(sdk.ImportCandidate{
Fields: map[sdk.FieldName]string{
fieldname.Token: user.Auth.Token,
},
})
}
}
}
})
}

type Config struct {
TelemetryDisabled bool `json:"telemetryDisabled"`
CliID string `json:"cliId"`
UserID string `json:"userId"`
Users map[string]UserInfo `json:"users"`
}

// UserInfo represents the user information in the config file
type UserInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Auth *UserAuthInfo `json:"auth"`
}

// UserAuthInfo represents the authentication information of a user
type UserAuthInfo struct {
Token string `json:"token"`
Github struct{} `json:"github"` // Empty struct for placeholder, you can add additional fields if needed
}
54 changes: 54 additions & 0 deletions plugins/netlify/personal_access_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 TestPersonalAccessTokenProvisioner(t *testing.T) {
plugintest.TestProvisioner(t, PersonalAccessToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{
"default": {
ItemFields: map[sdk.FieldName]string{ // TODO: Check if this is correct
fieldname.Token: "tGtp-IMFGyRcoLdK40zQ4ENKfvDeIOASs1ilEXAMPLE",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"NETLIFY_TOKEN": "tGtp-IMFGyRcoLdK40zQ4ENKfvDeIOASs1ilEXAMPLE",
},
},
},
})
}

func TestPersonalAccessTokenImporter(t *testing.T) {
plugintest.TestImporter(t, PersonalAccessToken().Importer, map[string]plugintest.ImportCase{
"environment": {
Environment: map[string]string{ // TODO: Check if this is correct
"NETLIFY_TOKEN": "tGtp-IMFGyRcoLdK40zQ4ENKfvDeIOASs1ilEXAMPLE",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "tGtp-IMFGyRcoLdK40zQ4ENKfvDeIOASs1ilEXAMPLE",
},
},
},
},
// 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: "tGtp-IMFGyRcoLdK40zQ4ENKfvDeIOASs1ilEXAMPLE",
// },
// },
},
},
})
}
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://netlify.com"),
},
Credentials: []schema.CredentialType{
PersonalAccessToken(),
},
Executables: []schema.Executable{
NetlifyCLI(),
},
}
}

0 comments on commit 25791ad

Please sign in to comment.