-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from rajapri28613/kaggle
Add support for Kaggle CLI
- Loading branch information
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package kaggle | ||
|
||
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://www.kaggle.com/docs/api"), | ||
ManagementURL: sdk.URL("https://www.kaggle.com/settings/account"), | ||
Fields: []schema.CredentialField{ | ||
{ | ||
Name: fieldname.Token, | ||
MarkdownDescription: "API Token used to authenticate to Kaggle.", | ||
Secret: true, | ||
Composition: &schema.ValueComposition{ | ||
Length: 32, | ||
Charset: schema.Charset{ | ||
Lowercase: true, | ||
Digits: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: fieldname.Username, | ||
MarkdownDescription: "Username to authenticate to Kaggle.", | ||
Secret: false, | ||
Composition: &schema.ValueComposition{ | ||
Charset: schema.Charset{ | ||
Lowercase: true, | ||
Digits: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), | ||
Importer: importer.TryAll( | ||
importer.TryEnvVarPair(defaultEnvVarMapping), | ||
TryKaggleConfigFile("~/.kaggle/kaggle.json"), | ||
)} | ||
} | ||
|
||
var defaultEnvVarMapping = map[string]sdk.FieldName{ | ||
"KAGGLE_KEY": fieldname.Token, | ||
"KAGGLE_USERNAME": fieldname.Username, | ||
} | ||
|
||
func TryKaggleConfigFile(path string) sdk.Importer { | ||
return importer.TryFile(path, 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.Token == "" { | ||
return | ||
} | ||
|
||
out.AddCandidate(sdk.ImportCandidate{ | ||
Fields: map[sdk.FieldName]string{ | ||
fieldname.Token: config.Token, | ||
fieldname.Username: config.Username, | ||
}, | ||
NameHint: importer.SanitizeNameHint(config.Username), | ||
}) | ||
}) | ||
} | ||
|
||
type Config struct { | ||
Username string `json:"username"` | ||
Token string `json:"key"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package kaggle | ||
|
||
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{ | ||
fieldname.Token: "z2pifkruzgbb17plmz2gux21fexample", | ||
fieldname.Username: "username", | ||
}, | ||
ExpectedOutput: sdk.ProvisionOutput{ | ||
Environment: map[string]string{ | ||
"KAGGLE_KEY": "z2pifkruzgbb17plmz2gux21fexample", | ||
"KAGGLE_USERNAME": "username", | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAPITokenImporter(t *testing.T) { | ||
plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ | ||
"environment": { | ||
Environment: map[string]string{ | ||
"KAGGLE_KEY": "z2pifkruzgbb17plmz2gux21fexample", | ||
"KAGGLE_USERNAME": "username", | ||
}, | ||
ExpectedCandidates: []sdk.ImportCandidate{ | ||
{ | ||
Fields: map[sdk.FieldName]string{ | ||
fieldname.Token: "z2pifkruzgbb17plmz2gux21fexample", | ||
fieldname.Username: "username", | ||
}, | ||
}, | ||
}, | ||
}, | ||
"config file": { | ||
Files: map[string]string{ | ||
"~/.kaggle/kaggle.json": plugintest.LoadFixture(t, "config.json"), | ||
}, | ||
ExpectedCandidates: []sdk.ImportCandidate{ | ||
{ | ||
Fields: map[sdk.FieldName]string{ | ||
fieldname.Token: "z2pifkruzgbb17plmz2gux21fexample", | ||
fieldname.Username: "username", | ||
}, | ||
NameHint: "username", | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package kaggle | ||
|
||
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 KaggleCLI() schema.Executable { | ||
return schema.Executable{ | ||
Name: "Kaggle CLI", | ||
Runs: []string{"kaggle"}, | ||
DocsURL: sdk.URL("https://github.com/Kaggle/kaggle-api"), | ||
NeedsAuth: needsauth.IfAll( | ||
needsauth.NotForHelpOrVersion(), | ||
needsauth.NotWithoutArgs(), | ||
), | ||
Uses: []schema.CredentialUsage{ | ||
{ | ||
Name: credname.APIToken, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package kaggle | ||
|
||
import ( | ||
"github.com/1Password/shell-plugins/sdk" | ||
"github.com/1Password/shell-plugins/sdk/schema" | ||
) | ||
|
||
func New() schema.Plugin { | ||
return schema.Plugin{ | ||
Name: "kaggle", | ||
Platform: schema.PlatformInfo{ | ||
Name: "Kaggle", | ||
Homepage: sdk.URL("https://kaggle.com"), | ||
}, | ||
Credentials: []schema.CredentialType{ | ||
APIToken(), | ||
}, | ||
Executables: []schema.Executable{ | ||
KaggleCLI(), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"username":"username","key":"z2pifkruzgbb17plmz2gux21fexample"} |