-
Notifications
You must be signed in to change notification settings - Fork 182
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
base: main
Are you sure you want to change the base?
Add Exercism plugin #404
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"), | ||
), | ||
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 | ||
} |
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", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Is |
||
needsauth.NotForHelpOrVersion(), | ||
needsauth.NotWithoutArgs(), | ||
), | ||
Uses: []schema.CredentialUsage{ | ||
{ | ||
Name: credname.APIKey, | ||
}, | ||
}, | ||
} | ||
} |
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(), | ||
}, | ||
} | ||
} |
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" | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.