From 7f3d28926a3153166f275018c2f03254b6014946 Mon Sep 17 00:00:00 2001 From: Aryan Prince Date: Tue, 20 Aug 2024 17:02:59 +0100 Subject: [PATCH 1/2] chore: Create new empty plugin with Makefile command --- plugins/railway/api_token.go | 70 +++++++++++++++++++++++++++++++ plugins/railway/api_token_test.go | 55 ++++++++++++++++++++++++ plugins/railway/plugin.go | 22 ++++++++++ plugins/railway/railway.go | 25 +++++++++++ 4 files changed, 172 insertions(+) create mode 100644 plugins/railway/api_token.go create mode 100644 plugins/railway/api_token_test.go create mode 100644 plugins/railway/plugin.go create mode 100644 plugins/railway/railway.go diff --git a/plugins/railway/api_token.go b/plugins/railway/api_token.go new file mode 100644 index 00000000..3b299f6b --- /dev/null +++ b/plugins/railway/api_token.go @@ -0,0 +1,70 @@ +package railway + +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 APIToken() schema.CredentialType { + return schema.CredentialType{ + Name: credname.APIToken, + DocsURL: sdk.URL("https://railway.com/docs/api_token"), // TODO: Replace with actual URL + ManagementURL: sdk.URL("https://console.railway.com/user/security/tokens"), // TODO: Replace with actual URL + Fields: []schema.CredentialField{ + { + Name: fieldname.Token, + MarkdownDescription: "Token used to authenticate to Railway.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 36, + Prefix: "aa7b1a4b-7bb3-", // TODO: Check if this is correct + Charset: schema.Charset{ + Lowercase: true, + Digits: true, + }, + }, + }, + }, + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryAll( + importer.TryEnvVarPair(defaultEnvVarMapping), + TryRailwayConfigFile(), + )} +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "RAILWAY_TOKEN": fieldname.Token, // TODO: Check if this is correct +} + +// TODO: Check if the platform stores the API Token in a local config file, and if so, +// implement the function below to add support for importing it. +func TryRailwayConfigFile() 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.Token == "" { + // return + // } + + // out.AddCandidate(sdk.ImportCandidate{ + // Fields: map[sdk.FieldName]string{ + // fieldname.Token: config.Token, + // }, + // }) + }) +} + +// TODO: Implement the config file schema +// type Config struct { +// Token string +// } diff --git a/plugins/railway/api_token_test.go b/plugins/railway/api_token_test.go new file mode 100644 index 00000000..cf9982f8 --- /dev/null +++ b/plugins/railway/api_token_test.go @@ -0,0 +1,55 @@ +package railway + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestAPITokenProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ // TODO: Check if this is correct + fieldname.Token: "abcdefghijklm-1234567890-example", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "RAILWAY_TOKEN": "abcdefghijklm-1234567890-example", + }, + }, + }, + }) +} + +func TestAPITokenImporter(t *testing.T) { + plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ + "environment": { + Environment: map[string]string{ // TODO: Check if this is correct + "RAILWAY_TOKEN": "abcdefghijklm-1234567890-example", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "abcdefghijklm-1234567890-example", + }, + }, + }, + }, + // TODO: If you implemented a config file importer, add a test file example in railway/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: "abcdefghijklm-1234567890-example", + // }, + // }, + }, + }, + }) +} diff --git a/plugins/railway/plugin.go b/plugins/railway/plugin.go new file mode 100644 index 00000000..9478f2d8 --- /dev/null +++ b/plugins/railway/plugin.go @@ -0,0 +1,22 @@ +package railway + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "railway", + Platform: schema.PlatformInfo{ + Name: "Railway", + Homepage: sdk.URL("https://railway.com"), // TODO: Check if this is correct + }, + Credentials: []schema.CredentialType{ + APIToken(), + }, + Executables: []schema.Executable{ + RailwayCLI(), + }, + } +} diff --git a/plugins/railway/railway.go b/plugins/railway/railway.go new file mode 100644 index 00000000..81fcc9fb --- /dev/null +++ b/plugins/railway/railway.go @@ -0,0 +1,25 @@ +package railway + +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 RailwayCLI() schema.Executable { + return schema.Executable{ + Name: "Railway CLI", // TODO: Check if this is correct + Runs: []string{"railway"}, + DocsURL: sdk.URL("https://railway.com/docs/cli"), // TODO: Replace with actual URL + NeedsAuth: needsauth.IfAll( + needsauth.NotForHelpOrVersion(), + needsauth.NotWithoutArgs(), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.APIToken, + }, + }, + } +} From 80604b51e1f79b79d8a37608073d0e20ff3bec9f Mon Sep 17 00:00:00 2001 From: Aryan Prince Date: Tue, 20 Aug 2024 17:07:15 +0100 Subject: [PATCH 2/2] feat: Create Railway CLI shell plugin (#151) --- plugins/railway/api_token.go | 37 +++---------------------------- plugins/railway/api_token_test.go | 26 +++++----------------- plugins/railway/plugin.go | 2 +- plugins/railway/railway.go | 6 ++--- 4 files changed, 13 insertions(+), 58 deletions(-) diff --git a/plugins/railway/api_token.go b/plugins/railway/api_token.go index 3b299f6b..60c41738 100644 --- a/plugins/railway/api_token.go +++ b/plugins/railway/api_token.go @@ -1,8 +1,6 @@ package railway import ( - "context" - "github.com/1Password/shell-plugins/sdk" "github.com/1Password/shell-plugins/sdk/importer" "github.com/1Password/shell-plugins/sdk/provision" @@ -14,8 +12,8 @@ import ( func APIToken() schema.CredentialType { return schema.CredentialType{ Name: credname.APIToken, - DocsURL: sdk.URL("https://railway.com/docs/api_token"), // TODO: Replace with actual URL - ManagementURL: sdk.URL("https://console.railway.com/user/security/tokens"), // TODO: Replace with actual URL + DocsURL: sdk.URL("https://docs.railway.app/guides/cli#authenticating-with-the-cli"), + ManagementURL: sdk.URL("https://railway.app/account/tokens"), Fields: []schema.CredentialField{ { Name: fieldname.Token, @@ -23,7 +21,6 @@ func APIToken() schema.CredentialType { Secret: true, Composition: &schema.ValueComposition{ Length: 36, - Prefix: "aa7b1a4b-7bb3-", // TODO: Check if this is correct Charset: schema.Charset{ Lowercase: true, Digits: true, @@ -34,37 +31,9 @@ func APIToken() schema.CredentialType { DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), - TryRailwayConfigFile(), )} } var defaultEnvVarMapping = map[string]sdk.FieldName{ - "RAILWAY_TOKEN": fieldname.Token, // TODO: Check if this is correct -} - -// TODO: Check if the platform stores the API Token in a local config file, and if so, -// implement the function below to add support for importing it. -func TryRailwayConfigFile() 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.Token == "" { - // return - // } - - // out.AddCandidate(sdk.ImportCandidate{ - // Fields: map[sdk.FieldName]string{ - // fieldname.Token: config.Token, - // }, - // }) - }) + "RAILWAY_API_TOKEN": fieldname.Token, } - -// TODO: Implement the config file schema -// type Config struct { -// Token string -// } diff --git a/plugins/railway/api_token_test.go b/plugins/railway/api_token_test.go index cf9982f8..00a677a6 100644 --- a/plugins/railway/api_token_test.go +++ b/plugins/railway/api_token_test.go @@ -2,21 +2,21 @@ package railway import ( "testing" - + "github.com/1Password/shell-plugins/sdk" "github.com/1Password/shell-plugins/sdk/plugintest" "github.com/1Password/shell-plugins/sdk/schema/fieldname" ) - + func TestAPITokenProvisioner(t *testing.T) { plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ "default": { - ItemFields: map[sdk.FieldName]string{ // TODO: Check if this is correct + ItemFields: map[sdk.FieldName]string{ fieldname.Token: "abcdefghijklm-1234567890-example", }, ExpectedOutput: sdk.ProvisionOutput{ Environment: map[string]string{ - "RAILWAY_TOKEN": "abcdefghijklm-1234567890-example", + "RAILWAY_API_TOKEN": "abcdefghijklm-1234567890-example", }, }, }, @@ -26,8 +26,8 @@ func TestAPITokenProvisioner(t *testing.T) { func TestAPITokenImporter(t *testing.T) { plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ "environment": { - Environment: map[string]string{ // TODO: Check if this is correct - "RAILWAY_TOKEN": "abcdefghijklm-1234567890-example", + Environment: map[string]string{ + "RAILWAY_API_TOKEN": "abcdefghijklm-1234567890-example", }, ExpectedCandidates: []sdk.ImportCandidate{ { @@ -37,19 +37,5 @@ func TestAPITokenImporter(t *testing.T) { }, }, }, - // TODO: If you implemented a config file importer, add a test file example in railway/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: "abcdefghijklm-1234567890-example", - // }, - // }, - }, - }, }) } diff --git a/plugins/railway/plugin.go b/plugins/railway/plugin.go index 9478f2d8..95564fb9 100644 --- a/plugins/railway/plugin.go +++ b/plugins/railway/plugin.go @@ -10,7 +10,7 @@ func New() schema.Plugin { Name: "railway", Platform: schema.PlatformInfo{ Name: "Railway", - Homepage: sdk.URL("https://railway.com"), // TODO: Check if this is correct + Homepage: sdk.URL("https://railway.app"), }, Credentials: []schema.CredentialType{ APIToken(), diff --git a/plugins/railway/railway.go b/plugins/railway/railway.go index 81fcc9fb..08479116 100644 --- a/plugins/railway/railway.go +++ b/plugins/railway/railway.go @@ -9,9 +9,9 @@ import ( func RailwayCLI() schema.Executable { return schema.Executable{ - Name: "Railway CLI", // TODO: Check if this is correct - Runs: []string{"railway"}, - DocsURL: sdk.URL("https://railway.com/docs/cli"), // TODO: Replace with actual URL + Name: "Railway CLI", + Runs: []string{"railway"}, + DocsURL: sdk.URL("https://docs.railway.app/guides/cli"), NeedsAuth: needsauth.IfAll( needsauth.NotForHelpOrVersion(), needsauth.NotWithoutArgs(),