Skip to content

Commit

Permalink
Add output customization to create commands
Browse files Browse the repository at this point in the history
This change adds support for output customization through the use of
global `--output-format` flag to all `create|add` commands.
  • Loading branch information
Marc Falzon authored and falzm committed Mar 18, 2020
1 parent dcf4558 commit b889ad2
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 173 deletions.
36 changes: 19 additions & 17 deletions cmd/affinitygroup_create.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package cmd

import (
"os"
"fmt"
"strings"

"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)

// createCmd represents the create command
var affinitygroupCreateCmd = &cobra.Command{
Use: "create <name>",
Short: "Create affinity group",
Use: "create <name>",
Short: "Create Anti-Affinity Group",
Long: fmt.Sprintf(`This command creates an Anti-Affinity Group.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&affinityGroupShowOutput{}), ", ")),
Aliases: gCreateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
if len(args) != 1 {
return cmd.Usage()
}

Expand All @@ -23,26 +26,25 @@ var affinitygroupCreateCmd = &cobra.Command{
return err
}

return createAffinityGroup(args[0], desc)
return output(createAffinityGroup(args[0], desc))
},
}

func createAffinityGroup(name, desc string) error {
resp, err := cs.RequestWithContext(gContext, &egoscale.CreateAffinityGroup{Name: name, Description: desc, Type: "host anti-affinity"})
func createAffinityGroup(name, desc string) (outputter, error) {
resp, err := cs.RequestWithContext(gContext, &egoscale.CreateAffinityGroup{
Name: name,
Description: desc,
Type: "host anti-affinity",
})
if err != nil {
return err
return nil, err
}

affinityGroup := resp.(*egoscale.AffinityGroup)

if !gQuiet {
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"Name", "Description", "ID"})
table.Append([]string{affinityGroup.Name, affinityGroup.Description, affinityGroup.ID.String()})
table.Render()
return showAffinityGroup(resp.(*egoscale.AffinityGroup))
}

return nil
return nil, nil
}

func init() {
Expand Down
14 changes: 7 additions & 7 deletions cmd/affinitygroup_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ Supported output template annotations: %s`,
return cmd.Usage()
}

return output(showAffinityGroup(args[0]))
ag, err := getAffinityGroupByName(args[0])
if err != nil {
return err
}

return output(showAffinityGroup(ag))
},
})
}

func showAffinityGroup(name string) (outputter, error) {
ag, err := getAffinityGroupByName(name)
if err != nil {
return nil, err
}

func showAffinityGroup(ag *egoscale.AffinityGroup) (outputter, error) {
out := affinityGroupShowOutput{
ID: ag.ID.String(),
Name: ag.Name,
Expand Down
3 changes: 2 additions & 1 deletion cmd/iam_apikey_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/exoscale/egoscale"
Expand Down Expand Up @@ -71,7 +72,7 @@ var apiKeyCreateCmd = &cobra.Command{
}
}

fmt.Print(`
fmt.Fprint(os.Stderr, `
/!\ Ensure to save your API Secret somewhere, /!\
/!\ as there is no way to recover it afterwards. /!\
Expand Down
15 changes: 7 additions & 8 deletions cmd/privnet_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@ var privnetCreateCmd = &cobra.Command{
return cmd.Usage()
}

newNet, err := privnetCreate(name, desc, zone, startIP.Value(), endIP.Value(), netmask.Value())
if err != nil {
return err
}

return privnetShow(*newNet)
return output(createPrivnet(name, desc, zone, startIP.Value(), endIP.Value(), netmask.Value()))
},
}

Expand All @@ -89,7 +84,7 @@ func isEmptyArgs(args ...string) bool {
return false
}

func privnetCreate(name, desc, zoneName string, startIP, endIP, netmask net.IP) (*egoscale.Network, error) {
func createPrivnet(name, desc, zoneName string, startIP, endIP, netmask net.IP) (outputter, error) {
zone, err := getZoneByName(zoneName)

if err != nil {
Expand All @@ -114,7 +109,11 @@ func privnetCreate(name, desc, zoneName string, startIP, endIP, netmask net.IP)
return nil, err
}

return resp.(*egoscale.Network), nil
if !gQuiet {
return showPrivnet(resp.(*egoscale.Network))
}

return nil, nil
}

func init() {
Expand Down
14 changes: 7 additions & 7 deletions cmd/privnet_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ Supported output template annotations: %s`,
return cmd.Usage()
}

return output(showPrivnet(args[0]))
privnet, err := getNetwork(args[0], nil)
if err != nil {
return err
}

return output(showPrivnet(privnet))
},
})
}

func showPrivnet(name string) (outputter, error) {
privnet, err := getNetwork(name, nil)
if err != nil {
return nil, err
}

func showPrivnet(privnet *egoscale.Network) (outputter, error) {
out := privnetShowOutput{
ID: privnet.ID.String(),
Name: privnet.Name,
Expand Down
25 changes: 4 additions & 21 deletions cmd/privnet_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package cmd
import (
"fmt"
"net"
"os"
"strconv"

"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -72,16 +70,16 @@ var privnetUpdateCmd = &cobra.Command{
netmask.IP = (*net.IP)(&ipmask)
}

newNet, err := privnetUpdate(id, name, desc, startIP.Value(), endIP.Value(), netmask.Value())
updatedPrivnet, err := updatePrivnet(id, name, desc, startIP.Value(), endIP.Value(), netmask.Value())
if err != nil {
return err
}

return privnetShow(*newNet)
return output(showPrivnet(updatedPrivnet))
},
}

func privnetUpdate(id, name, desc string, startIP, endIP, netmask net.IP) (*egoscale.Network, error) {
func updatePrivnet(id, name, desc string, startIP, endIP, netmask net.IP) (*egoscale.Network, error) {
uuid, err := egoscale.ParseUUID(id)
if err != nil {
return nil, fmt.Errorf("update the network by ID, got %q", id)
Expand All @@ -100,29 +98,14 @@ func privnetUpdate(id, name, desc string, startIP, endIP, netmask net.IP) (*egos
Netmask: netmask,
}

resp, err := asyncRequest(req, fmt.Sprintf("Updating the network %q ", id))
resp, err := asyncRequest(req, fmt.Sprintf("Updating the network %s", id))
if err != nil {
return nil, err
}

return resp.(*egoscale.Network), nil
}

func privnetShow(network egoscale.Network) error {
if !gQuiet {
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"Name", "Description", "ID", "DHCP"})
table.Append([]string{
network.Name,
network.DisplayText,
network.ID.String(),
dhcpRange(network)})
table.Render()
}

return nil
}

func init() {
privnetUpdateCmd.Flags().StringP("name", "n", "", "Private network name")
privnetUpdateCmd.Flags().StringP("description", "d", "", "Private network description")
Expand Down
83 changes: 43 additions & 40 deletions cmd/snapshot_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,59 @@ package cmd

import (
"fmt"
"log"
"strings"

"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)

// createCmd represents the create command
var snapshotCreateCmd = &cobra.Command{
Use: "create <vm name | vm id>",
Short: "Create a snapshot of an instance volume",
Aliases: gCreateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}

vm, err := getVirtualMachineByNameOrID(args[0])
if err != nil {
return err
}

query := &egoscale.Volume{
VirtualMachineID: vm.ID,
Type: "ROOT",
}

resp, err := cs.GetWithContext(gContext, query)
if err != nil {
return err
}
func init() {
snapshotCmd.AddCommand(&cobra.Command{
Use: "create <vm name | vm id>",
Short: "Create a snapshot of a Compute instance volume",
Long: fmt.Sprintf(`This command creates a snapshot of a Compute instance volume.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&snapshotShowOutput{}), ", ")),
Aliases: gCreateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Usage()
}

