Skip to content

Commit

Permalink
feat(Volume): Added the volume cmd
Browse files Browse the repository at this point in the history
- Added the possibility the you can list, create, attach, detach and delete a volume
- Add a utility, which gives the possibility to request confirmation when deleting a resource
- Modified all cmd to delete, to add confirmation

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed May 8, 2020
1 parent 775885d commit 6a84a03
Show file tree
Hide file tree
Showing 20 changed files with 681 additions and 182 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ Golang has no shortage of external libraries for various parts of this, but the
-~~Load balancers~~
-~~SSH keys~~
-~~Networks~~
- Snapshots
- Volumes
- ~~Snapshots~~
- ~~Volumes~~
- Templates

- Kubernetes Clusters
- Kubernetes Applications

Expand Down
20 changes: 13 additions & 7 deletions cmd/apikey_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/civo/cli/utility"
"os"

"github.com/civo/cli/config"
Expand All @@ -21,15 +22,20 @@ var apikeyRemoveCmd = &cobra.Command{
os.Exit(1)
}

numKeys := len(config.Current.APIKeys)
delete(config.Current.APIKeys, index)
config.SaveConfig()
if utility.AskForConfirmDelete("api key") == nil {
numKeys := len(config.Current.APIKeys)
delete(config.Current.APIKeys, index)
config.SaveConfig()

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

},
}
43 changes: 24 additions & 19 deletions cmd/domain_record_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,34 @@ var domainRecordRemoveCmd = &cobra.Command{
os.Exit(1)
}

domain, err := client.FindDNSDomain(args[0])
if err != nil {
fmt.Printf("Unable to find domain for your search: %s\n", aurora.Red(err))
os.Exit(1)
}
if utility.AskForConfirmDelete("domain record") == nil {
domain, err := client.FindDNSDomain(args[0])
if err != nil {
fmt.Printf("Unable to find domain for your search: %s\n", aurora.Red(err))
os.Exit(1)
}

record, err := client.GetDNSRecord(domain.ID, args[1])
if err != nil {
fmt.Printf("Unable to get domains record: %s\n", aurora.Red(err))
os.Exit(1)
}
record, err := client.GetDNSRecord(domain.ID, args[1])
if err != nil {
fmt.Printf("Unable to get domains record: %s\n", aurora.Red(err))
os.Exit(1)
}

_, err = client.DeleteDNSRecord(record)
_, err = client.DeleteDNSRecord(record)

ow := utility.NewOutputWriterWithMap(map[string]string{"ID": record.ID, "Name": record.Name})
ow := utility.NewOutputWriterWithMap(map[string]string{"ID": record.ID, "Name": record.Name})

switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The domain record called %s with ID %s was delete\n", aurora.Green(record.Name), aurora.Green(record.ID))
switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The domain record called %s with ID %s was delete\n", aurora.Green(record.Name), aurora.Green(record.ID))
}
} else {
fmt.Println("Operation aborted.")
}

},
}
32 changes: 18 additions & 14 deletions cmd/domain_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ var domainRemoveCmd = &cobra.Command{
os.Exit(1)
}

domain, err := client.FindDNSDomain(args[0])
if err != nil {
fmt.Printf("Unable to find domain for your search: %s\n", aurora.Red(err))
os.Exit(1)
}
if utility.AskForConfirmDelete("domain") == nil {
domain, err := client.FindDNSDomain(args[0])
if err != nil {
fmt.Printf("Unable to find domain for your search: %s\n", aurora.Red(err))
os.Exit(1)
}

_, err = client.DeleteDNSDomain(domain)
_, err = client.DeleteDNSDomain(domain)

ow := utility.NewOutputWriterWithMap(map[string]string{"ID": domain.ID, "Name": domain.Name})
ow := utility.NewOutputWriterWithMap(map[string]string{"ID": domain.ID, "Name": domain.Name})

switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The domain called %s with ID %s was delete\n", aurora.Green(domain.Name), aurora.Green(domain.ID))
switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The domain called %s with ID %s was delete\n", aurora.Green(domain.Name), aurora.Green(domain.ID))
}
} else {
fmt.Println("Operation aborted.")
}
},
}
32 changes: 18 additions & 14 deletions cmd/firewall_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ var firewallRemoveCmd = &cobra.Command{
os.Exit(1)
}

firewall, err := client.FindFirewall(args[0])
if err != nil {
fmt.Printf("Unable to find firewall for your search: %s\n", aurora.Red(err))
os.Exit(1)
}
if utility.AskForConfirmDelete("firewall") == nil {
firewall, err := client.FindFirewall(args[0])
if err != nil {
fmt.Printf("Unable to find firewall for your search: %s\n", aurora.Red(err))
os.Exit(1)
}

_, err = client.DeleteFirewall(firewall.ID)
_, err = client.DeleteFirewall(firewall.ID)

ow := utility.NewOutputWriterWithMap(map[string]string{"ID": firewall.ID, "Name": firewall.Name})
ow := utility.NewOutputWriterWithMap(map[string]string{"ID": firewall.ID, "Name": firewall.Name})

switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The firewall called %s with ID %s was delete\n", aurora.Green(firewall.Name), aurora.Green(firewall.ID))
switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The firewall %s with ID %s was delete\n", aurora.Green(firewall.Name), aurora.Green(firewall.ID))
}
} else {
fmt.Println("Operation aborted.")
}
},
}
43 changes: 24 additions & 19 deletions cmd/firewall_rule_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,34 @@ var firewallRuleRemoveCmd = &cobra.Command{
os.Exit(1)
}

