diff --git a/plugins/nimble/nimble.go b/plugins/nimble/nimble.go new file mode 100644 index 000000000..ca7ba71f6 --- /dev/null +++ b/plugins/nimble/nimble.go @@ -0,0 +1,27 @@ +package nimble + +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 nimCLI() schema.Executable { + return schema.Executable{ + Name: "Nimble CLI", + Runs: []string{"nimble"}, + DocsURL: sdk.URL("https://github.com/nim-lang/nimble"), + NeedsAuth: needsauth.IfAll( + needsauth.IfAny( + needsauth.ForCommand("publish"), + ), + needsauth.NotForHelp(), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.PersonalAccessToken, + }, + }, + } +} diff --git a/plugins/nimble/personal_access_token.go b/plugins/nimble/personal_access_token.go new file mode 100644 index 000000000..b03708ae7 --- /dev/null +++ b/plugins/nimble/personal_access_token.go @@ -0,0 +1,59 @@ +package nimble + +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://github.com/nim-lang/nimble"), + ManagementURL: sdk.URL("https://nimble.directory/about.html"), + Fields: []schema.CredentialField{ + { + Name: fieldname.Token, + MarkdownDescription: "Token used to authenticate to GitHub.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 40, + Prefix: "ghp_", + Charset: schema.Charset{ + Uppercase: true, + Lowercase: true, + Digits: true, + }, + }, + }, + }, + DefaultProvisioner: provision.EnvVars(nimbleDefaultEnvVarMapping), + Importer: importer.TryAll( + importer.TryEnvVarPair(nimbleDefaultEnvVarMapping), + TryNimbleConfigFile(), + ), + } +} + +var nimbleDefaultEnvVarMapping = map[string]sdk.FieldName{ + "NIMBLE_GITHUB_API_TOKEN": fieldname.Token, +} + +func TryNimbleConfigFile() sdk.Importer { + return importer.TryFile("~/.nimble/github_api_token", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { + credential := contents.ToString() + fields := make(map[sdk.FieldName]string) + + if len(credential) != 0 { + fields[fieldname.Token] = credential + } + out.AddCandidate(sdk.ImportCandidate{ + Fields: fields, + }) + }) +} diff --git a/plugins/nimble/personal_access_token_test.go b/plugins/nimble/personal_access_token_test.go new file mode 100644 index 000000000..e740237ee --- /dev/null +++ b/plugins/nimble/personal_access_token_test.go @@ -0,0 +1,53 @@ +package nimble + +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{ + fieldname.Token: "ghp_ahhfw82h48fh72nfn29fn291nwidhf8EXAMPLE", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "NIMBLE_GITHUB_API_TOKEN": "ghp_ahhfw82h48fh72nfn29fn291nwidhf8EXAMPLE", + }, + }, + }, + }) +} + +func TestPersonalAccessTokenImporter(t *testing.T) { + plugintest.TestImporter(t, PersonalAccessToken().Importer, map[string]plugintest.ImportCase{ + "environment": { + Environment: map[string]string{ + "NIMBLE_GITHUB_API_TOKEN": "ghp_ahhfw82h48fh72nfn29fn291nwidhf8EXAMPLE", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "ghp_ahhfw82h48fh72nfn29fn291nwidhf8EXAMPLE", + }, + }, + }, + }, + "config file": { + Files: map[string]string{ + "~/.nimble/github_api_token": plugintest.LoadFixture(t, "github_api_token"), + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "ghp_ahhfw82h48fh72nfn29fn291nwidhf8EXAMPLE", + }, + }, + }, + }, + }) +} diff --git a/plugins/nimble/plugin.go b/plugins/nimble/plugin.go new file mode 100644 index 000000000..8fe389a63 --- /dev/null +++ b/plugins/nimble/plugin.go @@ -0,0 +1,22 @@ +package nimble + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "nimble", + Platform: schema.PlatformInfo{ + Name: "Nimble", + Homepage: sdk.URL("https://nimble.directory/"), + }, + Credentials: []schema.CredentialType{ + PersonalAccessToken(), + }, + Executables: []schema.Executable{ + nimCLI(), + }, + } +} diff --git a/plugins/nimble/test-fixtures/github_api_token b/plugins/nimble/test-fixtures/github_api_token new file mode 100644 index 000000000..974328ba4 --- /dev/null +++ b/plugins/nimble/test-fixtures/github_api_token @@ -0,0 +1 @@ +ghp_ahhfw82h48fh72nfn29fn291nwidhf8EXAMPLE \ No newline at end of file