Skip to content

Commit 51f994c

Browse files
committed
civo plugin added
1 parent bb20d67 commit 51f994c

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed

plugins/civo/api_key.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package civo
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
7+
"github.com/1Password/shell-plugins/sdk"
8+
"github.com/1Password/shell-plugins/sdk/importer"
9+
"github.com/1Password/shell-plugins/sdk/provision"
10+
"github.com/1Password/shell-plugins/sdk/schema"
11+
"github.com/1Password/shell-plugins/sdk/schema/credname"
12+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
13+
)
14+
15+
func APIKey() schema.CredentialType {
16+
return schema.CredentialType{
17+
Name: credname.APIKey,
18+
DocsURL: sdk.URL("https://www.civo.com/docs/account/api-keys"),
19+
ManagementURL: sdk.URL("https://dashboard.civo.com/security"),
20+
Fields: []schema.CredentialField{
21+
{
22+
Name: fieldname.APIKey,
23+
MarkdownDescription: "API Key used to authenticate to Civo.",
24+
Secret: true,
25+
Composition: &schema.ValueComposition{
26+
Length: 50,
27+
Charset: schema.Charset{
28+
Uppercase: true,
29+
Lowercase: true,
30+
Digits: true,
31+
},
32+
},
33+
},
34+
{
35+
Name: fieldname.APIKeyID,
36+
MarkdownDescription: "API Name to identify the API Key.",
37+
},
38+
{
39+
Name: fieldname.DefaultRegion,
40+
MarkdownDescription: "The default region to use for this API Key.",
41+
Optional: true,
42+
},
43+
},
44+
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
45+
Importer: importer.TryAll(
46+
importer.TryEnvVarPair(defaultEnvVarMapping),
47+
TryCivoConfigFile(),
48+
)}
49+
}
50+
51+
var defaultEnvVarMapping = map[string]sdk.FieldName{
52+
"CIVO_API_KEY_NAME": fieldname.APIKeyID,
53+
"CIVO_API_KEY": fieldname.APIKey,
54+
}
55+
56+
func TryCivoConfigFile() sdk.Importer {
57+
58+
return importer.TryFile("~/.civo.json", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
59+
var config Config
60+
if err := contents.ToJSON(&config); err != nil {
61+
out.AddError(err)
62+
return
63+
64+
}
65+
if len(config.Properties) == 0 && config.Meta.CurrentAPIKey == "" {
66+
return
67+
}
68+
69+
var apiKey string
70+
for key, value := range config.Properties {
71+
if key == config.Meta.CurrentAPIKey {
72+
err := json.Unmarshal(value, &apiKey)
73+
if err != nil {
74+
out.AddError(err)
75+
return
76+
}
77+
}
78+
break
79+
}
80+
81+
out.AddCandidate(sdk.ImportCandidate{
82+
Fields: map[sdk.FieldName]string{
83+
fieldname.APIKey: apiKey,
84+
fieldname.APIKeyID: config.Meta.CurrentAPIKey,
85+
fieldname.DefaultRegion: config.Meta.DefaultRegion,
86+
},
87+
})
88+
89+
})
90+
}
91+
92+
// {
93+
// "apikeys": {
94+
// "newspidey": "Vdi1GHFqXLG47VcfdvfvfvfvfvfvfvfvEgOd",
95+
// },
96+
// "meta": {
97+
// "admin": false,
98+
// "current_apikey": "newspidey",
99+
// "default_region": "LON1",
100+
// "latest_release_check": "2023-06-11T20:25:06.916682112+05:30",
101+
// "url": "https://api.civo.com",
102+
// "last_command_executed": "2023-06-11T20:25:06.916237569+05:30"
103+
// }
104+
// }
105+
106+
type Config struct {
107+
Properties map[string]json.RawMessage `json:"apikeys"`
108+
109+
Meta struct {
110+
Admin bool `json:"admin"`
111+
CurrentAPIKey string `json:"current_apikey"`
112+
DefaultRegion string `json:"default_region"`
113+
LatestReleaseCheck string `json:"latest_release_check"`
114+
URL string `json:"url"`
115+
LastCommandExecuted string `json:"last_command_executed"`
116+
} `json:"meta"`
117+
}

plugins/civo/api_key_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package civo
2+
3+
import (
4+
"testing"
5+
6+
"github.com/1Password/shell-plugins/sdk"
7+
"github.com/1Password/shell-plugins/sdk/plugintest"
8+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
9+
)
10+
11+
func TestAPIKeyProvisioner(t *testing.T) {
12+
plugintest.TestProvisioner(t, APIKey().DefaultProvisioner, map[string]plugintest.ProvisionCase{
13+
"default": {
14+
ItemFields: map[sdk.FieldName]string{ // TODO: Check if this is correct
15+
fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
16+
},
17+
ExpectedOutput: sdk.ProvisionOutput{
18+
Environment: map[string]string{
19+
"CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
20+
},
21+
},
22+
},
23+
})
24+
}
25+
26+
func TestAPIKeyImporter(t *testing.T) {
27+
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
28+
"environment": {
29+
Environment: map[string]string{ // TODO: Check if this is correct
30+
"CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
31+
},
32+
ExpectedCandidates: []sdk.ImportCandidate{
33+
{
34+
Fields: map[sdk.FieldName]string{
35+
fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
36+
},
37+
},
38+
},
39+
},
40+
// TODO: If you implemented a config file importer, add a test file example in civo/test-fixtures
41+
// and fill the necessary details in the test template below.
42+
"config file": {
43+
Files: map[string]string{
44+
// "~/path/to/config.yml": plugintest.LoadFixture(t, "config.yml"),
45+
},
46+
ExpectedCandidates: []sdk.ImportCandidate{
47+
// {
48+
// Fields: map[sdk.FieldName]string{
49+
// fieldname.Token: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
50+
// },
51+
// },
52+
},
53+
},
54+
})
55+
}

plugins/civo/civo.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package civo
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/needsauth"
6+
"github.com/1Password/shell-plugins/sdk/schema"
7+
"github.com/1Password/shell-plugins/sdk/schema/credname"
8+
)
9+
10+
func CivoCLI() schema.Executable {
11+
return schema.Executable{
12+
Name: "Civo CLI", // TODO: Check if this is correct
13+
Runs: []string{"civo"},
14+
DocsURL: sdk.URL("https://civo.com/docs/cli"), // TODO: Replace with actual URL
15+
NeedsAuth: needsauth.IfAll(
16+
needsauth.NotForHelpOrVersion(),
17+
needsauth.NotWithoutArgs(),
18+
),
19+
Uses: []schema.CredentialUsage{
20+
{
21+
Name: credname.APIKey,
22+
},
23+
},
24+
}
25+
}

plugins/civo/plugin.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package civo
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/schema"
6+
)
7+
8+
func New() schema.Plugin {
9+
return schema.Plugin{
10+
Name: "civo",
11+
Platform: schema.PlatformInfo{
12+
Name: "Civo",
13+
Homepage: sdk.URL("https://civo.com"), // TODO: Check if this is correct
14+
},
15+
Credentials: []schema.CredentialType{
16+
APIKey(),
17+
},
18+
Executables: []schema.Executable{
19+
CivoCLI(),
20+
},
21+
}
22+
}

0 commit comments

Comments
 (0)