@@ -3,15 +3,13 @@ package civo
33import (
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
1715func 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
15351var 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
19594type 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}
0 commit comments