Skip to content

Commit

Permalink
return error codes when errors are encountered
Browse files Browse the repository at this point in the history
  • Loading branch information
displague committed Aug 9, 2020
1 parent 691f5ac commit 92d8ff8
Show file tree
Hide file tree
Showing 54 changed files with 290 additions and 412 deletions.
12 changes: 5 additions & 7 deletions cmd/assign_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
package cmd

import (
"fmt"
"strconv"

"github.com/packethost/packngo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -41,26 +41,24 @@ var assignIPCmd = &cobra.Command{
packet ip assign -d [device-id] -a [ip-address]
`,
Run: func(cmd *cobra.Command, args []string) {

RunE: func(cmd *cobra.Command, args []string) error {
assignment, _, err := PacknGo.DeviceIPs.Assign(deviceID, &packngo.AddressStruct{Address: address})
if err != nil {
fmt.Println("Client error:", err)
return
return errors.Wrap(err, "Could not assign Device IP address")
}

data := make([][]string, 1)

data[0] = []string{assignment.ID, assignment.Address, strconv.FormatBool(assignment.Public), assignment.Created}
header := []string{"ID", "Address", "Public", "Created"}

output(assignment, header, &data)
return output(assignment, header, &data)
},
}

func init() {
assignIPCmd.Flags().StringVarP(&deviceID, "device-id", "d", "", "UUID of the device")
assignIPCmd.Flags().StringVarP(&address, "address", "a", "", "IP Address")
assignIPCmd.Flags().StringVarP(&address, "address", "a", "", "IP address")

_ = assignIPCmd.MarkFlagRequired("device-id")
_ = assignIPCmd.MarkFlagRequired("address")
Expand Down
10 changes: 4 additions & 6 deletions cmd/attach_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
package cmd

import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -35,18 +34,17 @@ var attachVolumeCmd = &cobra.Command{
packet volume attach --id [volume_UUID] --device-id [device_UUID]
`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
a, _, err := PacknGo.VolumeAttachments.Create(volumeID, deviceID)
if err != nil {
fmt.Println("Client error:", err)
return
return errors.Wrap(err, "Could not create volume attachment")
}

header := []string{"ID"}
data := make([][]string, 1)
data[0] = []string{a.ID}

output(a, header, &data)
return output(a, header, &data)
},
}

Expand Down
10 changes: 4 additions & 6 deletions cmd/available.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
package cmd

