Skip to content

Commit 224dfdd

Browse files
committed
suggested changes added
1 parent c363432 commit 224dfdd

File tree

3 files changed

+32
-124
lines changed

3 files changed

+32
-124
lines changed

plugins/civo/api_key.go

Lines changed: 19 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ package civo
33
import (
44
"context"
55
"encoding/json"
6-
"os"
76

87
"github.com/1Password/shell-plugins/sdk"
98
"github.com/1Password/shell-plugins/sdk/importer"
109
"github.com/1Password/shell-plugins/sdk/provision"
1110
"github.com/1Password/shell-plugins/sdk/schema"
1211
"github.com/1Password/shell-plugins/sdk/schema/credname"
1312
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
14-
"github.com/mitchellh/go-homedir"
1513
)
1614

1715
func APIKey() schema.CredentialType {
@@ -43,114 +41,15 @@ func APIKey() schema.CredentialType {
4341
Optional: true,
4442
},
4543
},
46-
// DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
47-
DefaultProvisioner: provision.TempFile(configFile,
48-
provision.Filename(".civo.json"),
49-
provision.AddArgs(
50-
"--config", "{{.Path}}",
51-
),
52-
),
44+
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
5345
Importer: importer.TryAll(
5446
importer.TryEnvVarPair(defaultEnvVarMapping),
5547
TryCivoConfigFile(),
5648
)}
5749
}
58-
func configFile(in sdk.ProvisionInput) ([]byte, error) {
59-
apiKey := in.ItemFields[fieldname.APIKey]
60-
apiKeyID := in.ItemFields[fieldname.APIKeyID]
61-
defaultRegion := in.ItemFields[fieldname.DefaultRegion]
62-
63-
// Check if the file already exists
64-
// filePath := "~/.civo.json"
65-
// exists, err := fileExists(filePath)
66-
// if err != nil {
67-
// return nil, err
68-
// }
69-
70-
// if exists {
71-
// // Read the existing file
72-
// contents, err := ioutil.ReadFile(filePath)
73-
// if err != nil {
74-
// return nil, err
75-
// }
76-
77-
// var config Config
78-
// if err := json.Unmarshal(contents, &config); err != nil {
79-
// return nil, err
80-
// }
81-
82-
// // Update the config with the new values
83-
// config.Properties[apiKeyID] = json.RawMessage(`"` + apiKey + `"`)
84-
// config.Meta.CurrentAPIKey = apiKeyID
85-
// config.Meta.DefaultRegion = defaultRegion
86-
87-
// return json.MarshalIndent(config, "", " ")
88-
// }
89-
90-
// Create a new config
91-
config := Config{
92-
Properties: map[string]json.RawMessage{
93-
apiKeyID: json.RawMessage(`"` + apiKey + `"`),
94-
},
95-
Meta: struct {
96-
Admin bool `json:"admin"`
97-
CurrentAPIKey string `json:"current_apikey"`
98-
DefaultRegion string `json:"default_region"`
99-
LatestReleaseCheck string `json:"latest_release_check"`
100-
URL string `json:"url"`
101-
LastCommandExecuted string `json:"last_command_executed"`
102-
}{
103-
CurrentAPIKey: apiKeyID,
104-
DefaultRegion: defaultRegion,
105-
},
106-
}
107-
108-
return json.MarshalIndent(config, "", " ")
109-
}
110-
111-
func fileExists(filePath string) (bool, error) {
112-
expandedPath, err := homedir.Expand(filePath)
113-
if err != nil {
114-
return false, err
115-
}
116-
117-
info, err := os.Stat(expandedPath)
118-
if os.IsNotExist(err) {
119-
return false, nil
120-
}
121-
if err != nil {
122-
return false, err
123-
}
124-
125-
return !info.IsDir(), nil
126-
}
127-
128-
// func configFile(in sdk.ProvisionInput) ([]byte, error) {
129-
// apiKey := in.ItemFields[fieldname.APIKey]
130-
// apiKeyID := in.ItemFields[fieldname.APIKeyID]
131-
// defaultRegion := in.ItemFields[fieldname.DefaultRegion]
132-
133-
// config := Config{
134-
// Properties: map[string]json.RawMessage{
135-
// apiKeyID: json.RawMessage(`"` + apiKey + `"`),
136-
// },
137-
// Meta: struct {
138-
// Admin bool `json:"admin"`
139-
// CurrentAPIKey string `json:"current_apikey"`
140-
// DefaultRegion string `json:"default_region"`
141-
// LatestReleaseCheck string `json:"latest_release_check"`
142-
// URL string `json:"url"`
143-
// LastCommandExecuted string `json:"last_command_executed"`
144-
// }{
145-
// CurrentAPIKey: apiKeyID,
146-
// DefaultRegion: defaultRegion,
147-
// },
148-
// }
149-
150-
// return json.MarshalIndent(config, "", " ")
151-
// }
15250

