diff --git a/cmd/assign_ip.go b/cmd/assign_ip.go index 1ddf8d68..d375088a 100644 --- a/cmd/assign_ip.go +++ b/cmd/assign_ip.go @@ -21,10 +21,10 @@ package cmd import ( - "fmt" "strconv" "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -41,12 +41,10 @@ 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) @@ -54,13 +52,13 @@ packet ip assign -d [device-id] -a [ip-address] 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") diff --git a/cmd/attach_volume.go b/cmd/attach_volume.go index 14a454ce..7c28f178 100644 --- a/cmd/attach_volume.go +++ b/cmd/attach_volume.go @@ -21,8 +21,7 @@ package cmd import ( - "fmt" - + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -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) }, } diff --git a/cmd/available.go b/cmd/available.go index 50481b7a..f8fd5d0d 100644 --- a/cmd/available.go +++ b/cmd/available.go @@ -21,9 +21,8 @@ package cmd import ( - "fmt" - "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -40,12 +39,11 @@ 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 { @@ -53,7 +51,7 @@ packet ip available --reservation-id [reservation_id] --cidr [size_of_subnet] } header := []string{"Available IPs"} - output(result, header, &data) + return output(result, header, &data) }, } diff --git a/cmd/check_capacity.go b/cmd/check_capacity.go index 3c0e4421..9a3777f2 100644 --- a/cmd/check_capacity.go +++ b/cmd/check_capacity.go @@ -21,10 +21,10 @@ package cmd import ( - "fmt" "strconv" "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -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{ { @@ -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) @@ -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) }, } diff --git a/cmd/cli.go b/cmd/cli.go index 7381f45d..00fe6e7f 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -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) @@ -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) @@ -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 } diff --git a/cmd/create_device.go b/cmd/create_device.go index d86027fc..3ec2147d 100644 --- a/cmd/create_device.go +++ b/cmd/create_device.go @@ -25,6 +25,7 @@ import ( "time" "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -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, @@ -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) }, } diff --git a/cmd/create_organization.go b/cmd/create_organization.go index ef70ddd7..a3ecac73 100644 --- a/cmd/create_organization.go +++ b/cmd/create_organization.go @@ -21,9 +21,8 @@ package cmd import ( - "fmt" - "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -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, } @@ -61,7 +60,7 @@ 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) @@ -69,7 +68,7 @@ packet organization create -n [name] data[0] = []string{org.ID, org.Name, org.Created} header := []string{"ID", "Name", "Created"} - output(org, header, &data) + return output(org, header, &data) }, } diff --git a/cmd/create_project.go b/cmd/create_project.go index c04e79b2..e3006d26 100644 --- a/cmd/create_project.go +++ b/cmd/create_project.go @@ -21,9 +21,8 @@ package cmd import ( - "fmt" - "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -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, } @@ -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) }, } diff --git a/cmd/create_ssh-key.go b/cmd/create_ssh-key.go index d3feada9..9013a5da 100644 --- a/cmd/create_ssh-key.go +++ b/cmd/create_ssh-key.go @@ -21,9 +21,8 @@ package cmd import ( - "fmt" - "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -41,7 +40,7 @@ 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, @@ -49,15 +48,14 @@ packet ssh-key create --key [public_key] --label [label] 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) }, } diff --git a/cmd/create_virtual_network.go b/cmd/create_virtual_network.go index 66c70632..e27430df 100644 --- a/cmd/create_virtual_network.go +++ b/cmd/create_virtual_network.go @@ -21,10 +21,10 @@ package cmd import ( - "fmt" "strconv" "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -37,7 +37,7 @@ var createVirtualNetworkCmd = &cobra.Command{ packet virtual-network create --project-id [project_UUID] --facility [facility_code] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { req := &packngo.VirtualNetworkCreateRequest{ ProjectID: projectID, Facility: facility, @@ -48,7 +48,7 @@ packet virtual-network create --project-id [project_UUID] --facility [facility_c n, _, err := PacknGo.ProjectVirtualNetworks.Create(req) if err != nil { - fmt.Println("Client error:", err) + return errors.Wrap(err, "Could not create ProjectVirtualNetwork") } data := make([][]string, 1) @@ -57,7 +57,7 @@ packet virtual-network create --project-id [project_UUID] --facility [facility_c header := []string{"ID", "Description", "VXLAN", "Facility", "Created"} - output(n, header, &data) + return output(n, header, &data) }, } diff --git a/cmd/create_volume.go b/cmd/create_volume.go index 5a50ce3f..7ede28e3 100644 --- a/cmd/create_volume.go +++ b/cmd/create_volume.go @@ -21,10 +21,10 @@ package cmd import ( - "fmt" "strconv" "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -41,7 +41,7 @@ var createVolumeCmd = &cobra.Command{ packet volume create --size [size_in_GB] --plan [plan_UUID] --project-id [project_UUID] --facility [facility_code] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { req := &packngo.VolumeCreateRequest{ BillingCycle: billingCycle, PlanID: plan, @@ -58,15 +58,14 @@ var createVolumeCmd = &cobra.Command{ v, _, err := PacknGo.Volumes.Create(req, projectID) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not create Volume") } header := []string{"ID", "Name", "Size", "State", "Created"} data := make([][]string, 1) data[0] = []string{v.ID, v.Name, strconv.Itoa(v.Size), v.State, v.Created} - output(v, header, &data) + return output(v, header, &data) }, } diff --git a/cmd/delete_device.go b/cmd/delete_device.go index 74512c1c..a84aa159 100644 --- a/cmd/delete_device.go +++ b/cmd/delete_device.go @@ -24,6 +24,7 @@ import ( "fmt" "github.com/manifoldco/promptui" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -37,7 +38,7 @@ var deleteDeviceCmd = &cobra.Command{ packet device delete -i [device_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if !force { prompt := promptui.Prompt{ Label: fmt.Sprintf("Are you sure you want to delete device %s: ", deviceID), @@ -46,21 +47,10 @@ var deleteDeviceCmd = &cobra.Command{ _, err := prompt.Run() if err != nil { - return - } - - err = deleteDevice(deviceID) - if err != nil { - fmt.Println("Client error:", err) - return - } - } else { - err := deleteDevice(deviceID) - if err != nil { - fmt.Println("Client error:", err) - return + return nil } } + return errors.Wrap(deleteDevice(deviceID), "Could not delete Device") }, } diff --git a/cmd/delete_organization.go b/cmd/delete_organization.go index de0abcfe..e743dae4 100644 --- a/cmd/delete_organization.go +++ b/cmd/delete_organization.go @@ -24,6 +24,7 @@ import ( "fmt" "github.com/manifoldco/promptui" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,7 +37,7 @@ var deleteOrganizationCmd = &cobra.Command{ packet organization delete -i [organization_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if !force { prompt := promptui.Prompt{ Label: fmt.Sprintf("Are you sure you want to delete organization %s: ", organizationID), @@ -45,21 +46,11 @@ packet organization delete -i [organization_UUID] _, err := prompt.Run() if err != nil { - return - } - - err = deleteOrganization(organizationID) - if err != nil { - fmt.Println("Client error:", err) - return - } - } else { - err := deleteOrganization(organizationID) - if err != nil { - fmt.Println("Client error:", err) - return + return nil } } + + return errors.Wrap(deleteOrganization(organizationID), "Could not delete Organization") }, } diff --git a/cmd/delete_project.go b/cmd/delete_project.go index ed07c8ca..fd716e7c 100644 --- a/cmd/delete_project.go +++ b/cmd/delete_project.go @@ -24,6 +24,7 @@ import ( "fmt" "github.com/manifoldco/promptui" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,7 +37,7 @@ var deleteProjectCmd = &cobra.Command{ packet project delete --id [project_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if !force { prompt := promptui.Prompt{ Label: fmt.Sprintf("Are you sure you want to delete project %s: ", projectID), @@ -45,21 +46,10 @@ packet project delete --id [project_UUID] _, err := prompt.Run() if err != nil { - return - } - - err = deleteProject(projectID) - if err != nil { - fmt.Println("Client error:", err) - return - } - } else { - err := deleteProject(projectID) - if err != nil { - fmt.Println("Client error:", err) - return + return nil } } + return errors.Wrap(deleteProject(projectID), "Could not delete Project") }, } diff --git a/cmd/delete_ssh-key.go b/cmd/delete_ssh-key.go index cdcee33c..2eee2412 100644 --- a/cmd/delete_ssh-key.go +++ b/cmd/delete_ssh-key.go @@ -24,10 +24,11 @@ import ( "fmt" "github.com/manifoldco/promptui" + "github.com/pkg/errors" "github.com/spf13/cobra" ) -// deleteProjectCmd represents the deleteProject command +// deleteSSHKeyCmd represents the deleteSSHKey command var deleteSSHKeyCmd = &cobra.Command{ Use: "delete", Short: "Deletes an SSH key", @@ -36,30 +37,20 @@ var deleteSSHKeyCmd = &cobra.Command{ packet ssh-key delete --id [ssh-key_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if !force { prompt := promptui.Prompt{ - Label: fmt.Sprintf("Are you sure you want to delete project %s: ", projectID), + Label: fmt.Sprintf("Are you sure you want to delete SSH Key %s: ", sshKeyID), IsConfirm: true, } _, err := prompt.Run() if err != nil { - return - } - - err = deleteSSHKey(sshKeyID) - if err != nil { - fmt.Println("Client error:", err) - return - } - } else { - err := deleteSSHKey(sshKeyID) - if err != nil { - fmt.Println("Client error:", err) - return + return nil } } + + return errors.Wrap(deleteSSHKey(sshKeyID), "Could not delete SSH Key") }, } diff --git a/cmd/delete_virtual_network.go b/cmd/delete_virtual_network.go index f4f673c9..d9571d3b 100644 --- a/cmd/delete_virtual_network.go +++ b/cmd/delete_virtual_network.go @@ -24,6 +24,7 @@ import ( "fmt" "github.com/manifoldco/promptui" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -40,7 +41,7 @@ var deleteVirtualNetworkCmd = &cobra.Command{ packet virtual-network delete -i [virtual_network_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if !force { prompt := promptui.Prompt{ Label: fmt.Sprintf("Are you sure you want to delete virual network %s", vnetID), @@ -49,31 +50,22 @@ packet virtual-network delete -i [virtual_network_UUID] _, err := prompt.Run() if err != nil { - return - } - err = deleteVnet(vnetID) - if err != nil { - fmt.Println(err) - return - } - - } else { - err := deleteVnet(vnetID) - if err != nil { - fmt.Println(err) - return + return nil } } - - fmt.Println("Virtual Network", vnetID, "successfully deleted.") - + return errors.Wrap(deleteVnet(vnetID), "Could not delete Virtual Network") }, } func deleteVnet(id string) error { _, err := PacknGo.ProjectVirtualNetworks.Delete(id) + if err != nil { + return err + } + fmt.Println("Virtual Network", id, "successfully deleted.") return err } + func init() { deleteVirtualNetworkCmd.Flags().StringVarP(&vnetID, "id", "i", "", "UUID of the vlan") _ = deleteVirtualNetworkCmd.MarkFlagRequired("id") diff --git a/cmd/delete_volume.go b/cmd/delete_volume.go index bef20200..d60836a9 100644 --- a/cmd/delete_volume.go +++ b/cmd/delete_volume.go @@ -24,6 +24,7 @@ import ( "fmt" "github.com/manifoldco/promptui" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,7 +37,7 @@ var deleteVolumeCmd = &cobra.Command{ packet volume delete --id [volume_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if !force { prompt := promptui.Prompt{ Label: fmt.Sprintf("Are you sure you want to delete the volume %s: ", volumeID), @@ -45,21 +46,11 @@ packet volume delete --id [volume_UUID] _, err := prompt.Run() if err != nil { - return - } - - err = deleteVolume(volumeID) - if err != nil { - fmt.Println("Client error:", err) - return - } - } else { - err := deleteVolume(volumeID) - if err != nil { - fmt.Println("Client error:", err) - return + return nil } } + + return errors.Wrap(deleteVolume(volumeID), "Could not delete Volume") }, } diff --git a/cmd/detach_volume.go b/cmd/detach_volume.go index b369c7ea..b5bea270 100644 --- a/cmd/detach_volume.go +++ b/cmd/detach_volume.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -39,14 +40,14 @@ var detachVolumeCmd = &cobra.Command{ packet volume detach --id [attachment_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { _, err := PacknGo.VolumeAttachments.Delete(attachmentID) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not detach Volume") } fmt.Println("Volume detachment initiated.") + return nil }, } diff --git a/cmd/disable2fa.go b/cmd/disable2fa.go index f5848d27..b28878a1 100644 --- a/cmd/disable2fa.go +++ b/cmd/disable2fa.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -38,28 +39,22 @@ packet 2fa disable -s -t [token] Disable two factor authentication via APP packet 2fa disable -a -t [token] `, - Run: func(cmd *cobra.Command, args []string) { - if !sms && !app { - fmt.Println("Either sms or app should be set") - return - } else if sms && app { - fmt.Println("Either sms or app can be set.") - return + RunE: func(cmd *cobra.Command, args []string) error { + if sms == app { + return fmt.Errorf("Either sms or app should be set") } else if sms { _, err := PacknGo.TwoFactorAuth.DisableSms(token) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not disable Two-Factor Authentication via SMS") } - fmt.Println("Two factor authentication successfuly disabled.") } else if app { _, err := PacknGo.TwoFactorAuth.DisableApp(token) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not disable Two-Factor Authentication via App") } - fmt.Println("Two factor authentication successfuly disabled.") } + fmt.Println("Two factor authentication successfully disabled.") + return nil }, } diff --git a/cmd/disable_vpn.go b/cmd/disable_vpn.go index 3dd0c5bf..9ff87731 100644 --- a/cmd/disable_vpn.go +++ b/cmd/disable_vpn.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -35,13 +36,13 @@ var disableVpnCmd = &cobra.Command{ Disable VPN service: packet vpn disable `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { _, err := PacknGo.VPN.Disable() if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not disable VPN service") } - fmt.Println("VPN service Disabled.") + fmt.Println("VPN service disabled.") + return nil }, } diff --git a/cmd/enable2fa.go b/cmd/enable2fa.go index aa8fc027..1723ca52 100644 --- a/cmd/enable2fa.go +++ b/cmd/enable2fa.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -40,28 +41,22 @@ packet 2fa enable -s -t [token] Enable two factor authentication via APP packet 2fa enable -a -t [token] `, - Run: func(cmd *cobra.Command, args []string) { - if !sms && !app { - fmt.Println("Either sms or app should be set") - return - } else if sms && app { - fmt.Println("Either sms or app can be set.") - return + RunE: func(cmd *cobra.Command, args []string) error { + if sms == app { + return fmt.Errorf("Either sms or app should be set") } else if sms { _, err := PacknGo.TwoFactorAuth.EnableSms(token) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not enable Two-Factor Authentication") } - fmt.Println("Two factor authentication successfuly enabled.") } else if app { _, err := PacknGo.TwoFactorAuth.EnableApp(token) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not enable Two-Factor Authentication") } - fmt.Println("Two factor authentication successfuly enabled.") } + fmt.Println("Two factor authentication successfully enabled.") + return nil }, } diff --git a/cmd/enable_vpn.go b/cmd/enable_vpn.go index ec844d34..a7b8f12e 100644 --- a/cmd/enable_vpn.go +++ b/cmd/enable_vpn.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -35,13 +36,13 @@ var enableVpnCmd = &cobra.Command{ Enable VPN service: packet vpn enable `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { _, err := PacknGo.VPN.Enable() if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not enable VPN Service") } fmt.Println("VPN service enabled.") + return nil }, } diff --git a/cmd/move_hardware_reservation.go b/cmd/move_hardware_reservation.go index a730321c..5c135612 100644 --- a/cmd/move_hardware_reservation.go +++ b/cmd/move_hardware_reservation.go @@ -21,8 +21,7 @@ package cmd import ( - "fmt" - + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -34,19 +33,18 @@ var moveHardwareReservationCmd = &cobra.Command{ packet hardware_reservation move -i [hardware_reservation_UUID] -p [project_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { header := []string{"ID", "Facility", "Plan", "Created"} r, _, err := PacknGo.HardwareReservations.Move(hardwareReservationID, projectID) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not move Hardware Reservation") } data := make([][]string, 1) data[0] = []string{r.ID, r.Facility.Code, r.Plan.Name, r.CreatedAt.String()} - output(r, header, &data) + return output(r, header, &data) }, } diff --git a/cmd/payment_methods.go b/cmd/payment_methods.go index 1cab2d38..11478e85 100644 --- a/cmd/payment_methods.go +++ b/cmd/payment_methods.go @@ -21,8 +21,7 @@ package cmd import ( - "fmt" - + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,11 +35,10 @@ var paymentMethodsCmd = &cobra.Command{ packet organization get payment-methods --id [organization_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { paymentMethods, _, err := PacknGo.Organizations.ListPaymentMethods(organizationID) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Payment Methods") } data := make([][]string, len(paymentMethods)) @@ -50,7 +48,7 @@ packet organization get payment-methods --id [organization_UUID] } header := []string{"ID", "Cardholder", "Exp. Month", "Exp. Year", "Created"} - output(paymentMethods, header, &data) + return output(paymentMethods, header, &data) }, } diff --git a/cmd/reboot_device.go b/cmd/reboot_device.go index 82e91213..1cbf6890 100644 --- a/cmd/reboot_device.go +++ b/cmd/reboot_device.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -35,14 +36,14 @@ var rebootDeviceCmd = &cobra.Command{ packet device reboot --id [device_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { _, err := PacknGo.Devices.Reboot(deviceID) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not reboot Device") } fmt.Println("Device", deviceID, "successfully rebooted.") + return nil }, } diff --git a/cmd/receive_2fa.go b/cmd/receive_2fa.go index 84e5017b..e6b96ee1 100644 --- a/cmd/receive_2fa.go +++ b/cmd/receive_2fa.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -41,34 +42,31 @@ Issue the token via app: packet 2fa receive -a `, - Run: func(cmd *cobra.Command, args []string) { - if !sms && !app { - fmt.Println("Either sms or app should be set") - return - } else if sms && app { - fmt.Println("Either sms or app can be set.") - return - } else if sms { + RunE: func(cmd *cobra.Command, args []string) error { + if sms == app { + return fmt.Errorf("Either sms or app should be set") + } + + if sms { _, err := PacknGo.TwoFactorAuth.ReceiveSms() if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not issue token via SMS") } fmt.Println("SMS token sent to your phone") - } else if app { - otpURI, _, err := PacknGo.TwoFactorAuth.SeedApp() - if err != nil { - fmt.Println("Client error:", err) - return - } - - data := make([][]string, 1) + return nil + } - data[0] = []string{otpURI} - header := []string{"OTP URI"} - output(otpURI, header, &data) + otpURI, _, err := PacknGo.TwoFactorAuth.SeedApp() + if err != nil { + return errors.Wrap(err, "Could not get the OTP Seed URI") } + + data := make([][]string, 1) + + data[0] = []string{otpURI} + header := []string{"OTP URI"} + return output(otpURI, header, &data) }, } diff --git a/cmd/remove_ip.go b/cmd/remove_ip.go index 00ea3864..5b470d25 100644 --- a/cmd/remove_ip.go +++ b/cmd/remove_ip.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -35,14 +36,14 @@ var removeIPCmd = &cobra.Command{ packet ip remove --id [reservation-UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { _, err := PacknGo.ProjectIPs.Remove(reservationID) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not remove IP address Reservation") } fmt.Println("IP reservation removed successfully.") + return nil }, } diff --git a/cmd/request_ip.go b/cmd/request_ip.go index 9445b50d..4a3662cf 100644 --- a/cmd/request_ip.go +++ b/cmd/request_ip.go @@ -21,10 +21,10 @@ package cmd import ( - "fmt" "strconv" "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -43,7 +43,7 @@ var requestIPCmd = &cobra.Command{ packet ip request --quantity [quantity] --facility [facility_code] --type [address_type] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { req := &packngo.IPReservationRequest{ Type: ttype, Quantity: quantity, @@ -52,8 +52,7 @@ packet ip request --quantity [quantity] --facility [facility_code] --type [addre reservation, _, err := PacknGo.ProjectIPs.Request(projectID, req) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not request IP addresses") } data := make([][]string, 1) @@ -61,7 +60,7 @@ packet ip request --quantity [quantity] --facility [facility_code] --type [addre data[0] = []string{reservation.ID, reservation.Address, strconv.FormatBool(reservation.Public), reservation.Created} header := []string{"ID", "Address", "Public", "Created"} - output(reservation, header, &data) + return output(reservation, header, &data) }, } diff --git a/cmd/retrieve_capacity.go b/cmd/retrieve_capacity.go index a022d916..6640d024 100644 --- a/cmd/retrieve_capacity.go +++ b/cmd/retrieve_capacity.go @@ -21,8 +21,7 @@ package cmd import ( - "fmt" - + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -34,12 +33,11 @@ var retrieveCapacityCmd = &cobra.Command{ Retrieve capacities: packet capacity get `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { var err error capacities, _, err := PacknGo.CapacityService.List() if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get Capacity") } header := []string{"Facility", "Plan", "Level"} @@ -53,8 +51,7 @@ packet capacity get } } - outputMergingCells(capacities, header, &requiredDataFormat) - + return outputMergingCells(capacities, header, &requiredDataFormat) }, } diff --git a/cmd/retrieve_device.go b/cmd/retrieve_device.go index c845cae0..67712936 100644 --- a/cmd/retrieve_device.go +++ b/cmd/retrieve_device.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -39,18 +40,15 @@ var retriveDeviceCmd = &cobra.Command{ packet device get --id [device_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if deviceID != "" && projectID != "" { - fmt.Println("Either id or project-id can be set.") - return + return fmt.Errorf("Either id or project-id can be set.") } else if deviceID == "" && projectID == "" { - fmt.Println("Either id or project-id should be set.") - return + return fmt.Errorf("Either id or project-id should be set.") } else if projectID != "" { devices, _, err := PacknGo.Devices.List(projectID, nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Devices") } data := make([][]string, len(devices)) @@ -59,21 +57,20 @@ packet device get --id [device_UUID] } header := []string{"ID", "Hostname", "OS", "State", "Created"} - output(devices, header, &data) + return output(devices, header, &data) } else if deviceID != "" { - device, _, err := PacknGo.Devices.Get(deviceID, nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get Devices") } 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) } + return nil }, } diff --git a/cmd/retrieve_event.go b/cmd/retrieve_event.go index 94cda5b0..275a1eeb 100644 --- a/cmd/retrieve_event.go +++ b/cmd/retrieve_event.go @@ -24,6 +24,7 @@ import ( "fmt" "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -52,50 +53,43 @@ packet event get -d [device_UUID] Retrieve all events of a current user: packet event get `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { var events []packngo.Event var err error header := []string{"ID", "Body", "Type", "Created"} listOpt := &packngo.ListOptions{Includes: []string{"relationships"}} if deviceID != "" && projectID != "" && organizationID != "" && eventID != "" { - fmt.Println("The id, project-id, device-id, and organization-id parameters are mutually exclusive") - return + return fmt.Errorf("The id, project-id, device-id, and organization-id parameters are mutually exclusive") } else if deviceID != "" { events, _, err = PacknGo.Devices.ListEvents(deviceID, listOpt) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Device Events") } } else if projectID != "" { events, _, err = PacknGo.Projects.ListEvents(projectID, listOpt) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Project Events") } } else if organizationID != "" { events, _, err = PacknGo.Organizations.ListEvents(organizationID, listOpt) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Organization Events") } } else if eventID != "" { getOpt := &packngo.GetOptions{Includes: listOpt.Includes} event, _, err := PacknGo.Events.Get(eventID, getOpt) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get Event") } data := make([][]string, 1) data[0] = []string{event.ID, event.Body, event.Type, event.CreatedAt.String()} - output(event, header, &data) - return + return output(event, header, &data) } else { events, _, err = PacknGo.Events.List(listOpt) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Events") } } @@ -105,7 +99,7 @@ packet event get data[i] = []string{event.ID, event.Body, event.Type, event.CreatedAt.String()} } - output(events, header, &data) + return output(events, header, &data) }, } diff --git a/cmd/retrieve_facilities.go b/cmd/retrieve_facilities.go index 961c73fb..2604a396 100644 --- a/cmd/retrieve_facilities.go +++ b/cmd/retrieve_facilities.go @@ -21,9 +21,9 @@ package cmd import ( - "fmt" "strings" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,11 +36,10 @@ var retrieveFacilitiesCmd = &cobra.Command{ packet facilities get `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { facilities, _, err := PacknGo.Facilities.List(nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Facilities") } data := make([][]string, len(facilities)) @@ -49,7 +48,7 @@ packet facilities get } header := []string{"Name", "Code", "Features"} - output(facilities, header, &data) + return output(facilities, header, &data) }, } diff --git a/cmd/retrieve_hardware_reservations.go b/cmd/retrieve_hardware_reservations.go index 6fdb9745..58e5fce3 100644 --- a/cmd/retrieve_hardware_reservations.go +++ b/cmd/retrieve_hardware_reservations.go @@ -24,6 +24,7 @@ import ( "fmt" "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,21 +37,18 @@ var retrieveHardwareReservationsCmd = &cobra.Command{ Retrieve all hardware reservations of a project: packet hardware_reservations get -p [project_id] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { header := []string{"ID", "Facility", "Plan", "Created"} listOpt := &packngo.ListOptions{Includes: []string{"project,facility,device"}} if hardwareReservationID == "" && projectID == "" { - fmt.Println("Either id or project-id should be set.") - return + return fmt.Errorf("Either id or project-id should be set.") } else if hardwareReservationID != "" && projectID != "" { - fmt.Println("Either id or project-id can be set.") - return + return fmt.Errorf("Either id or project-id can be set.") } else if projectID != "" { reservations, _, err := PacknGo.HardwareReservations.List(projectID, listOpt) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Hardware Reservations") } data := make([][]string, len(reservations)) @@ -59,21 +57,21 @@ packet hardware_reservations get -p [project_id] data[i] = []string{r.ID, r.Facility.Code, r.Plan.Name, r.CreatedAt.String()} } - output(reservations, header, &data) + return output(reservations, header, &data) } else if hardwareReservationID != "" { getOpt := &packngo.GetOptions{Includes: listOpt.Includes} r, _, err := PacknGo.HardwareReservations.Get(hardwareReservationID, getOpt) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get Hardware Reservation") } data := make([][]string, 1) data[0] = []string{r.ID, r.Facility.Code, r.Plan.Name, r.CreatedAt.String()} - output(r, header, &data) + return output(r, header, &data) } + return nil }, } diff --git a/cmd/retrieve_ip.go b/cmd/retrieve_ip.go index b98aa671..f8e2c387 100644 --- a/cmd/retrieve_ip.go +++ b/cmd/retrieve_ip.go @@ -24,6 +24,7 @@ import ( "fmt" "strconv" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -51,12 +52,11 @@ To get IP addresses by reservation ID: packet ip get --reservation-id [reservation_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if projectID != "" && assignmentID == "" && reservationID == "" { ips, _, err := PacknGo.ProjectIPs.List(projectID) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Project IP addresses") } data := make([][]string, len(ips)) @@ -70,12 +70,11 @@ packet ip get --reservation-id [reservation_UUID] } header := []string{"ID", "Address", "Facility", "Public", "Created"} - output(ips, header, &data) + return output(ips, header, &data) } else if projectID == "" && reservationID == "" && assignmentID != "" { ip, _, err := PacknGo.DeviceIPs.Get(assignmentID, nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get Device IP address") } data := make([][]string, 1) @@ -83,12 +82,11 @@ packet ip get --reservation-id [reservation_UUID] data[0] = []string{ip.ID, ip.Address, strconv.FormatBool(ip.Public), ip.Created} header := []string{"ID", "Address", "Public", "Created"} - output(ip, header, &data) + return output(ip, header, &data) } else if projectID == "" && assignmentID == "" && reservationID != "" { ip, _, err := PacknGo.ProjectIPs.Get(reservationID, nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get Reservation IP address") } data := make([][]string, 1) @@ -96,10 +94,11 @@ packet ip get --reservation-id [reservation_UUID] data[0] = []string{ip.ID, ip.Address, ip.Facility.Code, strconv.FormatBool(ip.Public), ip.Created} header := []string{"ID", "Address", "Facility", "Public", "Created"} - output(ip, header, &data) + return output(ip, header, &data) } else if (projectID != "" && (assignmentID != "" || reservationID != "")) || (projectID == "" && assignmentID == "" && reservationID == "") { - fmt.Println("Either project-id or assignment-id or reservation-id can be passed as parameters.") + return fmt.Errorf("Either project-id or assignment-id or reservation-id can be passed as parameters.") } + return nil }, } diff --git a/cmd/retrieve_operating_system.go b/cmd/retrieve_operating_system.go index 37a90ec0..9dbfa415 100644 --- a/cmd/retrieve_operating_system.go +++ b/cmd/retrieve_operating_system.go @@ -21,8 +21,7 @@ package cmd import ( - "fmt" - + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -32,11 +31,10 @@ var retrieveOperatingSystemCmd = &cobra.Command{ Short: "Retrieves a list of available operating systems.", Long: `Example: packet operating-systems get`, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { oss, _, err := PacknGo.OperatingSystems.List() if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list OperatingSystems") } data := make([][]string, len(oss)) @@ -46,7 +44,7 @@ var retrieveOperatingSystemCmd = &cobra.Command{ } header := []string{"Name", "Slug", "Distro", "Version"} - output(oss, header, &data) + return output(oss, header, &data) }, } diff --git a/cmd/retrieve_organization.go b/cmd/retrieve_organization.go index 07093126..aa76024a 100644 --- a/cmd/retrieve_organization.go +++ b/cmd/retrieve_organization.go @@ -21,8 +21,7 @@ package cmd import ( - "fmt" - + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -39,12 +38,11 @@ To retrieve a single organization: packet organization get -i [organization-id] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if organizationID == "" { orgs, _, err := PacknGo.Organizations.List(nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Organizations") } data := make([][]string, len(orgs)) @@ -54,12 +52,11 @@ packet organization get -i [organization-id] } header := []string{"ID", "Name", "Created"} - output(orgs, header, &data) + return output(orgs, header, &data) } else { org, _, err := PacknGo.Organizations.Get(organizationID, nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get Organization") } data := make([][]string, 1) @@ -67,7 +64,7 @@ packet organization get -i [organization-id] data[0] = []string{org.ID, org.Name, org.Created} header := []string{"ID", "Name", "Created"} - output(org, header, &data) + return output(org, header, &data) } }, } diff --git a/cmd/retrieve_plans.go b/cmd/retrieve_plans.go index 81c28e7e..840eca5e 100644 --- a/cmd/retrieve_plans.go +++ b/cmd/retrieve_plans.go @@ -21,8 +21,7 @@ package cmd import ( - "fmt" - + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -35,11 +34,10 @@ var retrievePlansCmd = &cobra.Command{ packet plans get `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { plans, _, err := PacknGo.Plans.List(nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Plans") } data := make([][]string, len(plans)) @@ -49,7 +47,7 @@ var retrievePlansCmd = &cobra.Command{ } header := []string{"ID", "Slug", "Name"} - output(plans, header, &data) + return output(plans, header, &data) }, } diff --git a/cmd/retrieve_project.go b/cmd/retrieve_project.go index 0f104909..4b345b97 100644 --- a/cmd/retrieve_project.go +++ b/cmd/retrieve_project.go @@ -24,6 +24,7 @@ import ( "fmt" "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -40,10 +41,9 @@ Retrieve a specific project: packet project get -i [project_UUID] packet project get -n [project_name] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if projectID != "" && projectName != "" { - fmt.Println("must specify only one of project-id and project name") - return + return fmt.Errorf("Must specify only one of project-id and project name") } if projectID == "" { listOpt := &packngo.ListOptions{ @@ -52,8 +52,7 @@ packet project get -n [project_name] projects, _, err := PacknGo.Projects.List(listOpt) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Projects") } var data [][]string @@ -71,25 +70,23 @@ packet project get -n [project_name] } } if len(data) == 0 { - fmt.Println("Error: no project found with name", projectName) - return + return fmt.Errorf("Could not find project with name %q", projectName) } } header := []string{"ID", "Name", "Created"} - output(projects, header, &data) + return output(projects, header, &data) } else { p, _, err := PacknGo.Projects.Get(projectID, &packngo.GetOptions{Includes: []string{"members"}}) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get 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) } }, } diff --git a/cmd/retrieve_ssh_key.go b/cmd/retrieve_ssh_key.go index a3a356ad..ead02e2e 100644 --- a/cmd/retrieve_ssh_key.go +++ b/cmd/retrieve_ssh_key.go @@ -21,8 +21,7 @@ package cmd import ( - "fmt" - + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -43,12 +42,11 @@ Retrieve a specific SSH key: packet ssh-key get --id [ssh-key_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if sshKeyID == "" { sshKeys, _, err := PacknGo.SSHKeys.List() if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list SSH Keys") } data := make([][]string, len(sshKeys)) @@ -58,12 +56,11 @@ packet ssh-key get --id [ssh-key_UUID] } header := []string{"ID", "Label", "Created"} - output(sshKeys, header, &data) + return output(sshKeys, header, &data) } else { sshKey, _, err := PacknGo.SSHKeys.Get(sshKeyID, nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get SSH Key") } data := make([][]string, 1) @@ -71,7 +68,7 @@ packet ssh-key get --id [ssh-key_UUID] data[0] = []string{sshKey.ID, sshKey.Label, sshKey.Created} header := []string{"ID", "Label", "Created"} - output(sshKey, header, &data) + return output(sshKey, header, &data) } }, } diff --git a/cmd/retrieve_user.go b/cmd/retrieve_user.go index eab2512c..c16f96b3 100644 --- a/cmd/retrieve_user.go +++ b/cmd/retrieve_user.go @@ -21,9 +21,8 @@ package cmd import ( - "fmt" - "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -44,20 +43,18 @@ Retrieve a specific user: packet user get --id [user_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { var err error var user *packngo.User if userID == "" { user, _, err = PacknGo.Users.Current() if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get current User") } } else { user, _, err = PacknGo.Users.Get(userID, nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get Users") } } @@ -66,7 +63,7 @@ packet user get --id [user_UUID] data[0] = []string{user.ID, user.FullName, user.Email, user.Created} header := []string{"ID", "Full Name", "Email", "Created"} - output(user, header, &data) + return output(user, header, &data) }, } diff --git a/cmd/retrieve_virtual_network.go b/cmd/retrieve_virtual_network.go index 90994f93..fd98b09b 100644 --- a/cmd/retrieve_virtual_network.go +++ b/cmd/retrieve_virtual_network.go @@ -21,9 +21,9 @@ package cmd import ( - "fmt" "strconv" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,11 +36,10 @@ var retrieveVirtualNetworksCmd = &cobra.Command{ packet virtual-network get -p [project_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { vnets, _, err := PacknGo.ProjectVirtualNetworks.List(projectID, nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Project Virtual Networks") } data := make([][]string, len(vnets.VirtualNetworks)) @@ -50,7 +49,7 @@ packet virtual-network get -p [project_UUID] } header := []string{"ID", "Description", "VXLAN", "Facility", "Created"} - output(vnets, header, &data) + return output(vnets, header, &data) }, } diff --git a/cmd/retrieve_volume.go b/cmd/retrieve_volume.go index 0a6b54b9..bccc0613 100644 --- a/cmd/retrieve_volume.go +++ b/cmd/retrieve_volume.go @@ -24,6 +24,7 @@ import ( "fmt" "strconv" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -44,18 +45,15 @@ Retrieve a specific volume: packet volume get --id [volume_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if projectID != "" && volumeID != "" { - fmt.Println("Either id or project-id can be set.") - return + return fmt.Errorf("Either id or project-id can be set.") } else if projectID == "" && volumeID == "" { - fmt.Println("Either id or project-id should be set.") - return + return fmt.Errorf("Either id or project-id should be set.") } else if projectID != "" { volumes, _, err := PacknGo.Volumes.List(projectID, nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not list Volumes") } data := make([][]string, len(volumes)) @@ -64,21 +62,21 @@ packet volume get --id [volume_UUID] } header := []string{"ID", "Name", "Size", "State", "Created"} - output(volumes, header, &data) + return output(volumes, header, &data) } else if volumeID != "" { v, _, err := PacknGo.Volumes.Get(volumeID, nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get Volume") } header := []string{"ID", "Name", "Size", "State", "Created"} data := make([][]string, 1) data[0] = []string{v.ID, v.Name, strconv.Itoa(v.Size), v.State, v.Created} - output(v, header, &data) + return output(v, header, &data) } + return nil }, } diff --git a/cmd/retrieve_vpn.go b/cmd/retrieve_vpn.go index 213014a4..7929be62 100644 --- a/cmd/retrieve_vpn.go +++ b/cmd/retrieve_vpn.go @@ -21,8 +21,7 @@ package cmd import ( - "fmt" - + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -35,18 +34,17 @@ var retrieveVpnCmd = &cobra.Command{ Enable VPN service: packet vpn get --faciliy ewr1 `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { config, _, err := PacknGo.VPN.Get(facility, nil) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not get VPN") } data := make([][]string, 1) data[0] = []string{config.Config} header := []string{"Config"} - output(config, header, &data) + return output(config, header, &data) }, } diff --git a/cmd/root.go b/cmd/root.go index 516ad196..9e1061c1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,6 +26,7 @@ import ( "runtime" "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -41,24 +42,23 @@ var ( // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ - Use: "packet", - Short: "Command line interface for Packet Host", - Long: `Command line interface for Packet Host`, - PersistentPreRun: packetConnect, + Use: "packet", + Short: "Command line interface for Packet Host", + Long: `Command line interface for Packet Host`, + PersistentPreRunE: packetConnect, } -func packetConnect(cmd *cobra.Command, args []string) { +func packetConnect(cmd *cobra.Command, args []string) error { if packetToken == "" { - fmt.Println("Packet authentication token not provided. Please either set the 'PACKET_TOKEN' environment variable or create a JSON or YAML configuration file.") - os.Exit(1) + return fmt.Errorf("Packet authentication token not provided. Please either set the 'PACKET_TOKEN' environment variable or create a JSON or YAML configuration file.") } client, err := packngo.NewClientWithBaseURL("Packet CLI", packetToken, nil, "https://api.packet.net/") if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not create Client") } client.UserAgent = fmt.Sprintf("packet-cli/%s %s", Version, client.UserAgent) PacknGo = *client + return nil } // Execute adds all child commands to the root command and sets flags appropriately. diff --git a/cmd/start_device.go b/cmd/start_device.go index 029917d5..2fe5d98c 100644 --- a/cmd/start_device.go +++ b/cmd/start_device.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -35,15 +36,15 @@ var startDeviceCmd = &cobra.Command{ packet device start --id [device_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { _, err := PacknGo.Devices.PowerOn(deviceID) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not start Device") } fmt.Println("Device", deviceID, "successfully started.") + return nil }, } diff --git a/cmd/stop_device.go b/cmd/stop_device.go index 01b769f2..f0e07428 100644 --- a/cmd/stop_device.go +++ b/cmd/stop_device.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -35,15 +36,15 @@ var stopDeviceCmd = &cobra.Command{ packet device stop --id [device_UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { _, err := PacknGo.Devices.PowerOff(deviceID) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not stop Device") } fmt.Println("Device", deviceID, "successfully stopped.") + return nil }, } diff --git a/cmd/unassign_ip.go b/cmd/unassign_ip.go index 35bf580f..27210722 100644 --- a/cmd/unassign_ip.go +++ b/cmd/unassign_ip.go @@ -23,6 +23,7 @@ package cmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -35,14 +36,14 @@ var unassignIPCmd = &cobra.Command{ packet ip unassign --id [assignment-UUID] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { _, err := PacknGo.DeviceIPs.Unassign(assignmentID) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not unassign IP address") } fmt.Println("IP address unassigned successfully.") + return nil }, } diff --git a/cmd/update_device.go b/cmd/update_device.go index 2be00474..2c2bccdf 100644 --- a/cmd/update_device.go +++ b/cmd/update_device.go @@ -21,9 +21,8 @@ package cmd import ( - "fmt" - "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -42,7 +41,7 @@ var updateDeviceCmd = &cobra.Command{ packet device update --id [device_UUID] --hostname [new_hostname] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { req := &packngo.DeviceUpdateRequest{} if hostname != "" { @@ -79,15 +78,14 @@ packet device update --id [device_UUID] --hostname [new_hostname] device, _, err := PacknGo.Devices.Update(deviceID, req) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not update Device") } header := []string{"ID", "Hostname", "OS", "State"} data := make([][]string, 1) data[0] = []string{device.ID, device.Hostname, device.OS.Name, device.State} - output(device, header, &data) + return output(device, header, &data) }, } diff --git a/cmd/update_organization.go b/cmd/update_organization.go index 5f241a60..b59cf6e2 100644 --- a/cmd/update_organization.go +++ b/cmd/update_organization.go @@ -21,9 +21,8 @@ package cmd import ( - "fmt" - "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,7 +35,7 @@ var updateOrganizationCmd = &cobra.Command{ packet organization update --id [organization_UUID] --name [new_name] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { req := &packngo.OrganizationUpdateRequest{} if name != "" { @@ -57,7 +56,7 @@ packet organization update --id [organization_UUID] --name [new_name] org, _, err := PacknGo.Organizations.Update(organizationID, req) if err != nil { - fmt.Println("Client error:", err) + return errors.Wrap(err, "Could not update Organization") } data := make([][]string, 1) @@ -65,7 +64,7 @@ packet organization update --id [organization_UUID] --name [new_name] data[0] = []string{org.ID, org.Name, org.Created} header := []string{"ID", "Name", "Created"} - output(org, header, &data) + return output(org, header, &data) }, } diff --git a/cmd/update_project.go b/cmd/update_project.go index 968f3c36..d649babf 100644 --- a/cmd/update_project.go +++ b/cmd/update_project.go @@ -21,9 +21,8 @@ package cmd import ( - "fmt" - "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,7 +35,7 @@ var updateProjectCmd = &cobra.Command{ packet project update --id [project_UUID] --name [new_name] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { req := &packngo.ProjectUpdateRequest{} if name != "" { req.Name = &name @@ -47,15 +46,14 @@ packet project update --id [project_UUID] --name [new_name] } p, _, err := PacknGo.Projects.Update(projectID, req) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not update 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) }, } diff --git a/cmd/update_ssh-key.go b/cmd/update_ssh-key.go index ed585e26..46c69bc1 100644 --- a/cmd/update_ssh-key.go +++ b/cmd/update_ssh-key.go @@ -21,13 +21,12 @@ package cmd import ( - "fmt" - "github.com/packethost/packngo" + "github.com/pkg/errors" "github.com/spf13/cobra" ) -// updateProjectCmd represents the updateProject command +// updateSSHKeyCmd represents the updateSSHKey command var updateSSHKeyCmd = &cobra.Command{ Use: "update", Short: "Updates a project", @@ -36,7 +35,7 @@ var updateSSHKeyCmd = &cobra.Command{ packet ssh-key update --id [ssh-key_UUID] --key [new_key] `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { req := &packngo.SSHKeyUpdateRequest{} if key != "" { req.Key = &key @@ -47,8 +46,7 @@ packet ssh-key update --id [ssh-key_UUID] --key [new_key] } sshKey, _, err := PacknGo.SSHKeys.Update(sshKeyID, req) if err != nil { - fmt.Println("Client error:", err) - return + return errors.Wrap(err, "Could not update SSH Key") } data := make([][]string, 1) @@ -56,7 +54,7 @@ packet ssh-key update --id [ssh-key_UUID] --key [new_key] data[0] = []string{sshKey.ID, sshKey.Label, sshKey.Created} header := []string{"ID", "Label", "Created"} - output(sshKey, header, &data) + return output(sshKey, header, &data) }, } diff --git a/docs/packet_ip_assign.md b/docs/packet_ip_assign.md index 3db40751..d89aa581 100644 --- a/docs/packet_ip_assign.md +++ b/docs/packet_ip_assign.md @@ -17,7 +17,7 @@ packet ip assign [flags] ### Options ``` - -a, --address string IP Address + -a, --address string IP address -d, --device-id string UUID of the device -h, --help help for assign ``` @@ -32,4 +32,4 @@ packet ip assign [flags] * [packet ip](packet_ip.md) - IP operations -###### Auto generated by spf13/cobra on 14-Apr-2020 +###### Auto generated by spf13/cobra on 8-Aug-2020 diff --git a/go.mod b/go.mod index fb9a8007..c1ae16d3 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/mattn/go-runewidth v0.0.2 // indirect github.com/olekukonko/tablewriter v0.0.0-20180506121414-d4647c9c7a84 github.com/packethost/packngo v0.2.0 + github.com/pkg/errors v0.8.0 github.com/spf13/cobra v1.0.0 github.com/spf13/viper v1.4.0 golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 diff --git a/go.sum b/go.sum index b78b1d87..bae52f8b 100644 --- a/go.sum +++ b/go.sum @@ -89,6 +89,7 @@ github.com/packethost/packngo v0.2.0 h1:mSlzOof8PsOWCy78sBMt/PwMJTEjjQ/rRvMixu4N github.com/packethost/packngo v0.2.0/go.mod h1:RQHg5xR1F614BwJyepfMqrKN+32IH0i7yX+ey43rEeQ= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=