import (
"fmt"

"github.com/packethost/packngo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -40,20 +39,19 @@ var availableCmd = &cobra.Command{
packet ip available --reservation-id [reservation_id] --cidr [size_of_subnet]
`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
result, _, err := PacknGo.ProjectIPs.AvailableAddresses(reservationID, &packngo.AvailableRequest{CIDR: cidr})

if err != nil {
fmt.Println("Client error:", err)
return
return errors.Wrap(err, "Could not get available IP addresses")
}
data := make([][]string, len(result))
for i, r := range result {
data[i] = []string{r}
}
header := []string{"Available IPs"}

output(result, header, &data)
return output(result, header, &data)
},
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/check_capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
package cmd

import (
"fmt"
"strconv"

"github.com/packethost/packngo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -37,7 +37,7 @@ var checkCapacityCommand = &cobra.Command{
packet capacity check -f [facility] -p [plan] -q [quantity]
`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
req := &packngo.CapacityInput{
Servers: []packngo.ServerInfo{
{
Expand All @@ -49,7 +49,7 @@ packet capacity check -f [facility] -p [plan] -q [quantity]

availability, _, err := PacknGo.CapacityService.Check(req)
if err != nil {
fmt.Println("Client error:", err)
return errors.Wrap(err, "Could not check capacity")
}

data := make([][]string, 1)
Expand All @@ -58,7 +58,7 @@ packet capacity check -f [facility] -p [plan] -q [quantity]
strconv.Itoa(availability.Servers[0].Quantity), strconv.FormatBool(availability.Servers[0].Available)}
header := []string{"Facility", "Plan", "Quantiy", "Availability"}

output(availability, header, &data)
return output(availability, header, &data)
},
}

Expand Down
18 changes: 8 additions & 10 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewCli() *Cli {
return cli
}

func output(in interface{}, header []string, data *[][]string) {
func output(in interface{}, header []string, data *[][]string) error {
if !isJSON && !isYaml {

table := tablewriter.NewWriter(os.Stdout)
Expand All @@ -55,21 +55,20 @@ func output(in interface{}, header []string, data *[][]string) {
} else if isJSON {
output, err := json.MarshalIndent(in, "", " ")
if err != nil {
fmt.Println(err)
return
return err
}
fmt.Println(string(output))
} else if isYaml {
output, err := yaml.Marshal(in)
if err != nil {
fmt.Println(err)
return
return err
}
fmt.Println(string(output))
}
return nil
}

func outputMergingCells(in interface{}, header []string, data *[][]string) {
func outputMergingCells(in interface{}, header []string, data *[][]string) error {
if !isJSON && !isYaml {

table := tablewriter.NewWriter(os.Stdout)
Expand All @@ -81,16 +80,15 @@ func outputMergingCells(in interface{}, header []string, data *[][]string) {
} else if isJSON {
output, err := json.MarshalIndent(in, "", " ")
if err != nil {
fmt.Println(err)
return
return err
}
fmt.Println(string(output))
} else if isYaml {
output, err := yaml.Marshal(in)
if err != nil {
fmt.Println(err)
return
return err
}
fmt.Println(string(output))
}
return nil
}
12 changes: 5 additions & 7 deletions cmd/create_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/packethost/packngo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -58,7 +59,7 @@ var createDeviceCmd = &cobra.Command{
packet device create --hostname [hostname] --plan [plan] --facility [facility_code] --operating-system [operating_system] --project-id [project_UUID]
`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {

request := &packngo.DeviceCreateRequest{
Hostname: hostname,
Expand Down Expand Up @@ -108,24 +109,21 @@ packet device create --hostname [hostname] --plan [plan] --facility [facility_co
if terminationTime != "" {
parsedTime, err := time.Parse(time.RFC3339, terminationTime)
if err != nil {
fmt.Printf("Error occured while parsing time string: %s", err.Error())
return
return errors.Wrap(err, fmt.Sprintf("Could not parse time %q", terminationTime))
}
request.TerminationTime = &packngo.Timestamp{Time: parsedTime}
}

device, _, err := PacknGo.Devices.Create(request)
if err != nil {
fmt.Println("Client error:", err)
return
return errors.Wrap(err, "Could not create Device")
}

header := []string{"ID", "Hostname", "OS", "State", "Created"}
data := make([][]string, 1)
data[0] = []string{device.ID, device.Hostname, device.OS.Name, device.State, device.Created}

output(device, header, &data)

return output(device, header, &data)
},
}

Expand Down
9 changes: 4 additions & 5 deletions cmd/create_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
package cmd

import (
"fmt"

"github.com/packethost/packngo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -42,7 +41,7 @@ var createOrganizationCmd = &cobra.Command{
packet organization create -n [name]
`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
req := &packngo.OrganizationCreateRequest{
Name: name,
}
Expand All @@ -61,15 +60,15 @@ packet organization create -n [name]

org, _, err := PacknGo.Organizations.Create(req)
if err != nil {
fmt.Println("Client error:", err)
return errors.Wrap(err, "Could not create Organization")
}

data := make([][]string, 1)

data[0] = []string{org.ID, org.Name, org.Created}
header := []string{"ID", "Name", "Created"}

output(org, header, &data)
return output(org, header, &data)
},
}

Expand Down
10 changes: 4 additions & 6 deletions cmd/create_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
package cmd

import (
"fmt"

"github.com/packethost/packngo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -41,7 +40,7 @@ var createProjectCmd = &cobra.Command{
packet project create --name [project_name]
`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
req := packngo.ProjectCreateRequest{
Name: name,
}
Expand All @@ -56,15 +55,14 @@ packet project create --name [project_name]

p, _, err := PacknGo.Projects.Create(&req)
if err != nil {
fmt.Println("Client error:", err)
return
return errors.Wrap(err, "Could not create Project")
}

data := make([][]string, 1)

data[0] = []string{p.ID, p.Name, p.Created}
header := []string{"ID", "Name", "Created"}
output(p, header, &data)
return output(p, header, &data)
},
}

Expand Down
10 changes: 4 additions & 6 deletions cmd/create_ssh-key.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
package cmd

import (
"fmt"

"github.com/packethost/packngo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -41,23 +40,22 @@ var createSSHKeyCmd = &cobra.Command{
packet ssh-key create --key [public_key] --label [label]
`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
req := packngo.SSHKeyCreateRequest{
Label: label,
Key: key,
}

s, _, err := PacknGo.SSHKeys.Create(&req)
if err != nil {
fmt.Println("Client error:", err)
return
return errors.Wrap(err, "Could not create SSHKey")
}

data := make([][]string, 1)

data[0] = []string{s.ID, s.Label, s.Created}
header := []string{"ID", "Label", "Created"}
output(s, header, &data)
return output(s, header, &data)
},
}

Expand Down
Loading

0 comments on commit 92d8ff8

Please sign in to comment.