Skip to content

Commit

Permalink
Merge pull request #338 from rajapri28613/pipedream
Browse files Browse the repository at this point in the history
Add support for Pipedream CLI
  • Loading branch information
Arun Sathiya authored Jul 25, 2023
2 parents 96ae561 + 396867d commit 3eaa0d5
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 0 deletions.
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 {
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) {
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(
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

0 comments on commit 3eaa0d5

Please sign in to comment.