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 #333

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also skip authentication for login and logout?

needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
needsauth.NotForExactArgs("config"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sure I see a config subcommand for netlify. Is there a reason why this is listed? Not seeing it as a command line flag either. 🤔

),
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-",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like tGtp- is an actual prefix. I have an entirely different token. Could we remove this?

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The location varies by the operating system:

https://docs.netlify.com/cli/get-started/#config-json-location

Could we update the importer to support OS-specific locations? importer.MacOnly and importer.LinuxOnly can help, there are some references of them in ngrok shell plugin:

importer.MacOnly(
TryngrokConfigFile("~/Library/Application Support/ngrok/ngrok.yml"),
),
importer.LinuxOnly(
TryngrokConfigFile("~/.config/ngrok/ngrok.yml"),
),

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this TODO.

fieldname.Token: "tGtp-IMFGyRcoLdK40zQ4ENKfvDeIOASs1ilEXAMPLE",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"NETLIFY_TOKEN": "tGtp-IMFGyRcoLdK40zQ4ENKfvDeIOASs1ilEXAMPLE",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After removing the prefix (addressed on another comment above), could we run make netlify/example-secrets again and use the new example secret?

},
},
},
})
}

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this todo and make sure to add a sample file to netlify/test-fixtures folder. ngrok's test-fixtures is a good example again, but we'll use a json file for Netlify.

// and fill the necessary details in the test template below.
"config file": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the config file importer is not completed yet. Can we help you with anything here, @joqim?

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(),
},
}
}