diff --git a/plugins/deta/api_key.go b/plugins/deta/api_key.go new file mode 100644 index 000000000..1dc3fd324 --- /dev/null +++ b/plugins/deta/api_key.go @@ -0,0 +1,71 @@ +package deta + +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 APIKey() schema.CredentialType { + return schema.CredentialType{ + Name: credname.APIKey, + DocsURL: sdk.URL("https://deta.com/docs/api_key"), // TODO: Replace with actual URL + ManagementURL: sdk.URL("https://console.deta.com/user/security/tokens"), // TODO: Replace with actual URL + Fields: []schema.CredentialField{ + { + Name: fieldname.APIKey, + MarkdownDescription: "API Key used to authenticate to Deta.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 25, + Prefix: "bE53FsM6_", // TODO: Check if this is correct + Charset: schema.Charset{ + Uppercase: true, + Lowercase: true, + Digits: true, + }, + }, + }, + }, + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryAll( + importer.TryEnvVarPair(defaultEnvVarMapping), + TryDetaConfigFile(), + )} +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "DETA_API_KEY": fieldname.APIKey, // TODO: Check if this is correct +} + +// TODO: Check if the platform stores the API Key in a local config file, and if so, +// implement the function below to add support for importing it. +func TryDetaConfigFile() sdk.Importer { + return importer.TryFile("~/path/to/config/file.yml", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { + // var config Config + // if err := contents.ToYAML(&config); err != nil { + // out.AddError(err) + // return + // } + + // if config.APIKey == "" { + // return + // } + + // out.AddCandidate(sdk.ImportCandidate{ + // Fields: map[sdk.FieldName]string{ + // fieldname.APIKey: config.APIKey, + // }, + // }) + }) +} + +// TODO: Implement the config file schema +// type Config struct { +// APIKey string +// } diff --git a/plugins/deta/api_key_test.go b/plugins/deta/api_key_test.go new file mode 100644 index 000000000..9102d45a6 --- /dev/null +++ b/plugins/deta/api_key_test.go @@ -0,0 +1,55 @@ +package deta + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestAPIKeyProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, APIKey().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ // TODO: Check if this is correct + fieldname.APIKey: "bE53FsM6_MPPjP3sg6EXAMPLE", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "DETA_API_KEY": "bE53FsM6_MPPjP3sg6EXAMPLE", + }, + }, + }, + }) +} + +func TestAPIKeyImporter(t *testing.T) { + plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{ + "environment": { + Environment: map[string]string{ // TODO: Check if this is correct + "DETA_API_KEY": "bE53FsM6_MPPjP3sg6EXAMPLE", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.APIKey: "bE53FsM6_MPPjP3sg6EXAMPLE", + }, + }, + }, + }, + // TODO: If you implemented a config file importer, add a test file example in deta/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: "bE53FsM6_MPPjP3sg6EXAMPLE", + // }, + // }, + }, + }, + }) +} diff --git a/plugins/deta/plugin.go b/plugins/deta/plugin.go new file mode 100644 index 000000000..1b8f50ef6 --- /dev/null +++ b/plugins/deta/plugin.go @@ -0,0 +1,22 @@ +package deta + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "deta", + Platform: schema.PlatformInfo{ + Name: "Deta", + Homepage: sdk.URL("https://deta.com"), // TODO: Check if this is correct + }, + Credentials: []schema.CredentialType{ + APIKey(), + }, + Executables: []schema.Executable{ + DetaCLI(), + }, + } +} diff --git a/plugins/deta/space.go b/plugins/deta/space.go new file mode 100644 index 000000000..ae437ab93 --- /dev/null +++ b/plugins/deta/space.go @@ -0,0 +1,25 @@ +package deta + +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 DetaCLI() schema.Executable { + return schema.Executable{ + Name: "Deta CLI", // TODO: Check if this is correct + Runs: []string{"space"}, + DocsURL: sdk.URL("https://deta.com/docs/cli"), // TODO: Replace with actual URL + NeedsAuth: needsauth.IfAll( + needsauth.NotForHelpOrVersion(), + needsauth.NotWithoutArgs(), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.APIKey, + }, + }, + } +}