Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render CLI Shell plugin #308

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions plugins/render/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package render
smyja marked this conversation as resolved.
Show resolved Hide resolved

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"
"gopkg.in/yaml.v2"
)

type Config struct {
Profiles map[string]Profile
}

type Profile struct {
APIKey string `yaml:"apiKey"`
}

func APIKey() schema.CredentialType {
return schema.CredentialType{
Name: credname.APIKey,
DocsURL: sdk.URL("https://render.com/docs"),
ManagementURL: sdk.URL("https://dashboard.render.com/u/settings#api-keys"),
Fields: []schema.CredentialField{
{
Name: fieldname.APIKey,
MarkdownDescription: "API Key used to authenticate to Render.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 30,
Charset: schema.Charset{
Uppercase: true,
Lowercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: provision.TempFile(renderConfig, provision.AtFixedPath("~/.render/config.yaml")),
Importer: TryRenderConfigFile(),
}
}

func renderConfig(in sdk.ProvisionInput) ([]byte, error) {
config := Config{
Profiles: map[string]Profile{
"default": {
APIKey: in.ItemFields[fieldname.APIKey],
},
},
}

contents, err := yaml.Marshal(&config)
if err != nil {
return nil, err
}
return []byte(contents), nil
}

func TryRenderConfigFile() sdk.Importer {
return importer.TryFile("~/.render/config.yaml", 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
}

for profileName, profile := range config.Profiles {
if profile.APIKey == "" {
continue
}

out.AddCandidate(sdk.ImportCandidate{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: profile.APIKey,
},
NameHint: importer.SanitizeNameHint(profileName),
})
}
})
}
43 changes: 43 additions & 0 deletions plugins/render/api_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package render
smyja marked this conversation as resolved.
Show resolved Hide resolved

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{
fieldname.APIKey: "rnd_Z7xMKp4NX1FoQNRyBpZs9yxDbu3i",
},
smyja marked this conversation as resolved.
Show resolved Hide resolved
ExpectedOutput: sdk.ProvisionOutput{
Files: map[string]sdk.OutputFile{
"~/.render/config.yaml": {
Contents: []byte(plugintest.LoadFixture(t, "config.yaml") + "\n"),
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a newline directly to the file, shall we?

},
},
},
})
}

func TestAPIKeyImporter(t *testing.T) {
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
smyja marked this conversation as resolved.
Show resolved Hide resolved
"config file": {
Files: map[string]string{
"~/.render/config.yaml": plugintest.LoadFixture(t, "config.yaml"),
smyja marked this conversation as resolved.
Show resolved Hide resolved
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "rnd_Z7xMKp4NX1FoQNRyBpZs9yxDbu3i",
},
},
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/render/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package render

import (
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/schema"
)

func New() schema.Plugin {
return schema.Plugin{
Name: "render",
Platform: schema.PlatformInfo{
Name: "Render",
Homepage: sdk.URL("https://render.com"),
},
Credentials: []schema.CredentialType{
APIKey(),
},
Executables: []schema.Executable{
RenderCLI(),
},
}
}
30 changes: 30 additions & 0 deletions plugins/render/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package render

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 RenderCLI() schema.Executable {
return schema.Executable{
Name: "Render CLI",
Runs: []string{"render"},
DocsURL: sdk.URL("https://render.com/docs/cli"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
smyja marked this conversation as resolved.
Show resolved Hide resolved
needsauth.NotWhenContainsArgs("config"),
needsauth.NotWhenContainsArgs("jobs"),
needsauth.NotWhenContainsArgs("deploys"),
needsauth.NotWhenContainsArgs("repo"),
needsauth.NotWhenContainsArgs("services"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIKey,
},
},
}
}
3 changes: 3 additions & 0 deletions plugins/render/test-fixtures/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
profiles:
default:
apiKey: rnd_Z7xMKp4NX1FoQNRyBpZs9yxDbu3i