-
Notifications
You must be signed in to change notification settings - Fork 172
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
base: main
Are you sure you want to change the base?
Netlify shell plugin #333
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am sure I see a |
||
), | ||
Uses: []schema.CredentialUsage{ | ||
{ | ||
Name: credname.PersonalAccessToken, | ||
}, | ||
}, | ||
} | ||
} |
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-", | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't look like |
||||||||||||||
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) { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? shell-plugins/plugins/ngrok/credentials.go Lines 51 to 56 in 8df60af
|
||||||||||||||
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 | ||||||||||||||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After removing the prefix (addressed on another comment above), could we run |
||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
// and fill the necessary details in the test template below. | ||
"config file": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
// }, | ||
// }, | ||
}, | ||
}, | ||
}) | ||
} |
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(), | ||
}, | ||
} | ||
} |
There was a problem hiding this comment.
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
andlogout
?