From acb8ae01b3f91dd70b474f07c64fc1af2fe36f3e Mon Sep 17 00:00:00 2001 From: Calum Daines Date: Mon, 1 May 2023 10:30:55 +0100 Subject: [PATCH 1/4] Add plugin for Nimble package manager --- plugins/nimble/nimble.go | 27 ++++++++++ plugins/nimble/personal_access_token.go | 52 ++++++++++++++++++++ plugins/nimble/personal_access_token_test.go | 41 +++++++++++++++ plugins/nimble/plugin.go | 22 +++++++++ 4 files changed, 142 insertions(+) create mode 100644 plugins/nimble/nimble.go create mode 100644 plugins/nimble/personal_access_token.go create mode 100644 plugins/nimble/personal_access_token_test.go create mode 100644 plugins/nimble/plugin.go diff --git a/plugins/nimble/nimble.go b/plugins/nimble/nimble.go new file mode 100644 index 000000000..23c9900c4 --- /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", + 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..1037365ce --- /dev/null +++ b/plugins/nimble/personal_access_token.go @@ -0,0 +1,52 @@ +package nimble + +import ( + "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(defaultEnvVarMapping), + Importer: importer.TryAll( + TryNimbleConfigFile(), + importer.TryEnvVarPair(defaultEnvVarMapping), + ), + } +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "NIMBLE_GITHUB_API_TOKEN": fieldname.Token, +} + +func TryNimbleConfigFile() sdk.Importer { + // TODO: Try importing from ~/.nimble/github_api_token file + return importer.NoOp() +} + +type Config struct { + Token string `ini:"token"` +} diff --git a/plugins/nimble/personal_access_token_test.go b/plugins/nimble/personal_access_token_test.go new file mode 100644 index 000000000..9bcc00351 --- /dev/null +++ b/plugins/nimble/personal_access_token_test.go @@ -0,0 +1,41 @@ +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", + }, + }, + }, + }, + }) +} 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(), + }, + } +} From a98ed23f3407e6759d914d64dd0d9cc46831293c Mon Sep 17 00:00:00 2001 From: Calum Daines Date: Tue, 2 May 2023 21:59:56 +0100 Subject: [PATCH 2/4] prioritise TryEnvVarPair in importers --- plugins/nimble/personal_access_token.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/nimble/personal_access_token.go b/plugins/nimble/personal_access_token.go index 1037365ce..097f2c1b4 100644 --- a/plugins/nimble/personal_access_token.go +++ b/plugins/nimble/personal_access_token.go @@ -32,8 +32,8 @@ func PersonalAccessToken() schema.CredentialType { }, DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), Importer: importer.TryAll( - TryNimbleConfigFile(), importer.TryEnvVarPair(defaultEnvVarMapping), + TryNimbleConfigFile(), ), } } From f46c2ef36dd45442adbeebba7a0089f9c97ee955 Mon Sep 17 00:00:00 2001 From: Calum Daines Date: Sun, 7 May 2023 15:16:19 +0100 Subject: [PATCH 3/4] Add config file importer --- plugins/nimble/personal_access_token.go | 21 ++++++++++++------- plugins/nimble/personal_access_token_test.go | 12 +++++++++++ plugins/nimble/test-fixtures/github_api_token | 1 + 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 plugins/nimble/test-fixtures/github_api_token diff --git a/plugins/nimble/personal_access_token.go b/plugins/nimble/personal_access_token.go index 097f2c1b4..08b0a7d38 100644 --- a/plugins/nimble/personal_access_token.go +++ b/plugins/nimble/personal_access_token.go @@ -1,6 +1,8 @@ package nimble import ( + "context" + "github.com/1Password/shell-plugins/sdk" "github.com/1Password/shell-plugins/sdk/importer" "github.com/1Password/shell-plugins/sdk/provision" @@ -33,7 +35,7 @@ func PersonalAccessToken() schema.CredentialType { DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), - TryNimbleConfigFile(), + TryNimbleConfigFile("~/.nimble/github_api_token"), ), } } @@ -42,11 +44,16 @@ var defaultEnvVarMapping = map[string]sdk.FieldName{ "NIMBLE_GITHUB_API_TOKEN": fieldname.Token, } -func TryNimbleConfigFile() sdk.Importer { - // TODO: Try importing from ~/.nimble/github_api_token file - return importer.NoOp() -} +func TryNimbleConfigFile(path string) sdk.Importer { + return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { + credential := contents.ToString() + fields := make(map[sdk.FieldName]string) -type Config struct { - Token string `ini:"token"` + 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 index 9bcc00351..e740237ee 100644 --- a/plugins/nimble/personal_access_token_test.go +++ b/plugins/nimble/personal_access_token_test.go @@ -37,5 +37,17 @@ func TestPersonalAccessTokenImporter(t *testing.T) { }, }, }, + "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/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 From b5c44761dc7b186c54b7de5b02e6bee50dd39d28 Mon Sep 17 00:00:00 2001 From: Calum Daines Date: Tue, 6 Jun 2023 23:07:27 +0100 Subject: [PATCH 4/4] tidy: use clearer variable definitions --- plugins/nimble/nimble.go | 2 +- plugins/nimble/personal_access_token.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/nimble/nimble.go b/plugins/nimble/nimble.go index 23c9900c4..ca7ba71f6 100644 --- a/plugins/nimble/nimble.go +++ b/plugins/nimble/nimble.go @@ -9,7 +9,7 @@ import ( func nimCLI() schema.Executable { return schema.Executable{ - Name: "nimble", + Name: "Nimble CLI", Runs: []string{"nimble"}, DocsURL: sdk.URL("https://github.com/nim-lang/nimble"), NeedsAuth: needsauth.IfAll( diff --git a/plugins/nimble/personal_access_token.go b/plugins/nimble/personal_access_token.go index 08b0a7d38..b03708ae7 100644 --- a/plugins/nimble/personal_access_token.go +++ b/plugins/nimble/personal_access_token.go @@ -32,20 +32,20 @@ func PersonalAccessToken() schema.CredentialType { }, }, }, - DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + DefaultProvisioner: provision.EnvVars(nimbleDefaultEnvVarMapping), Importer: importer.TryAll( - importer.TryEnvVarPair(defaultEnvVarMapping), - TryNimbleConfigFile("~/.nimble/github_api_token"), + importer.TryEnvVarPair(nimbleDefaultEnvVarMapping), + TryNimbleConfigFile(), ), } } -var defaultEnvVarMapping = map[string]sdk.FieldName{ +var nimbleDefaultEnvVarMapping = map[string]sdk.FieldName{ "NIMBLE_GITHUB_API_TOKEN": fieldname.Token, } -func TryNimbleConfigFile(path string) sdk.Importer { - return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { +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)