Skip to content

Commit

Permalink
Deta.space initital setup
Browse files Browse the repository at this point in the history
  • Loading branch information
smyja committed Jun 25, 2023
1 parent 75bd39c commit 1e339fb
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
71 changes: 71 additions & 0 deletions plugins/deta/api_key.go
Original file line number Diff line number Diff line change
@@ -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
// }
55 changes: 55 additions & 0 deletions plugins/deta/api_key_test.go
Original file line number Diff line number Diff line change
@@ -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",
// },
// },
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/deta/plugin.go
Original file line number Diff line number Diff line change
@@ -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(),
},
}
}
25 changes: 25 additions & 0 deletions plugins/deta/space.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
}
}

0 comments on commit 1e339fb

Please sign in to comment.