Skip to content

Commit

Permalink
feat: Create Railway CLI shell plugin (1Password#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanprince committed Aug 20, 2024
1 parent 7f3d289 commit 80604b5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 58 deletions.
37 changes: 3 additions & 34 deletions plugins/railway/api_token.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -14,16 +12,15 @@ 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,
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,
Expand All @@ -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
// }
26 changes: 6 additions & 20 deletions plugins/railway/api_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
Expand All @@ -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{
{
Expand All @@ -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",
// },
// },
},
},
})
}
2 changes: 1 addition & 1 deletion plugins/railway/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions plugins/railway/railway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 80604b5

Please sign in to comment.