@@ -3,13 +3,16 @@ package civo
33import (
44 "context"
55 "encoding/json"
6+ "io/ioutil"
7+ "os"
68
79 "github.com/1Password/shell-plugins/sdk"
810 "github.com/1Password/shell-plugins/sdk/importer"
911 "github.com/1Password/shell-plugins/sdk/provision"
1012 "github.com/1Password/shell-plugins/sdk/schema"
1113 "github.com/1Password/shell-plugins/sdk/schema/credname"
1214 "github.com/1Password/shell-plugins/sdk/schema/fieldname"
15+ "github.com/mitchellh/go-homedir"
1316)
1417
1518func APIKey () schema.CredentialType {
@@ -41,12 +44,113 @@ func APIKey() schema.CredentialType {
4144 Optional : true ,
4245 },
4346 },
44- DefaultProvisioner : provision .EnvVars (defaultEnvVarMapping ),
47+ // DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
48+ DefaultProvisioner : provision .TempFile (configFile ,
49+ provision .Filename (".civo.json" ),
50+ provision .AddArgs (
51+ "--config" , "{{.Path}}" ,
52+ ),
53+ provision .AtFixedPath ("~/.civo.json" ),
54+ ),
4555 Importer : importer .TryAll (
4656 importer .TryEnvVarPair (defaultEnvVarMapping ),
4757 TryCivoConfigFile (),
4858 )}
4959}
60+ func configFile (in sdk.ProvisionInput ) ([]byte , error ) {
61+ apiKey := in .ItemFields [fieldname .APIKey ]
62+ apiKeyID := in .ItemFields [fieldname .APIKeyID ]
63+ defaultRegion := in .ItemFields [fieldname .DefaultRegion ]
64+
65+ // Check if the file already exists
66+ filePath := "~/.civo.json"
67+ exists , err := fileExists (filePath )
68+ if err != nil {
69+ return nil , err
70+ }
71+
72+ if exists {
73+ // Read the existing file
74+ contents , err := ioutil .ReadFile (filePath )
75+ if err != nil {
76+ return nil , err
77+ }
78+
79+ var config Config
80+ if err := json .Unmarshal (contents , & config ); err != nil {
81+ return nil , err
82+ }
83+
84+ // Update the config with the new values
85+ config .Properties [apiKeyID ] = json .RawMessage (`"` + apiKey + `"` )
86+ config .Meta .CurrentAPIKey = apiKeyID
87+ config .Meta .DefaultRegion = defaultRegion
88+
89+ return json .MarshalIndent (config , "" , " " )
90+ }
91+
92+ // Create a new config
93+ config := Config {
94+ Properties : map [string ]json.RawMessage {
95+ apiKeyID : json .RawMessage (`"` + apiKey + `"` ),
96+ },
97+ Meta : struct {
98+ Admin bool `json:"admin"`
99+ CurrentAPIKey string `json:"current_apikey"`
100+ DefaultRegion string `json:"default_region"`
101+ LatestReleaseCheck string `json:"latest_release_check"`
102+ URL string `json:"url"`
103+ LastCommandExecuted string `json:"last_command_executed"`
104+ }{
105+ CurrentAPIKey : apiKeyID ,
106+ DefaultRegion : defaultRegion ,
107+ },
108+ }
109+
110+ return json .MarshalIndent (config , "" , " " )
111+ }
112+
113+ func fileExists (filePath string ) (bool , error ) {
114+ expandedPath , err := homedir .Expand (filePath )
115+ if err != nil {
116+ return false , err
117+ }
118+
119+ info , err := os .Stat (expandedPath )
120+ if os .IsNotExist (err ) {
121+ return false , nil
122+ }
123+ if err != nil {
124+ return false , err
125+ }
126+
127+ return ! info .IsDir (), nil
128+ }
129+
130+ // func configFile(in sdk.ProvisionInput) ([]byte, error) {
131+ // apiKey := in.ItemFields[fieldname.APIKey]
132+ // apiKeyID := in.ItemFields[fieldname.APIKeyID]
133+ // defaultRegion := in.ItemFields[fieldname.DefaultRegion]
134+
135+ // config := Config{
136+ // Properties: map[string]json.RawMessage{
137+ // apiKeyID: json.RawMessage(`"` + apiKey + `"`),
138+ // },
139+ // Meta: struct {
140+ // Admin bool `json:"admin"`
141+ // CurrentAPIKey string `json:"current_apikey"`
142+ // DefaultRegion string `json:"default_region"`
143+ // LatestReleaseCheck string `json:"latest_release_check"`
144+ // URL string `json:"url"`
145+ // LastCommandExecuted string `json:"last_command_executed"`
146+ // }{
147+ // CurrentAPIKey: apiKeyID,
148+ // DefaultRegion: defaultRegion,
149+ // },
150+ // }
151+
152+ // return json.MarshalIndent(config, "", " ")
153+ // }
50154
51155var defaultEnvVarMapping = map [string ]sdk.FieldName {
52156 "CIVO_API_KEY_NAME" : fieldname .APIKeyID ,
@@ -62,6 +166,7 @@ func TryCivoConfigFile() sdk.Importer {
62166 return
63167
64168 }
169+
65170 if len (config .Properties ) == 0 && config .Meta .CurrentAPIKey == "" {
66171 return
67172 }
0 commit comments