return output(createSnapshot(args[0]))
},
})
}

createSnapshot := &egoscale.CreateSnapshot{
VolumeID: resp.(*egoscale.Volume).ID,
}
func createSnapshot(vmID string) (outputter, error) {
vm, err := getVirtualMachineByNameOrID(vmID)
if err != nil {
return nil, err
}

message := fmt.Sprintf("Creating snapshot of %q", vm.Name)
query := &egoscale.Volume{
VirtualMachineID: vm.ID,
Type: "ROOT",
}

res, err := asyncRequest(createSnapshot, message)
if err != nil {
return err
}
resp, err := cs.GetWithContext(gContext, query)
if err != nil {
return nil, err
}

result := res.(*egoscale.Snapshot)
createSnapshot := &egoscale.CreateSnapshot{
VolumeID: resp.(*egoscale.Volume).ID,
}

log.Printf("Snapshot %q of %q successfully created", result.Name, vm.Name)
res, err := asyncRequest(createSnapshot, fmt.Sprintf("Creating snapshot of %q", vm.Name))
if err != nil {
return nil, err
}

return nil
},
}
if !gQuiet {
return showSnapshot(res.(*egoscale.Snapshot))
}

func init() {
snapshotCmd.AddCommand(snapshotCreateCmd)
return nil, nil
}
2 changes: 1 addition & 1 deletion cmd/snapshot_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var snapshotDeleteCmd = &cobra.Command{
if err != nil {
return err
}
t := task{egoscale.DeleteSnapshot{ID: volume.ID}, fmt.Sprintf("Deleting snapshot %q", volume.Name)}
t := task{egoscale.DeleteSnapshot{ID: volume.ID}, fmt.Sprintf("Deleting snapshot %s", volume.ID.String())}
tasks = append(tasks, t)
}

Expand Down
21 changes: 10 additions & 11 deletions cmd/snapshot_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

humanize "github.com/dustin/go-humanize"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -34,24 +35,22 @@ Supported output template annotations: %s`,
return cmd.Usage()
}

return output(showSnapshot(args[0]))
snapshot, err := getSnapshotWithNameOrID(args[0])
if err != nil {
return err
}

return output(showSnapshot(snapshot))
},
})
}

func showSnapshot(id string) (outputter, error) {
snapshot, err := getSnapshotWithNameOrID(id)
if err != nil {
return nil, err
}

out := snapshotShowOutput{
func showSnapshot(snapshot *egoscale.Snapshot) (outputter, error) {
return &snapshotShowOutput{
ID: snapshot.ID.String(),
Instance: snapshotVMName(*snapshot),
Date: snapshot.Created,
State: snapshot.State,
Size: humanize.IBytes(uint64(snapshot.Size)),
}

return &out, nil
}, nil
}
Loading

0 comments on commit b889ad2

Please sign in to comment.