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 Exercism plugin #404

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
94 changes: 94 additions & 0 deletions plugins/exercism/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package exercism

import (
"context"
"encoding/json"

"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://exercism.org/docs/using/solving-exercises/working-locally"),
ManagementURL: sdk.URL("https://exercism.org/settings/api_cli"),
Fields: []schema.CredentialField{
{
Name: fieldname.URL,
MarkdownDescription: `The URL of the Exercism API.`,
Secret: false,
},
{
Name: fieldname.APIKey,
MarkdownDescription: "API Key used to authenticate to Exercism.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 37,
Charset: schema.Charset{
Lowercase: true,
Digits: true,
},
},
},
{
Name: sdk.FieldName("Directory"),
MarkdownDescription: `The path to the workspace directory where the exercises are stored.`,
Secret: false,
},
},
DefaultProvisioner: provision.TempFile(
tempFileConfig,
provision.AtFixedPath("~/.config/exercism/user.json"),
Copy link
Member

Choose a reason for hiding this comment

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

For many tools, the equivalent of ~/.config from Linux on MacOS is ~/Library/Application Support.

Is this the case here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually had to find the config file on my Mac since the documentation doesn't give the location, so it is indeed ~/.config for MacOS.

),
Importer: importer.TryAll(
TryExercismConfigFile(),
)}
}

func TryExercismConfigFile() sdk.Importer {
return importer.TryFile("~/.config/exercism/user.json", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
var config Config
if err := contents.ToJSON(&config); err != nil {
out.AddError(err)
return
}

if config.URL == "" || config.APIKey == "" || config.Workspace == "" {
return
}

out.AddCandidate(sdk.ImportCandidate{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: config.APIKey,
fieldname.URL: config.URL,
sdk.FieldName("Directory"): config.Workspace,
},
})
})
}

type Config struct {
URL string `json:"apibaseurl"`
APIKey string `json:"token"`
Workspace string `json:"workspace"`
}

func tempFileConfig(in sdk.ProvisionInput) ([]byte, error) {
config := Config{
URL: in.ItemFields[fieldname.URL],
APIKey: in.ItemFields[fieldname.APIKey],
Workspace: in.ItemFields[sdk.FieldName("Directory")],
}

contents, err := json.Marshal(&config)
if err != nil {
return nil, err
}

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

import (
"strings"
"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.URL: "https://api.exercism.io/v1",
fieldname.APIKey: "v1o2p80wuf2qhnurrvf8rigro6sp38example",
sdk.FieldName("Directory"): "/Users/username/exercism",
},
ExpectedOutput: sdk.ProvisionOutput{
Files: map[string]sdk.OutputFile{
"~/.config/exercism/user.json": {
Contents: []byte(strings.Join(strings.Fields(plugintest.LoadFixture(t, "user.json")), "")),
},
},
},
},
})
}

func TestAPIKeyImporter(t *testing.T) {
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
"config file": {
Files: map[string]string{
"~/.config/exercism/user.json": plugintest.LoadFixture(t, "user.json"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.URL: "https://api.exercism.io/v1",
fieldname.APIKey: "v1o2p80wuf2qhnurrvf8rigro6sp38example",
sdk.FieldName("Directory"): "/Users/username/exercism",
},
},
},
},
})
}
25 changes: 25 additions & 0 deletions plugins/exercism/exercism.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package exercism

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 ExercismCLI() schema.Executable {
return schema.Executable{
Name: "Exercism CLI",
Runs: []string{"exercism"},
DocsURL: sdk.URL("https://exercism.org/docs/using/solving-exercises/working-locally"),
NeedsAuth: needsauth.IfAll(
Copy link
Member

Choose a reason for hiding this comment

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

I think there may be some other situations as well when we don't want to prompt, for example for a command such as exercism configure --token=<your-api-token>.

Is --token a flag available for all the commands? In that case, we may want to exclude this flag from prompting altogether.

needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIKey,
},
},
}
}
22 changes: 22 additions & 0 deletions plugins/exercism/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package exercism

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

func New() schema.Plugin {
return schema.Plugin{
Name: "exercism",
Platform: schema.PlatformInfo{
Name: "Exercism",
Homepage: sdk.URL("https://exercism.org"),
},
Credentials: []schema.CredentialType{
APIKey(),
},
Executables: []schema.Executable{
ExercismCLI(),
},
}
}
5 changes: 5 additions & 0 deletions plugins/exercism/test-fixtures/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"apibaseurl":"https://api.exercism.io/v1",
"token":"v1o2p80wuf2qhnurrvf8rigro6sp38example",
"workspace":"/Users/username/exercism"
}