-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
// }, | ||
// }, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
} | ||
} |