diff --git a/plugins/stackhero/access_token.go b/plugins/stackhero/access_token.go new file mode 100644 index 00000000..e7764930 --- /dev/null +++ b/plugins/stackhero/access_token.go @@ -0,0 +1,39 @@ +package stackhero + +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 AccessToken() schema.CredentialType { + return schema.CredentialType{ + Name: credname.AccessToken, + DocsURL: sdk.URL("https://www.stackhero.io/stackhero/documentations/Use-the-CLI#Non-interactive-authentication-for-scripts-CI-and-automation"), + ManagementURL: sdk.URL("https://dashboard.stackhero.io/account/"), + Fields: []schema.CredentialField{ + { + Name: fieldname.Token, + MarkdownDescription: "Token used to authenticate to Stackhero.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 21, + Prefix: "usr-", + Charset: schema.Charset{ + Lowercase: true, + }, + }, + }, + }, + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryAll( + importer.TryEnvVarPair(defaultEnvVarMapping), + )} +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "STACKHERO_TOKEN": fieldname.Token, +} diff --git a/plugins/stackhero/access_token_test.go b/plugins/stackhero/access_token_test.go new file mode 100644 index 00000000..99d41982 --- /dev/null +++ b/plugins/stackhero/access_token_test.go @@ -0,0 +1,41 @@ +package stackhero + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestAccessTokenProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, AccessToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + fieldname.Token: "usr-eqovniewliexample", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "STACKHERO_TOKEN": "usr-eqovniewliexample", + }, + }, + }, + }) +} + +func TestAccessTokenImporter(t *testing.T) { + plugintest.TestImporter(t, AccessToken().Importer, map[string]plugintest.ImportCase{ + "environment": { + Environment: map[string]string{ + "STACKHERO_TOKEN": "usr-eqovniewliexample", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "usr-eqovniewliexample", + }, + }, + }, + }, + }) +} diff --git a/plugins/stackhero/plugin.go b/plugins/stackhero/plugin.go new file mode 100644 index 00000000..dd37cdec --- /dev/null +++ b/plugins/stackhero/plugin.go @@ -0,0 +1,22 @@ +package stackhero + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "stackhero", + Platform: schema.PlatformInfo{ + Name: "Stackhero", + Homepage: sdk.URL("https://www.stackhero.io/"), + }, + Credentials: []schema.CredentialType{ + AccessToken(), + }, + Executables: []schema.Executable{ + StackheroCLI(), + }, + } +} diff --git a/plugins/stackhero/stackhero.go b/plugins/stackhero/stackhero.go new file mode 100644 index 00000000..97cf6735 --- /dev/null +++ b/plugins/stackhero/stackhero.go @@ -0,0 +1,29 @@ +package stackhero + +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 StackheroCLI() schema.Executable { + return schema.Executable{ + Name: "Stackhero CLI", + Runs: []string{"stackhero"}, + DocsURL: sdk.URL("https://www.stackhero.io/stackhero/documentations/Use-the-CLI"), + NeedsAuth: needsauth.IfAll( + needsauth.NotForCommand("login"), + needsauth.NotForCommand("logout"), + needsauth.NotForCommand("self-update"), + needsauth.NotForHelpOrVersion(), + needsauth.NotWhenContainsArgs("--help-agents"), + needsauth.NotWithoutArgs(), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.AccessToken, + }, + }, + } +}