Skip to content

Commit

Permalink
refactor volumes to demonstrate how globals could be avoided
Browse files Browse the repository at this point in the history
  • Loading branch information
displague committed Aug 10, 2020
1 parent e23f301 commit fb98229
Show file tree
Hide file tree
Showing 19 changed files with 473 additions and 269 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.so
*.dylib
packet
packet-cli

# Test binary, build with `go test -c`
*.test
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![GitHub release](https://img.shields.io/github/release/packethost/packet-cli/all.svg?style=flat-square)](https://github.com/packethost/packet-cli/releases)
[![Go Report Card](https://goreportcard.com/badge/github.com/packethost/packet-cli)](https://goreportcard.com/report/github.com/packethost/packet-cli)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/packethost/packet-cli)](https://pkg.go.dev/github.com/packethost/packet-cli)
[![Slack](https://slack.packet.com/badge.svg)](https://slack.packet.com)
[![Twitter Follow](https://img.shields.io/twitter/follow/packethost.svg?style=social&label=Follow)](https://twitter.com/intent/follow?screen_name=packethost)

Expand Down
89 changes: 41 additions & 48 deletions cmd/cli.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package cmd

import (
"encoding/json"
"fmt"
"os"

"github.com/ghodss/yaml"
"github.com/olekukonko/tablewriter"
outputPkg "github.com/packethost/packet-cli/internal/output"
"github.com/packethost/packet-cli/internal/volume"
"github.com/packethost/packngo"
"github.com/spf13/cobra"
)

// Cli struct
type Cli struct {
Client *packngo.Client
MainCmd *cobra.Command
Client *packngo.Client
MainCmd *cobra.Command
Outputer outputPkg.Outputer
}

// VERSION build
Expand All @@ -32,63 +32,56 @@ func NewCli() *Cli {
return nil
}

rootCmd.DisableSuggestions = false
cli.MainCmd = rootCmd
cli.Outputer = &outputPkg.Standard{}
cli.RegisterCommands()

err = rootCmd.Execute()
if err != nil {
fmt.Println("Execution error:", err)
return nil
}

rootCmd.DisableSuggestions = false
cli.MainCmd = rootCmd
return cli
}

type Registrar interface {
Register(*cobra.Command, outputPkg.Outputer)
}

func (cli *Cli) RegisterCommands() {
c := cli.Client
for _, reggie := range []Registrar{
&volume.VolumeClient{VolumeService: c.Volumes, VolumeAttachmentService: c.VolumeAttachments},
} {
reggie.Register(cli.MainCmd, cli.Outputer)
}
}

func output(in interface{}, header []string, data *[][]string) error {
if !isJSON && !isYaml {
format := outputPkg.FormatText

table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeader(header)
table.AppendBulk(*data)
table.Render()
} else if isJSON {
output, err := json.MarshalIndent(in, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
} else if isYaml {
output, err := yaml.Marshal(in)
if err != nil {
return err
}
fmt.Println(string(output))
// TODO(displague) remove isJSON and isYaml globals
switch {
case isJSON:
format = outputPkg.FormatJSON
case isYaml:
format = outputPkg.FormatYAML
}
return nil
output := &outputPkg.Standard{Format: format}
return output.Output(in, header, data)
}

func outputMergingCells(in interface{}, header []string, data *[][]string) error {
if !isJSON && !isYaml {
format := outputPkg.FormatText

table := tablewriter.NewWriter(os.Stdout)
table.SetAutoMergeCells(true)
table.SetRowLine(true)
table.SetHeader(header)
table.AppendBulk(*data)
table.Render()
} else if isJSON {
output, err := json.MarshalIndent(in, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
} else if isYaml {
output, err := yaml.Marshal(in)
if err != nil {
return err
}
fmt.Println(string(output))
// TODO(displague) remove isJSON and isYaml globals
switch {
case isJSON:
format = outputPkg.FormatJSON
case isYaml:
format = outputPkg.FormatYAML
}
return nil
output := &outputPkg.CellMerging{Format: format}
return output.Output(in, header, data)
}
88 changes: 0 additions & 88 deletions cmd/create_volume.go

This file was deleted.

4 changes: 3 additions & 1 deletion cmd/retrieve_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import (
"github.com/spf13/cobra"
)

var eventID string
var (
volumeID, eventID string
)

// retrieveEventsCmd represents the retrieveEvents command
var retrieveEventCmd = &cobra.Command{
Expand Down
11 changes: 7 additions & 4 deletions docs/packet_volume_attach.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ Attaches a volume to a device.

### Synopsis

Example:
Attaches a volume to a device.

packet volume attach --id [volume_UUID] --device-id [device_UUID]
```
packet volume attach [flags]
```

### Examples

```
packet volume attach [flags]
packet volume attach --id [volume_UUID] --device-id [device_UUID]
```

### Options
Expand Down
15 changes: 8 additions & 7 deletions docs/packet_volume_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@ Creates a volume

### Synopsis

Example:

packet volume create --size [size_in_GB] --plan [plan_UUID] --project-id [project_UUID] --facility [facility_code]


Creates a volume

```
packet volume create [flags]
```

### Examples

```
packet volume create --size [size_in_GB] --plan [plan_UUID]--project-id [project_UUID] --facility [facility_code]
```

### Options

```
-b, --billing-cycle string Billing cycle (default "hourly")
-d, --description string Description of the volume
-f, --facility string Code of the facility where the volume will be created
-h, --help help for create
-j, --json JSON output
-l, --locked Set the volume to be locked
-P, --plan string Name of the plan
-p, --project-id string UUID of the project
-s, --size int Size in GB]
-y, --yaml YAML output
```

### Options inherited from parent commands
Expand Down
11 changes: 7 additions & 4 deletions docs/packet_volume_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ Deletes a volume

### Synopsis

Example:
Deletes a volume

packet volume delete --id [volume_UUID]
```
packet volume delete [flags]
```

### Examples

```
packet volume delete [flags]
packet volume delete --id [volume_UUID]
```

### Options
Expand Down
11 changes: 7 additions & 4 deletions docs/packet_volume_detach.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ Detaches a volume from a device

### Synopsis

Example:

packet volume detach --id [attachment_UUID]
Detaches a volume from a device

```
packet volume detach [flags]
```

### Examples

```
packet volume detach [flags]
packet volume detach --id [attachment_UUID]
```

### Options
Expand Down
18 changes: 10 additions & 8 deletions docs/packet_volume_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ Retrieves a volume list or volume details.

### Synopsis

Example:

Retrieve the list of volumes:
packet volume get --project-id [project_UUID]

Retrieve a specific volume:
packet volume get --id [volume_UUID]
Retrieves a volume list or volume details.

```
packet volume get [flags]
```

### Examples

```
packet volume get [flags]
Retrieve the list of volumes:
packet volume get --project-id [project_UUID]
Retrieve a specific volume:
packet volume get --id [volume_UUID]
```

### Options
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ require (
github.com/spf13/viper v1.4.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
gopkg.in/yaml.v2 v2.2.2
)
Loading

0 comments on commit fb98229

Please sign in to comment.