firewall, err := client.FindFirewall(args[0])
if err != nil {
fmt.Printf("Unable to find the firewall for your search: %s\n", aurora.Red(err))
os.Exit(1)
}
if utility.AskForConfirmDelete("firewall") == nil {
firewall, err := client.FindFirewall(args[0])
if err != nil {
fmt.Printf("Unable to find the firewall for your search: %s\n", aurora.Red(err))
os.Exit(1)
}

rule, err := client.FindFirewallRule(firewall.ID, args[1])
if err != nil {
fmt.Printf("Unable to find the firewall rule: %s\n", aurora.Red(err))
os.Exit(1)
}
rule, err := client.FindFirewallRule(firewall.ID, args[1])
if err != nil {
fmt.Printf("Unable to find the firewall rule: %s\n", aurora.Red(err))
os.Exit(1)
}

_, err = client.DeleteFirewallRule(firewall.ID, rule.ID)
_, err = client.DeleteFirewallRule(firewall.ID, rule.ID)

ow := utility.NewOutputWriterWithMap(map[string]string{"ID": rule.ID, "Label": rule.Label})
ow := utility.NewOutputWriterWithMap(map[string]string{"ID": rule.ID, "Label": rule.Label})

switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The Firewall rule %s with ID %s was delete\n", aurora.Green(rule.Label), aurora.Green(rule.ID))
switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The firewall rule %s with ID %s was delete\n", aurora.Green(rule.Label), aurora.Green(rule.ID))
}
} else {
fmt.Println("Operation aborted.")
}

},
}
44 changes: 24 additions & 20 deletions cmd/instance_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,34 @@ Example: civo instance remove ID/NAME`,
os.Exit(1)
}

instance, err := client.FindInstance(args[0])
if err != nil {
fmt.Printf("Finding instance: %s\n", aurora.Red(err))
os.Exit(1)
}
if utility.AskForConfirmDelete("instance") == nil {
instance, err := client.FindInstance(args[0])
if err != nil {
fmt.Printf("Finding instance: %s\n", aurora.Red(err))
os.Exit(1)
}

_, err = client.DeleteInstance(instance.ID)
if err != nil {
fmt.Printf("Removing instance: %s\n", aurora.Red(err))
os.Exit(1)
}
_, err = client.DeleteInstance(instance.ID)
if err != nil {
fmt.Printf("Removing instance: %s\n", aurora.Red(err))
os.Exit(1)
}

if outputFormat == "human" {
fmt.Printf("The instance %s (%s) has been removed\n", aurora.Green(instance.Hostname), instance.ID)
} else {
ow := utility.NewOutputWriter()
ow.StartLine()
ow.AppendData("ID", instance.ID)
ow.AppendData("Hostname", instance.Hostname)
if outputFormat == "json" {
ow.WriteSingleObjectJSON()
if outputFormat == "human" {
fmt.Printf("The instance %s (%s) has been removed\n", aurora.Green(instance.Hostname), instance.ID)
} else {
ow.WriteCustomOutput(outputFields)
ow := utility.NewOutputWriter()
ow.StartLine()
ow.AppendData("ID", instance.ID)
ow.AppendData("Hostname", instance.Hostname)
if outputFormat == "json" {
ow.WriteSingleObjectJSON()
} else {
ow.WriteCustomOutput(outputFields)
}
}
} else {
fmt.Println("Operation aborted.")
}
},
}
32 changes: 18 additions & 14 deletions cmd/loadbalancer_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ var loadBalancerRemoveCmd = &cobra.Command{
os.Exit(1)
}

lb, err := client.FindLoadBalancer(args[0])
if err != nil {
fmt.Printf("Unable to find load balancer for your search: %s\n", aurora.Red(err))
os.Exit(1)
}
if utility.AskForConfirmDelete("load balancer") == nil {
lb, err := client.FindLoadBalancer(args[0])
if err != nil {
fmt.Printf("Unable to find load balancer for your search: %s\n", aurora.Red(err))
os.Exit(1)
}

_, err = client.DeleteLoadBalancer(lb.ID)
_, err = client.DeleteLoadBalancer(lb.ID)

ow := utility.NewOutputWriterWithMap(map[string]string{"ID": lb.ID, "Hostname": lb.Hostname})
ow := utility.NewOutputWriterWithMap(map[string]string{"ID": lb.ID, "Hostname": lb.Hostname})

switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The load blancer %s with ID %s was delete\n", aurora.Green(lb.Hostname), aurora.Green(lb.ID))
switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The load blancer %s with ID %s was delete\n", aurora.Green(lb.Hostname), aurora.Green(lb.ID))
}
} else {
fmt.Println("Operation aborted.")
}
},
}
33 changes: 19 additions & 14 deletions cmd/network_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,28 @@ var networkRemoveCmd = &cobra.Command{
os.Exit(1)
}

network, err := client.FindNetwork(args[0])
if err != nil {
fmt.Printf("Unable to find network for your search: %s\n", aurora.Red(err))
os.Exit(1)
}
if utility.AskForConfirmDelete("network") == nil {
network, err := client.FindNetwork(args[0])
if err != nil {
fmt.Printf("Unable to find network for your search: %s\n", aurora.Red(err))
os.Exit(1)
}

_, err = client.DeleteNetwork(network.ID)
_, err = client.DeleteNetwork(network.ID)

ow := utility.NewOutputWriterWithMap(map[string]string{"ID": network.ID, "Name": network.Name, "Label": network.Label})
ow := utility.NewOutputWriterWithMap(map[string]string{"ID": network.ID, "Name": network.Name, "Label": network.Label})

switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The network called %s with ID %s was delete\n", aurora.Green(network.Label), aurora.Green(network.ID))
switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The network called %s with ID %s was delete\n", aurora.Green(network.Label), aurora.Green(network.ID))
}
} else {
fmt.Println("Operation aborted.")
}

},
}
Loading

0 comments on commit 6a84a03

Please sign in to comment.