Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rajapri28613 committed Jul 18, 2023
1 parent 1e9608f commit 91e933d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
30 changes: 30 additions & 0 deletions plugins/kaggle/api_token.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kaggle

import (
"context"

"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/importer"
"github.com/1Password/shell-plugins/sdk/provision"
Expand Down Expand Up @@ -42,10 +44,38 @@ func APIToken() schema.CredentialType {
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"`
}
33 changes: 33 additions & 0 deletions plugins/kaggle/api_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,36 @@ func TestAPITokenProvisioner(t *testing.T) {
},
})
}

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",
},
},
},
})
}
1 change: 1 addition & 0 deletions plugins/kaggle/test-fixtures/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"username":"username","key":"z2pifkruzgbb17plmz2gux21fexample"}

0 comments on commit 91e933d

Please sign in to comment.