Skip to content

Commit

Permalink
suggested changes added
Browse files Browse the repository at this point in the history
  • Loading branch information
itsCheithanya committed Jun 24, 2023
1 parent c363432 commit 224dfdd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 124 deletions.
143 changes: 19 additions & 124 deletions plugins/civo/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package civo
import (
"context"
"encoding/json"
"os"

"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"
"github.com/mitchellh/go-homedir"
)

func APIKey() schema.CredentialType {
Expand Down Expand Up @@ -43,114 +41,15 @@ func APIKey() schema.CredentialType {
Optional: true,
},
},
// DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
DefaultProvisioner: provision.TempFile(configFile,
provision.Filename(".civo.json"),
provision.AddArgs(
"--config", "{{.Path}}",
),
),
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryCivoConfigFile(),
)}
}
func configFile(in sdk.ProvisionInput) ([]byte, error) {
apiKey := in.ItemFields[fieldname.APIKey]
apiKeyID := in.ItemFields[fieldname.APIKeyID]
defaultRegion := in.ItemFields[fieldname.DefaultRegion]

// Check if the file already exists
// filePath := "~/.civo.json"
// exists, err := fileExists(filePath)
// if err != nil {
// return nil, err
// }

// if exists {
// // Read the existing file
// contents, err := ioutil.ReadFile(filePath)
// if err != nil {
// return nil, err
// }

// var config Config
// if err := json.Unmarshal(contents, &config); err != nil {
// return nil, err
// }

// // Update the config with the new values
// config.Properties[apiKeyID] = json.RawMessage(`"` + apiKey + `"`)
// config.Meta.CurrentAPIKey = apiKeyID
// config.Meta.DefaultRegion = defaultRegion

// return json.MarshalIndent(config, "", " ")
// }

// Create a new config
config := Config{
Properties: map[string]json.RawMessage{
apiKeyID: json.RawMessage(`"` + apiKey + `"`),
},
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"`
}{
CurrentAPIKey: apiKeyID,
DefaultRegion: defaultRegion,
},
}

return json.MarshalIndent(config, "", " ")
}

func fileExists(filePath string) (bool, error) {
expandedPath, err := homedir.Expand(filePath)
if err != nil {
return false, err
}

info, err := os.Stat(expandedPath)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}

return !info.IsDir(), nil
}

// func configFile(in sdk.ProvisionInput) ([]byte, error) {
// apiKey := in.ItemFields[fieldname.APIKey]
// apiKeyID := in.ItemFields[fieldname.APIKeyID]
// defaultRegion := in.ItemFields[fieldname.DefaultRegion]

// config := Config{
// Properties: map[string]json.RawMessage{
// apiKeyID: json.RawMessage(`"` + apiKey + `"`),
// },
// 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"`
// }{
// CurrentAPIKey: apiKeyID,
// DefaultRegion: defaultRegion,
// },
// }

// return json.MarshalIndent(config, "", " ")
// }

var defaultEnvVarMapping = map[string]sdk.FieldName{
"CIVO_TOKEN": fieldname.APIKey,
"CIVO_API_KEY_NAME": fieldname.APIKeyID,
"CIVO_API_KEY": fieldname.APIKey,
}
Expand All @@ -169,38 +68,34 @@ func TryCivoConfigFile() sdk.Importer {
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
}
var apiKey string

err := json.Unmarshal(value, &apiKey)
if err != nil {
out.AddError(err)
return
}
out.AddCandidate(sdk.ImportCandidate{
NameHint: key,
Fields: map[sdk.FieldName]string{
fieldname.APIKey: apiKey,
fieldname.APIKeyID: config.Meta.CurrentAPIKey,
fieldname.DefaultRegion: config.Meta.DefaultRegion,
},
})

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"`
CurrentAPIKey string `json:"current_apikey"`
DefaultRegion string `json:"default_region"`
} `json:"meta"`
}
2 changes: 2 additions & 0 deletions plugins/civo/api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestAPIKeyProvisioner(t *testing.T) {
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
"CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
"CIVO_API_KEY_NAME": "testdemoname",
},
Expand All @@ -29,6 +30,7 @@ func TestAPIKeyImporter(t *testing.T) {
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
"environment": {
Environment: map[string]string{
"CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
"CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
"CIVO_API_KEY_NAME": "testdemoname",
},
Expand Down
11 changes: 11 additions & 0 deletions plugins/civo/test-fixtures/.civo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"apikeys": {
"newspidey": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE"
},
"meta": {

"current_apikey": "newspidey1",
"default_region": "LON1"

}
}

0 comments on commit 224dfdd

Please sign in to comment.