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

Add support for Pipedream CLI #338

Merged
merged 4 commits into from
Jul 25, 2023
Merged
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
102 changes: 102 additions & 0 deletions plugins/pipedream/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package pipedream

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://pipedream.com/docs/api_key"),
ManagementURL: sdk.URL("https://console.pipedream.com/user/security/tokens"),
Fields: []schema.CredentialField{
{
Name: fieldname.APIKey,
MarkdownDescription: "API Key used to authenticate to Pipedream.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 32,
Charset: schema.Charset{
Lowercase: true,
Digits: true,
},
},
},
{
Name: fieldname.OrgID,
MarkdownDescription: "OrgId for the Pipedream organization.",
Secret: false,
Optional: true,
Composition: &schema.ValueComposition{
Length: 9,
Charset: schema.Charset{
Lowercase: true,
Uppercase: true,
Digits: true,
Symbols: true,
},
},
},
},
DefaultProvisioner: provision.TempFile(
pipedreamConfig,
provision.AtFixedPath("~/.config/pipedream/config"),
),
Importer: importer.TryAll(
TryPipedreamConfigFile(),
)}
}

func TryPipedreamConfigFile() sdk.Importer {
rajapri28613 marked this conversation as resolved.
Show resolved Hide resolved
return importer.TryFile("~/.config/pipedream/config", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
configFile, err := contents.ToINI()

if err != nil {
out.AddError(err)
return
}

for _, section := range configFile.Sections() {
fields := make(map[sdk.FieldName]string)
if section.HasKey("api_key") && section.Key("api_key").Value() != "" {
fields[fieldname.APIKey] = section.Key("api_key").Value()
}
if section.HasKey("org_id") && section.Key("org_id").Value() != "" {
fields[fieldname.OrgID] = section.Key("org_id").Value()
}

if fields[fieldname.APIKey] != "" {
out.AddCandidate(sdk.ImportCandidate{
Fields: fields,
NameHint: importer.SanitizeNameHint(section.Name()),
})
}
}
})
}

type Config struct {
APIKey string
OrgId string
}

func pipedreamConfig(in sdk.ProvisionInput) ([]byte, error) {
rajapri28613 marked this conversation as resolved.
Show resolved Hide resolved
contents := ""

if apikey, ok := in.ItemFields[fieldname.APIKey]; ok {
contents += "api_key = " + apikey + "\n"
}

if orgid, ok := in.ItemFields[fieldname.OrgID]; ok {
contents += "org_id = " + orgid + "\n"
}

return []byte(contents), nil
}
59 changes: 59 additions & 0 deletions plugins/pipedream/api_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package pipedream

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{
"config file": {
ItemFields: map[sdk.FieldName]string{
fieldname.APIKey: "ugvfxesz62ycsl42z49c0t1hjexample",
fieldname.OrgID: "YbEXAMPLE",
},
ExpectedOutput: sdk.ProvisionOutput{
Files: map[string]sdk.OutputFile{
"~/.config/pipedream/config": {
Contents: []byte(plugintest.LoadFixture(t, "provision")),
},
},
},
},
})
}

func TestAPIKeyImporter(t *testing.T) {
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
"config file": {
Files: map[string]string{
"~/.config/pipedream/config": plugintest.LoadFixture(t, "import"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "ugvfxesz62ycsl42z49c0t1hjexample",
fieldname.OrgID: "YbEXAMPLE",
},
NameHint: "DEFAULT",
},
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "5puf32rvhkz83c6oj4wpxvaniexample",
fieldname.OrgID: "KVEXAMPLE",
},
NameHint: "first",
},
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "lgx1amb0qf7mjy6y7nkgfc3x9example",
},
NameHint: "second",
},
},
},
})
}
27 changes: 27 additions & 0 deletions plugins/pipedream/pd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pipedream

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 PipedreamCLI() schema.Executable {
return schema.Executable{
Name: "Pipedream CLI",
Runs: []string{"pd"},
DocsURL: sdk.URL("https://pipedream.com/docs/cli/reference/"),
NeedsAuth: needsauth.IfAll(
rajapri28613 marked this conversation as resolved.
Show resolved Hide resolved
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
needsauth.NotWhenContainsArgs("-p"),
needsauth.NotWhenContainsArgs("--profile"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIKey,
},
},
}
}
22 changes: 22 additions & 0 deletions plugins/pipedream/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pipedream

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

func New() schema.Plugin {
return schema.Plugin{
Name: "pipedream",
Platform: schema.PlatformInfo{
Name: "Pipedream",
Homepage: sdk.URL("https://pipedream.com"),
},
Credentials: []schema.CredentialType{
APIKey(),
},
Executables: []schema.Executable{
PipedreamCLI(),
},
}
}
9 changes: 9 additions & 0 deletions plugins/pipedream/test-fixtures/import
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
api_key = ugvfxesz62ycsl42z49c0t1hjexample
org_id = YbEXAMPLE

[first]
api_key = 5puf32rvhkz83c6oj4wpxvaniexample
org_id = KVEXAMPLE

[second]
api_key = lgx1amb0qf7mjy6y7nkgfc3x9example
2 changes: 2 additions & 0 deletions plugins/pipedream/test-fixtures/provision
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
api_key = ugvfxesz62ycsl42z49c0t1hjexample
org_id = YbEXAMPLE
2 changes: 2 additions & 0 deletions sdk/schema/fieldname/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
Mode = sdk.FieldName("Mode")
Namespace = sdk.FieldName("Namespace")
OneTimePassword = sdk.FieldName("One-Time Password")
OrgID = sdk.FieldName("Org ID")
OrgURL = sdk.FieldName("Org URL")
Organization = sdk.FieldName("Organization")
Password = sdk.FieldName("Password")
Expand Down Expand Up @@ -86,6 +87,7 @@ func ListAll() []sdk.FieldName {
Mode,
Namespace,
OneTimePassword,
OrgID,
OrgURL,
Organization,
Password,
Expand Down