Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cmd/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ between them when required.`,

func apiKeyFind(search string) (string, error) {
var result string
for key, value := range config.Current.APIKeys {
if key == search || value == search {
result = key
for _, apiKey := range config.Current.APIKeys {
if apiKey.Name == search {
result = apiKey.Name
}
}

Expand All @@ -41,7 +41,6 @@ func apiKeyFind(search string) (string, error) {
}

func init() {

config.SkipAPIInitialization = true

APIKeyCmd.AddCommand(apikeyListCmd)
Expand Down
17 changes: 12 additions & 5 deletions cmd/apikey/apikey_current.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ var apikeyCurrentCmd = &cobra.Command{
Short: "Set the current API key",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
index, err := apiKeyFind(args[0])
apiKeyName, err := apiKeyFind(args[0])
if err != nil {
utility.Error("Unable to find the API key %s", err.Error())
os.Exit(1)
}

if index != "" {
config.Current.Meta.CurrentAPIKey = index
if apiKeyName != "" {
var idx int
for index, apiKey := range config.Current.APIKeys {
if apiKeyName == apiKey.Name {
idx = index
break
}
}
config.Current.Meta.CurrentAPIKey = config.Current.APIKeys[idx].Name
config.Current.Meta.URL = config.Current.APIKeys[idx].APIURL
config.SaveConfig()
fmt.Printf("Set the default API Key to be %s\n", utility.Green(index))
fmt.Printf("Set the default API Key to be %s\n", utility.Green(apiKeyName))
}

},
}
4 changes: 2 additions & 2 deletions cmd/apikey/apikey_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ If you wish to use a custom format, the available fields are:
Example: civo apikey ls -o custom -f "Name: Key"`,
Run: func(cmd *cobra.Command, args []string) {
keys := make([]string, 0, len(config.Current.APIKeys))
for k := range config.Current.APIKeys {
keys = append(keys, k)
for _, k := range config.Current.APIKeys {
keys = append(keys, k.Name)
}
sort.Strings(keys)

Expand Down
17 changes: 12 additions & 5 deletions cmd/apikey/apikey_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ var apikeyRemoveCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Example: "civo apikey remove NAME",
Run: func(cmd *cobra.Command, args []string) {
index, err := apiKeyFind(args[0])
apiKeyName, err := apiKeyFind(args[0])
if err != nil {
utility.Error("Unable to find the API key %s", err.Error())
os.Exit(1)
}

// Check if the requested API key is the current one
if index == config.Current.Meta.CurrentAPIKey {
if apiKeyName == config.Current.Meta.CurrentAPIKey {
if forceFlag {
utility.Warning("The API key %q is the current one. You are using the --force flag, so it will be deleted.", args[0])
} else {
Expand All @@ -37,18 +37,25 @@ var apikeyRemoveCmd = &cobra.Command{
// Confirm deletion of the API key
if forceFlag || utility.UserConfirmedDeletion("API key", common.DefaultYes, args[0]) {
numKeys := len(config.Current.APIKeys)
delete(config.Current.APIKeys, index)
var index int
for idx, apiKey := range config.Current.APIKeys {
if apiKey.Name == apiKeyName {
index = idx
break
}
}
// delete(config.Current.APIKeys, index)
config.Current.APIKeys = append(config.Current.APIKeys[:index], config.Current.APIKeys[index+1:]...)
config.SaveConfig()

if numKeys > len(config.Current.APIKeys) {
fmt.Printf("Removed the API Key %s\n", utility.Green(index))
fmt.Printf("Removed the API Key %s\n", utility.Green(apiKeyName))
} else {
utility.Error("The API Key %q couldn't be found", args[0])
os.Exit(1)
}
} else {
fmt.Println("Operation aborted.")
}

},
}
26 changes: 20 additions & 6 deletions cmd/apikey/apikey_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var apikeySaveCmdExample = `* Interactive way:
civo apikey save

* Non-interactive way:
civo apikey save NAME APIKEY
civo apikey save NAME APIKEY APIURL

* Load from environment variables way:
civo apikey save --load-from-env
Expand All @@ -43,8 +43,7 @@ var apikeySaveCmd = &cobra.Command{
Short: "Save a new API key",
Example: apikeySaveCmdExample,
Run: func(cmd *cobra.Command, args []string) {

var name, apiKey string
var name, apiKey, apiURL string
var err error

// if arg is more than two, return an error
Expand Down Expand Up @@ -79,7 +78,22 @@ var apikeySaveCmd = &cobra.Command{
utility.Error("Error reading api key %v", err)
os.Exit(1)
}
fmt.Println()
apiKey = string(apikeyBytes)
fmt.Printf("Enter the API URL key: (default is %s): ", config.DefaultAPIURL)
apiURL, err = reader.ReadString('\n')
if err != nil {
utility.Error("Error reading name %v", err)
os.Exit(1)
}
if runtime.GOOS == "windows" {
apiURL = strings.TrimSuffix(apiURL, "\r\n")
} else {
apiURL = strings.TrimSuffix(apiURL, "\n")
}
if len(apiURL) == 0 {
apiURL = config.DefaultAPIURL
}
}

if len(args) == 2 && !loadAPIKeyFromEnv {
Expand Down Expand Up @@ -110,9 +124,9 @@ var apikeySaveCmd = &cobra.Command{
apiKey = apiKeyEnv
}

config.Current.APIKeys[name] = apiKey
config.Current.APIKeys = append(config.Current.APIKeys, config.APIKey{Name: name, Value: apiKey, APIURL: apiURL})
if config.Current.Meta.DefaultRegion == "" {
client, err := civogo.NewClientWithURL(apiKey, config.Current.Meta.URL, "")
client, err := civogo.NewClientWithURL(apiKey, apiURL, "")
if err != nil {
utility.Error("Unable to create a Civo API client, please report this at https://github.com/civo/cli")
os.Exit(1)
Expand All @@ -129,6 +143,7 @@ var apikeySaveCmd = &cobra.Command{

if len(config.Current.APIKeys) == 1 {
config.Current.Meta.CurrentAPIKey = name
config.Current.Meta.URL = apiURL
config.SaveConfig()
}

Expand All @@ -142,6 +157,5 @@ var apikeySaveCmd = &cobra.Command{
default:
fmt.Printf("Saved the API Key %s\n", utility.Green(name))
}

},
}
24 changes: 16 additions & 8 deletions cmd/apikey/apikey_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,34 @@ If you wish to use a custom format, the available fields are:
`,
Run: func(cmd *cobra.Command, args []string) {
keys := make([]string, 0, len(config.Current.APIKeys))
for k := range config.Current.APIKeys {
keys = append(keys, k)
for _, apiKey := range config.Current.APIKeys {
keys = append(keys, apiKey.Name)
}
sort.Strings(keys)

ow := utility.NewOutputWriter()

for _, name := range keys {
apiKey := config.Current.APIKeys[name]
var idx int
for index, apiKey := range config.Current.APIKeys {
if name == apiKey.Name {
idx = index
break
}
}
apiKey := config.Current.APIKeys[idx]
if len(args) > 0 {
if strings.Contains(name, args[0]) {
ow.StartLine()
ow.AppendDataWithLabel("name", name, "Name")
ow.AppendDataWithLabel("key", apiKey, "Key")
ow.AppendDataWithLabel("name", apiKey.Name, "Name")
ow.AppendDataWithLabel("key", apiKey.Value, "Key")
ow.AppendDataWithLabel("url", apiKey.APIURL, "APIURL")
}
} else {
if config.Current.Meta.CurrentAPIKey == name {
ow.StartLine()
ow.AppendDataWithLabel("name", name, "Name")
ow.AppendDataWithLabel("key", apiKey, "Key")
ow.AppendDataWithLabel("name", apiKey.Name, "Name")
ow.AppendDataWithLabel("key", apiKey.Value, "Key")
ow.AppendDataWithLabel("url", apiKey.APIURL, "APIURL")
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"github.com/spf13/cobra"
)

var firewallID, networkID, size, updatedName, software, softwareVersion string
var nodes int
var (
firewallID, networkID, size, updatedName, software, softwareVersion string
nodes int
)

// DBCmd is the root command for the db subcommand
var DBCmd = &cobra.Command{
Expand Down
6 changes: 4 additions & 2 deletions cmd/database/database_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"github.com/spf13/cobra"
)

var name, schedule, backupType string
var scheduled bool
var (
name, schedule, backupType string
scheduled bool
)

// dbBackupCmd is the root command for the db backup subcommand
var dbBackupCmd = &cobra.Command{
Expand Down
Loading
Loading