-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb20d67
commit 8941afc
Showing
5 changed files
with
220 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,105 @@ | ||
package civo | ||
|
||
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://www.civo.com/docs/account/api-keys"), | ||
ManagementURL: sdk.URL("https://dashboard.civo.com/security"), | ||
Fields: []schema.CredentialField{ | ||
{ | ||
Name: fieldname.APIKey, | ||
MarkdownDescription: "API Key used to authenticate to Civo.", | ||
Secret: true, | ||
Composition: &schema.ValueComposition{ | ||
Length: 50, | ||
Charset: schema.Charset{ | ||
Uppercase: true, | ||
Lowercase: true, | ||
Digits: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: fieldname.APIKeyID, | ||
MarkdownDescription: "API Name to identify the API Key.", | ||
}, | ||
{ | ||
Name: fieldname.DefaultRegion, | ||
MarkdownDescription: "The default region to use for this API Key.", | ||
Optional: true, | ||
}, | ||
}, | ||
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), | ||
|
||
Importer: importer.TryAll( | ||
importer.TryEnvVarPair(defaultEnvVarMapping), | ||
TryCivoConfigFile(), | ||
)} | ||
} | ||
|
||
var defaultEnvVarMapping = map[string]sdk.FieldName{ | ||
"CIVO_API_KEY_NAME": fieldname.APIKeyID, | ||
"CIVO_API_KEY": fieldname.APIKey, | ||
} | ||
|
||
func TryCivoConfigFile() sdk.Importer { | ||
|
||
return importer.TryFile("~/.civo.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 len(config.Properties) == 0 && config.Meta.CurrentAPIKey == "" { | ||
return | ||
} | ||
|
||
var apiKey string | ||
for key, value := range config.Properties { | ||
if key == config.Meta.CurrentAPIKey { | ||
err := json.Unmarshal(value, &apiKey) | ||
if err != nil { | ||
out.AddError(err) | ||
return | ||
} | ||
} | ||
break | ||
} | ||
|
||
out.AddCandidate(sdk.ImportCandidate{ | ||
Fields: map[sdk.FieldName]string{ | ||
fieldname.APIKey: apiKey, | ||
fieldname.APIKeyID: config.Meta.CurrentAPIKey, | ||
fieldname.DefaultRegion: config.Meta.DefaultRegion, | ||
}, | ||
}) | ||
|
||
}) | ||
} | ||
|
||
type Config struct { | ||
Properties map[string]json.RawMessage `json:"apikeys"` | ||
|
||
Meta struct { | ||
Admin bool `json:"admin"` | ||
CurrentAPIKey string `json:"current_apikey"` | ||
DefaultRegion string `json:"default_region"` | ||
LatestReleaseCheck string `json:"latest_release_check"` | ||
URL string `json:"url"` | ||
LastCommandExecuted string `json:"last_command_executed"` | ||
} `json:"meta"` | ||
} |
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,55 @@ | ||
package civo | ||
|
||
import ( | ||
"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{ // TODO: Check if this is correct | ||
fieldname.APIKey: "amqsmbexample", | ||
}, | ||
ExpectedOutput: sdk.ProvisionOutput{ | ||
Environment: map[string]string{ | ||
"CIVO_API_KEY": "amqsmbexample", | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAPIKeyImporter(t *testing.T) { | ||
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{ | ||
"environment": { | ||
Environment: map[string]string{ // TODO: Check if this is correct | ||
"CIVO_API_KEY": "amqsmbexample", | ||
}, | ||
ExpectedCandidates: []sdk.ImportCandidate{ | ||
{ | ||
Fields: map[sdk.FieldName]string{ | ||
fieldname.APIKey: "amqsmbexample", | ||
}, | ||
}, | ||
}, | ||
}, | ||
// TODO: If you implemented a config file importer, add a test file example in civo/test-fixtures | ||
// and fill the necessary details in the test template below. | ||
"config file": { | ||
Files: map[string]string{ | ||
// "~/path/to/config.yml": plugintest.LoadFixture(t, "config.yml"), | ||
}, | ||
ExpectedCandidates: []sdk.ImportCandidate{ | ||
// { | ||
// Fields: map[sdk.FieldName]string{ | ||
// fieldname.Token: "amqsmbexample", | ||
// }, | ||
// }, | ||
}, | ||
}, | ||
}) | ||
} |
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 civo | ||
|
||
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 CivoCLI() schema.Executable { | ||
return schema.Executable{ | ||
Name: "Civo CLI", | ||
Runs: []string{"civo"}, | ||
DocsURL: sdk.URL("https://civo.com/docs/cli"), | ||
NeedsAuth: needsauth.IfAll( | ||
needsauth.NotForHelpOrVersion(), | ||
needsauth.NotWithoutArgs(), | ||
), | ||
Uses: []schema.CredentialUsage{ | ||
{ | ||
Name: credname.APIKey, | ||
}, | ||
}, | ||
} | ||
} |
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 civo | ||
|
||
import ( | ||
"github.com/1Password/shell-plugins/sdk" | ||
"github.com/1Password/shell-plugins/sdk/schema" | ||
) | ||
|
||
func New() schema.Plugin { | ||
return schema.Plugin{ | ||
Name: "civo", | ||
Platform: schema.PlatformInfo{ | ||
Name: "Civo", | ||
Homepage: sdk.URL("https://civo.com"), | ||
}, | ||
Credentials: []schema.CredentialType{ | ||
APIKey(), | ||
}, | ||
Executables: []schema.Executable{ | ||
CivoCLI(), | ||
}, | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"apikeys": { | ||
"newspidey": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE" | ||
}, | ||
"meta": { | ||
"admin": false, | ||
"current_apikey": "newspidey1", | ||
"default_region": "LON1", | ||
"latest_release_check": "2023-06-11T20:25:06.916682112+05:30", | ||
"url": "https://api.civo.com", | ||
"last_command_executed": "2023-06-11T20:25:06.916237569+05:30" | ||
} | ||
} |