15351
var defaultEnvVarMapping = map[string]sdk.FieldName{
52+
"CIVO_TOKEN": fieldname.APIKey,
15453
"CIVO_API_KEY_NAME": fieldname.APIKeyID,
15554
"CIVO_API_KEY": fieldname.APIKey,
15655
}
@@ -169,38 +68,34 @@ func TryCivoConfigFile() sdk.Importer {
16968
return
17069
}
17170

172-
var apiKey string
17371
for key, value := range config.Properties {
174-
if key == config.Meta.CurrentAPIKey {
175-
err := json.Unmarshal(value, &apiKey)
176-
if err != nil {
177-
out.AddError(err)
178-
return
179-
}
72+
var apiKey string
73+
74+
err := json.Unmarshal(value, &apiKey)
75+
if err != nil {
76+
out.AddError(err)
77+
return
18078
}
79+
out.AddCandidate(sdk.ImportCandidate{
80+
NameHint: key,
81+
Fields: map[sdk.FieldName]string{
82+
fieldname.APIKey: apiKey,
83+
fieldname.APIKeyID: config.Meta.CurrentAPIKey,
84+
fieldname.DefaultRegion: config.Meta.DefaultRegion,
85+
},
86+
})
87+
18188
break
18289
}
18390

184-
out.AddCandidate(sdk.ImportCandidate{
185-
Fields: map[sdk.FieldName]string{
186-
fieldname.APIKey: apiKey,
187-
fieldname.APIKeyID: config.Meta.CurrentAPIKey,
188-
fieldname.DefaultRegion: config.Meta.DefaultRegion,
189-
},
190-
})
191-
19291
})
19392
}
19493

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

19897
Meta struct {
199-
Admin bool `json:"admin"`
200-
CurrentAPIKey string `json:"current_apikey"`
201-
DefaultRegion string `json:"default_region"`
202-
LatestReleaseCheck string `json:"latest_release_check"`
203-
URL string `json:"url"`
204-
LastCommandExecuted string `json:"last_command_executed"`
98+
CurrentAPIKey string `json:"current_apikey"`
99+
DefaultRegion string `json:"default_region"`
205100
} `json:"meta"`
206101
}

plugins/civo/api_key_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func TestAPIKeyProvisioner(t *testing.T) {
1717
},
1818
ExpectedOutput: sdk.ProvisionOutput{
1919
Environment: map[string]string{
20+
"CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
2021
"CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
2122
"CIVO_API_KEY_NAME": "testdemoname",
2223
},
@@ -29,6 +30,7 @@ func TestAPIKeyImporter(t *testing.T) {
2930
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
3031
"environment": {
3132
Environment: map[string]string{
33+
"CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
3234
"CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
3335
"CIVO_API_KEY_NAME": "testdemoname",
3436
},
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"apikeys": {
3+
"newspidey": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE"
4+
},
5+
"meta": {
6+
7+
"current_apikey": "newspidey1",
8+
"default_region": "LON1"
9+
10+
}
11+
}

0 commit comments

Comments
 (0)