diff --git a/plugins/netlify/netlify.go b/plugins/netlify/netlify.go new file mode 100644 index 000000000..e56e4586b --- /dev/null +++ b/plugins/netlify/netlify.go @@ -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, + }, + }, + } +} \ No newline at end of file diff --git a/plugins/netlify/personal_access_token.go b/plugins/netlify/personal_access_token.go new file mode 100644 index 000000000..2dfdc222e --- /dev/null +++ b/plugins/netlify/personal_access_token.go @@ -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 +} \ No newline at end of file diff --git a/plugins/netlify/personal_access_token_test.go b/plugins/netlify/personal_access_token_test.go new file mode 100644 index 000000000..20524b9a8 --- /dev/null +++ b/plugins/netlify/personal_access_token_test.go @@ -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", + // }, + // }, + }, + }, + }) +} \ No newline at end of file diff --git a/plugins/netlify/plugin.go b/plugins/netlify/plugin.go new file mode 100644 index 000000000..d47193b23 --- /dev/null +++ b/plugins/netlify/plugin.go @@ -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(), + }, + } +} \ No newline at end of file