Skip to content

Commit

Permalink
files modified
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhikhapare committed Jun 29, 2023
1 parent 0304366 commit 37ecffe
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 48 deletions.
49 changes: 16 additions & 33 deletions plugins/civo/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package civo

import (
"context"
"encoding/json"

"github.com/1Password/shell-plugins/sdk"

Check failure on line 5 in plugins/civo/api_key.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofmt`-ed with `-s` (gofmt)
"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"
Expand Down Expand Up @@ -33,69 +32,53 @@ func APIKey() schema.CredentialType {
},
{
Name: fieldname.APIKeyID,
MarkdownDescription: "API Name to identify the API Key.",
},
{
Name: fieldname.DefaultRegion,
MarkdownDescription: "The default region to use for this API Key.",
MarkdownDescription: "The Name of apikey used to authenticate to civo",
Optional: true,
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryCivoConfigFile(),
importer.TryEnvVarPair(secondEnvVarMapping),
TryCivoConfigFile("~/.civo.json"),
)}
}

var defaultEnvVarMapping = map[string]sdk.FieldName{
"CIVO_TOKEN": fieldname.APIKey,
"CIVO_API_KEY_NAME": fieldname.APIKeyID,
"CIVO_API_KEY": fieldname.APIKey,
//"CIVO_API_KEY_NAME": fieldname.APIKeyID,
}

var secondEnvVarMapping = map[string]sdk.FieldName{
"CIVO_API_KEY" : fieldname.APIKey,
"CIVO_API_KEY_NAME": fieldname.APIKeyID,
}

func TryCivoConfigFile() sdk.Importer {
func TryCivoConfigFile(path string) sdk.Importer {

return importer.TryFile("~/.civo.json", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
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 len(config.Properties) == 0 && config.Meta.CurrentAPIKey == "" {
if len(config.Properties) == 0 {
return
}

for key, value := range config.Properties {
var apiKey string

err := json.Unmarshal(value, &apiKey)
if err != nil {
out.AddError(err)
return
}
out.AddCandidate(sdk.ImportCandidate{
NameHint: key,
Fields: map[sdk.FieldName]string{
fieldname.APIKey: apiKey,
fieldname.APIKeyID: config.Meta.CurrentAPIKey,
fieldname.DefaultRegion: config.Meta.DefaultRegion,
fieldname.APIKey: value,
},
})

break
}

})
}

type Config struct {
Properties map[string]json.RawMessage `json:"apikeys"`

Meta struct {
CurrentAPIKey string `json:"current_apikey"`
DefaultRegion string `json:"default_region"`
} `json:"meta"`
Properties map[string]string `json:"apikeys"`
}
4 changes: 0 additions & 4 deletions plugins/civo/api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ func TestAPIKeyProvisioner(t *testing.T) {
"default": {
ItemFields: map[sdk.FieldName]string{
fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",

Check failure on line 15 in plugins/civo/api_key_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofmt`-ed with `-s` (gofmt)
fieldname.APIKeyID: "testdemoname",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
"CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
"CIVO_API_KEY_NAME": "testdemoname",
},
},
},
Expand Down Expand Up @@ -55,7 +52,6 @@ func TestAPIKeyImporter(t *testing.T) {
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
fieldname.APIKeyID: "testdemoname",
fieldname.DefaultRegion: "LON1",
},
},
},
Expand Down
5 changes: 3 additions & 2 deletions plugins/civo/civo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ func CivoCLI() schema.Executable {
return schema.Executable{
Name: "Civo CLI",
Runs: []string{"civo"},
DocsURL: sdk.URL("https://civo.com/docs/cli"),
DocsURL: sdk.URL("https://www.civo.com/docs/overview/civo-cli"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
needsauth.NotWhenContainsArgs("update"),
needsauth.NotForExactArgs("config"),
needsauth.NotWhenContainsArgs("apikey", "save"),
),
Uses: []schema.CredentialUsage{
{
Expand Down
2 changes: 1 addition & 1 deletion plugins/civo/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func New() schema.Plugin {
Name: "civo",
Platform: schema.PlatformInfo{
Name: "Civo",
Homepage: sdk.URL("https://civo.com"),
Homepage: sdk.URL("https://www.civo.com"),
},
Credentials: []schema.CredentialType{
APIKey(),
Expand Down
10 changes: 2 additions & 8 deletions plugins/civo/test-fixtures/.civo.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
{
"apikeys": {
"newspidey": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE"
},
"meta": {

"current_apikey": "newspidey1",
"default_region": "LON1"

"testdemoname": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE"
}
}
}

0 comments on commit 37ecffe

Please sign in to comment.