diff --git a/cmd/account/account.go b/cmd/account/account.go index 500dd6d..5c52cb7 100644 --- a/cmd/account/account.go +++ b/cmd/account/account.go @@ -4,6 +4,7 @@ package account import ( "context" "errors" + "fmt" "github.com/spf13/cobra" "github.com/vultr/govultr/v3" @@ -34,10 +35,15 @@ func NewCmdAccount(base *cli.Base) *cobra.Command { } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { account, err := o.get() + if err != nil { + return fmt.Errorf("error retrieving account information : %v", err) + } + + o.Base.Printer.Display(&AccountPrinter{Account: account}, nil) - o.Base.Printer.Display(&AccountPrinter{Account: account}, err) + return nil }, } diff --git a/cmd/backups/backups.go b/cmd/backups/backups.go index 9d38d08..ca2ed38 100644 --- a/cmd/backups/backups.go +++ b/cmd/backups/backups.go @@ -4,11 +4,9 @@ package backups import ( "errors" "fmt" - "os" "github.com/spf13/cobra" "github.com/vultr/govultr/v3" - "github.com/vultr/vultr-cli/v3/cmd/printer" "github.com/vultr/vultr-cli/v3/cmd/utils" "github.com/vultr/vultr-cli/v3/pkg/cli" ) @@ -48,15 +46,16 @@ func NewCmdBackups(base *cli.Base) *cobra.Command { Aliases: []string{"l"}, Long: listLong, Example: listExample, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) backups, meta, err := o.list() if err != nil { - printer.Error(fmt.Errorf("error retrieving backups list : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving backups list : %v", err) } data := &BackupsPrinter{Backups: backups, Meta: meta} o.Base.Printer.Display(data, err) + + return nil }, } @@ -80,14 +79,16 @@ func NewCmdBackups(base *cli.Base) *cobra.Command { } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { backup, err := o.get() if err != nil { - panic(fmt.Errorf("error retrieving backup : %v", err)) + return fmt.Errorf("error retrieving backup : %v", err) } data := &BackupPrinter{Backup: backup} o.Base.Printer.Display(data, err) + + return nil }, } diff --git a/cmd/baremetal/baremetal.go b/cmd/baremetal/baremetal.go index 2c43f41..965e60f 100644 --- a/cmd/baremetal/baremetal.go +++ b/cmd/baremetal/baremetal.go @@ -147,15 +147,16 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo Aliases: []string{"l"}, Long: listLong, Example: listExample, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) list, meta, err := o.list() if err != nil { - printer.Error(fmt.Errorf("error retrieving bare metal list : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving bare metal list : %v", err) } data := &BareMetalsPrinter{BareMetals: list, Meta: meta} o.Base.Printer.Display(data, err) + + return nil }, } @@ -180,14 +181,15 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { bm, err := o.get() if err != nil { - printer.Error(fmt.Errorf("error retrieving bare metal : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving bare metal : %v", err) } data := &BareMetalPrinter{BareMetal: *bm} o.Base.Printer.Display(data, err) + + return nil }, } @@ -198,16 +200,23 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo Aliases: []string{"c"}, Long: createLong, Example: createExample, - Run: func(cmd *cobra.Command, args []string) { - o.CreateReq = parseCreateFlags(cmd) + RunE: func(cmd *cobra.Command, args []string) error { + req, errParse := parseCreateFlags(cmd) + if errParse != nil { + return fmt.Errorf("error parsing flags for bare metal create : %v", errParse) + } + + o.CreateReq = req + bm, err := o.create() if err != nil { - printer.Error(fmt.Errorf("error with bare metal create : %v", err)) - os.Exit(1) + return fmt.Errorf("error with bare metal create : %v", err) } data := &BareMetalPrinter{BareMetal: *bm} o.Base.Printer.Display(data, err) + + return nil }, } @@ -265,12 +274,12 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo create.Flags().BoolP("persistent_pxe", "x", false, "enable persistent_pxe | true or false") if err := create.MarkFlagRequired("region"); err != nil { - printer.Error(fmt.Errorf("error marking bare metal create 'region' flag required: %v", err)) + fmt.Printf("error marking bare metal create 'region' flag required: %v", err) os.Exit(1) } if err := create.MarkFlagRequired("plan"); err != nil { - printer.Error(fmt.Errorf("error marking bare metal create 'plan' flag required: %v", err)) + fmt.Printf("error marking bare metal create 'plan' flag required: %v", err) os.Exit(1) } @@ -291,12 +300,13 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := o.del(); err != nil { - printer.Error(fmt.Errorf("error deleting bare metal : %v", err)) - os.Exit(1) + return fmt.Errorf("error deleting bare metal : %v", err) } o.Base.Printer.Display(printer.Info("bare metal server has been deleted"), nil) + + return nil }, } @@ -313,12 +323,13 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := o.halt(); err != nil { - printer.Error(fmt.Errorf("error halting bare metal : %v", err)) - os.Exit(1) + return fmt.Errorf("error halting bare metal : %v", err) } o.Base.Printer.Display(printer.Info("bare metal server has been halted"), nil) + + return nil }, } @@ -334,13 +345,14 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := o.start(); err != nil { - printer.Error(fmt.Errorf("error starting bare metal : %v", err)) - os.Exit(1) + return fmt.Errorf("error starting bare metal : %v", err) } o.Base.Printer.Display(printer.Info("bare metal server has been started"), nil) + + return nil }, } @@ -357,13 +369,14 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := o.reboot(); err != nil { - printer.Error(fmt.Errorf("error rebooting bare metal : %v", err)) - os.Exit(1) + return fmt.Errorf("error rebooting bare metal : %v", err) } o.Base.Printer.Display(printer.Info("bare metal server has been rebooted"), nil) + + return nil }, } @@ -379,13 +392,14 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := o.reinstall(); err != nil { - printer.Error(fmt.Errorf("error reinstalling bare metal : %v", err)) - os.Exit(1) + return fmt.Errorf("error reinstalling bare metal : %v", err) } o.Base.Printer.Display(printer.Info("bare metal server has initiated reinstallation"), nil) + + return nil }, } @@ -411,11 +425,10 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { appID, err := cmd.Flags().GetInt("app") if err != nil { - printer.Error(fmt.Errorf("error parsing app flag for bare metal app ID change : %v", err)) - os.Exit(1) + return fmt.Errorf("error parsing app flag for bare metal app ID change : %v", err) } o.UpdateReq = &govultr.BareMetalUpdate{ @@ -424,12 +437,13 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo bm, err := o.update() if err != nil { - printer.Error(fmt.Errorf("error with bare metal update : %v", err)) - os.Exit(1) + return fmt.Errorf("error with bare metal update : %v", err) } data := &BareMetalPrinter{BareMetal: *bm} o.Base.Printer.Display(data, err) + + return nil }, } @@ -441,7 +455,7 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo ) if err := applicationChange.MarkFlagRequired("app"); err != nil { - printer.Error(fmt.Errorf("error marking bare metal 'app' flag required : %v", err)) + fmt.Printf("error marking bare metal 'app' flag required : %v", err) os.Exit(1) } @@ -457,14 +471,15 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { upgrades, err := o.getUpgrades() if err != nil { - printer.Error(fmt.Errorf("error with bare metal get upgrades : %v", err)) - os.Exit(1) + return fmt.Errorf("error with bare metal get upgrades : %v", err) } data := &applications.ApplicationsPrinter{Applications: upgrades.Applications} o.Base.Printer.Display(data, err) + + return nil }, } @@ -492,11 +507,10 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { imageID, err := cmd.Flags().GetString("image") if err != nil { - printer.Error(fmt.Errorf("error parsing image flag for bare metal image change : %v", err)) - os.Exit(1) + return fmt.Errorf("error parsing image flag for bare metal image change : %v", err) } o.UpdateReq = &govultr.BareMetalUpdate{ @@ -505,12 +519,13 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo bm, err := o.update() if err != nil { - printer.Error(fmt.Errorf("error with bare metal image update : %v", err)) - os.Exit(1) + return fmt.Errorf("error with bare metal image update : %v", err) } data := &BareMetalPrinter{BareMetal: *bm} o.Base.Printer.Display(data, err) + + return nil }, } @@ -522,7 +537,7 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo ) if err := imageChange.MarkFlagRequired("image"); err != nil { - printer.Error(fmt.Errorf("error marking bare metal 'image' flag required : %v", err)) + fmt.Printf("error marking bare metal 'image' flag required : %v", err) os.Exit(1) } @@ -550,11 +565,10 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { operatingSystemID, err := cmd.Flags().GetInt("os") if err != nil { - printer.Error(fmt.Errorf("error parsing os flag for bare metal os change : %v", err)) - os.Exit(1) + return fmt.Errorf("error parsing os flag for bare metal os change : %v", err) } o.UpdateReq = &govultr.BareMetalUpdate{ @@ -563,12 +577,13 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo bm, err := o.update() if err != nil { - printer.Error(fmt.Errorf("error with bare metal os update : %v", err)) - os.Exit(1) + return fmt.Errorf("error with bare metal os update : %v", err) } data := &BareMetalPrinter{BareMetal: *bm} o.Base.Printer.Display(data, err) + + return nil }, } @@ -579,7 +594,7 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo "ID of the operating system that will be installed on the server", ) if err := operatingSystemChange.MarkFlagRequired("os"); err != nil { - printer.Error(fmt.Errorf("error marking bare metal 'os' flag required : %v", err)) + fmt.Printf("error marking bare metal 'os' flag required : %v", err) os.Exit(1) } @@ -595,15 +610,16 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { upgrades, err := o.getUpgrades() if err != nil { - printer.Error(fmt.Errorf("error with bare metal get upgrades : %v", err)) - os.Exit(1) + return fmt.Errorf("error with bare metal get upgrades : %v", err) } data := &operatingsystems.OSPrinter{OperatingSystems: upgrades.OS} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -631,15 +647,16 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { ud, err := o.getUserData() if err != nil { - printer.Error(fmt.Errorf("error with bare metal get user data : %v", err)) - os.Exit(1) + return fmt.Errorf("error with bare metal get user data : %v", err) } data := &userdata.UserDataPrinter{UserData: *ud} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -655,17 +672,15 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { path, err := cmd.Flags().GetString("user-data") if err != nil { - printer.Error(fmt.Errorf("error parsing user-data flag for bare metal user data set : %v", err)) - os.Exit(1) + return fmt.Errorf("error parsing user-data flag for bare metal user data set : %v", err) } rawData, err := os.ReadFile(filepath.Clean(path)) if err != nil { - printer.Error(fmt.Errorf("error reading user-data : %v", err)) - os.Exit(1) + return fmt.Errorf("error reading user-data : %v", err) } o.UpdateReq = &govultr.BareMetalUpdate{ @@ -674,17 +689,18 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo _, errUpdate := o.update() if err != nil { - printer.Error(fmt.Errorf("error updating bare metal user-data : %v", errUpdate)) - os.Exit(1) + return fmt.Errorf("error updating bare metal user-data : %v", errUpdate) } o.Base.Printer.Display(printer.Info("bare metal server user data has been set"), nil) + + return nil }, } userDataSet.Flags().StringP("user-data", "d", "/dev/stdin", "file to read userdata from") if err := userDataSet.MarkFlagRequired("user-data"); err != nil { - printer.Error(fmt.Errorf("error marking bare metal 'user-data' flag required: %v", err)) + fmt.Printf("error marking bare metal 'user-data' flag required: %v", err) os.Exit(1) } @@ -702,15 +718,16 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { vnc, err := o.getVNCURL() if err != nil { - printer.Error(fmt.Errorf("error retrieving bare metal VNC URL : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving bare metal VNC URL : %v", err) } data := &BareMetalVNCPrinter{VNC: *vnc} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -727,15 +744,16 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { bw, err := o.getBandwidth() if err != nil { - printer.Error(fmt.Errorf("error retrieving bare metal bandwidth usage : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving bare metal bandwidth usage : %v", err) } data := &BareMetalBandwidthPrinter{Bandwidth: *bw} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -751,7 +769,7 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { tags, _ := cmd.Flags().GetStringSlice("tags") o.UpdateReq = &govultr.BareMetalUpdate{ Tags: tags, @@ -759,17 +777,18 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo _, err := o.update() if err != nil { - printer.Error(fmt.Errorf("error updating bare metal tags : %v", err)) - os.Exit(1) + return fmt.Errorf("error updating bare metal tags : %v", err) } o.Base.Printer.Display(printer.Info("bare metal server tags have been updated"), nil) + + return nil }, } tags.Flags().StringSliceP("tags", "t", []string{}, "A comma separated list of tags to apply to the server") if err := tags.MarkFlagRequired("tags"); err != nil { - printer.Error(fmt.Errorf("error marking bare metal 'tags' flag required: %v", err)) + fmt.Printf("error marking bare metal 'tags' flag required: %v", err) os.Exit(1) } @@ -785,15 +804,16 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) ipv4, meta, err := o.getIPv4Addresses() if err != nil { - printer.Error(fmt.Errorf("error retrieving bare metal IPv4 information : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving bare metal IPv4 information : %v", err) } data := &ip.IPv4sPrinter{IPv4s: ipv4, Meta: meta} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -809,15 +829,16 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) ipv6, meta, err := o.getIPv6Addresses() if err != nil { - printer.Error(fmt.Errorf("error retrieving bare metal IPv6 information : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving bare metal IPv6 information : %v", err) } data := &ip.IPv6sPrinter{IPv6s: ipv6, Meta: meta} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -841,14 +862,15 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { vpc2s, err := o.vpc2NetworksList() if err != nil { - printer.Error(fmt.Errorf("error retrieving bare metal vpc2 information : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving bare metal vpc2 information : %v", err) } data := &BareMetalVPC2sPrinter{VPC2s: vpc2s} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -864,17 +886,15 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { vpcID, errID := cmd.Flags().GetString("vpc-id") if errID != nil { - printer.Error(fmt.Errorf("error parsing vpc-id flag for bare metal VPC2 attach : %v", errID)) - os.Exit(1) + return fmt.Errorf("error parsing vpc-id flag for bare metal VPC2 attach : %v", errID) } IPAddress, errIP := cmd.Flags().GetString("ip-address") if errIP != nil { - printer.Error(fmt.Errorf("error parsing ip-address flag for bare metal VPC2 attach : %v", errIP)) - os.Exit(1) + return fmt.Errorf("error parsing ip-address flag for bare metal VPC2 attach : %v", errIP) } o.VPC2Req = &govultr.AttachVPC2Req{ @@ -883,18 +903,19 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } if err := o.vpc2NetworksAttach(); err != nil { - printer.Error(fmt.Errorf("error attaching bare metal to VPC2 : %v", err)) - os.Exit(1) + return fmt.Errorf("error attaching bare metal to VPC2 : %v", err) } o.Base.Printer.Display(printer.Info("bare metal server has been attached to VPC2 network"), nil) + + return nil }, } vpc2Attach.Flags().StringP("vpc-id", "v", "", "the ID of the VPC 2.0 network you wish to attach") vpc2Attach.Flags().StringP("ip-address", "i", "", "the IP address to use for this server on the attached VPC 2.0 network") if errVPC := vpc2Attach.MarkFlagRequired("vpc-id"); errVPC != nil { - printer.Error(fmt.Errorf("error marking bare metal 'vpc-id' flag required for attach : %v", errVPC)) + fmt.Printf("error marking bare metal 'vpc-id' flag required for attach : %v", errVPC) os.Exit(1) } @@ -910,26 +931,26 @@ func NewCmdBareMetal(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { vpcID, errID := cmd.Flags().GetString("vpc-id") if errID != nil { - printer.Error(fmt.Errorf("error parsing vpc-id flag for bare metal VPC2 detach : %v", errID)) - os.Exit(1) + return fmt.Errorf("error parsing vpc-id flag for bare metal VPC2 detach : %v", errID) } o.VPC2ID = vpcID if err := o.vpc2NetworksDetach(); err != nil { - printer.Error(fmt.Errorf("error detaching bare metal VPC2 : %v", err)) - os.Exit(1) + return fmt.Errorf("error detaching bare metal VPC2 : %v", err) } o.Base.Printer.Display(printer.Info("bare metal server has been detached from VPC2 network"), nil) + + return nil }, } vpc2Detach.Flags().StringP("vpc-id", "v", "", "the ID of the VPC 2.0 network you wish to detach") if errVPC2 := vpc2Detach.MarkFlagRequired("vpc-id"); errVPC2 != nil { - printer.Error(fmt.Errorf("error marking bare metal 'vpc-id' flag required for detach : %v", errVPC2)) + fmt.Printf("error marking bare metal 'vpc-id' flag required for detach : %v", errVPC2) os.Exit(1) } @@ -1053,101 +1074,85 @@ func (b *options) vpc2NetworksDetach() error { // ============================ -func parseCreateFlags(cmd *cobra.Command) *govultr.BareMetalCreate { //nolint:funlen,gocyclo +func parseCreateFlags(cmd *cobra.Command) (*govultr.BareMetalCreate, error) { //nolint:funlen,gocyclo region, err := cmd.Flags().GetString("region") if err != nil { - printer.Error(fmt.Errorf("error parsing region flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing region flag for bare metal create : %v", err) } plan, err := cmd.Flags().GetString("plan") if err != nil { - printer.Error(fmt.Errorf("error parsing plan flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing plan flag for bare metal create : %v", err) } osID, err := cmd.Flags().GetInt("os") if err != nil { - printer.Error(fmt.Errorf("error parsing os flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing os flag for bare metal create : %v", err) } script, err := cmd.Flags().GetString("script") if err != nil { - printer.Error(fmt.Errorf("error parsing script flag bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing script flag bare metal create : %v", err) } snapshot, err := cmd.Flags().GetString("snapshot") if err != nil { - printer.Error(fmt.Errorf("error parsing snapshot flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing snapshot flag for bare metal create : %v", err) } ipv6, err := cmd.Flags().GetString("ipv6") if err != nil { - printer.Error(fmt.Errorf("error parsing ipv6 flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing ipv6 flag for bare metal create : %v", err) } label, err := cmd.Flags().GetString("label") if err != nil { - printer.Error(fmt.Errorf("error parsing label flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing label flag for bare metal create : %v", err) } sshKeys, err := cmd.Flags().GetStringSlice("ssh") if err != nil { - printer.Error(fmt.Errorf("error parsing ssh flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing ssh flag for bare metal create : %v", err) } app, err := cmd.Flags().GetInt("app") if err != nil { - printer.Error(fmt.Errorf("error parsing app flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing app flag for bare metal create : %v", err) } userdata, err := cmd.Flags().GetString("userdata") if err != nil { - printer.Error(fmt.Errorf("error parsing userdata flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing userdata flag for bare metal create : %v", err) } notify, err := cmd.Flags().GetString("notify") if err != nil { - printer.Error(fmt.Errorf("error parsing notify flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing notify flag for bare metal create : %v", err) } hostname, err := cmd.Flags().GetString("hostname") if err != nil { - printer.Error(fmt.Errorf("error parsing hostname flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing hostname flag for bare metal create : %v", err) } tags, err := cmd.Flags().GetStringSlice("tags") if err != nil { - printer.Error(fmt.Errorf("error parsing tags flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing tags flag for bare metal create : %v", err) } ripv4, err := cmd.Flags().GetString("ripv4") if err != nil { - printer.Error(fmt.Errorf("error parsing ripv4 flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing ripv4 flag for bare metal create : %v", err) } pxe, err := cmd.Flags().GetBool("persistenterrpxe") if err != nil { - printer.Error(fmt.Errorf("error parsing persistenterrpxe flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing persistenterrpxe flag for bare metal create : %v", err) } image, err := cmd.Flags().GetString("image") if err != nil { - printer.Error(fmt.Errorf("error parsing image flag for bare metal create : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing image flag for bare metal create : %v", err) } options := &govultr.BareMetalCreate{ @@ -1177,44 +1182,38 @@ func parseCreateFlags(cmd *cobra.Command) *govultr.BareMetalCreate { //nolint:fu options.EnableIPv6 = govultr.BoolToBoolPtr(true) } - return options + return options, nil } -func parseUpdateFlags(cmd *cobra.Command) *govultr.BareMetalUpdate { //nolint:unused +func parseUpdateFlags(cmd *cobra.Command) (*govultr.BareMetalUpdate, error) { //nolint:unused osID, err := cmd.Flags().GetInt("os") if err != nil { - printer.Error(fmt.Errorf("error parsing os flag for bare metal update : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing os flag for bare metal update : %v", err) } label, err := cmd.Flags().GetString("label") if err != nil { - printer.Error(fmt.Errorf("error parsing label flag for bare metal update : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing label flag for bare metal update : %v", err) } app, err := cmd.Flags().GetInt("app") if err != nil { - printer.Error(fmt.Errorf("error parsing app flag for bare metal update : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing app flag for bare metal update : %v", err) } userdata, err := cmd.Flags().GetString("userdata") if err != nil { - printer.Error(fmt.Errorf("error parsing userdata flag for bare metal update : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing userdata flag for bare metal update : %v", err) } tags, err := cmd.Flags().GetStringSlice("tags") if err != nil { - printer.Error(fmt.Errorf("error parsing tags flag for bare metal update : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing tags flag for bare metal update : %v", err) } image, err := cmd.Flags().GetString("image") if err != nil { - printer.Error(fmt.Errorf("error parsing image flag for bare metal update : %v", err)) - os.Exit(1) + return nil, fmt.Errorf("error parsing image flag for bare metal update : %v", err) } options := &govultr.BareMetalUpdate{ @@ -1228,5 +1227,5 @@ func parseUpdateFlags(cmd *cobra.Command) *govultr.BareMetalUpdate { //nolint:un options.UserData = base64.StdEncoding.EncodeToString([]byte(userdata)) } - return options + return options, nil } diff --git a/cmd/billing/billing.go b/cmd/billing/billing.go index 0372271..fad7a7d 100644 --- a/cmd/billing/billing.go +++ b/cmd/billing/billing.go @@ -4,12 +4,10 @@ package billing import ( "errors" "fmt" - "os" "strconv" "github.com/spf13/cobra" "github.com/vultr/govultr/v3" - "github.com/vultr/vultr-cli/v3/cmd/printer" "github.com/vultr/vultr-cli/v3/cmd/utils" "github.com/vultr/vultr-cli/v3/pkg/cli" ) @@ -109,15 +107,16 @@ func NewCmdBilling(base *cli.Base) *cobra.Command { Aliases: []string{"l"}, Long: invoiceListLong, Example: invoiceListExample, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) invs, meta, err := o.listInvoices() if err != nil { - printer.Error(fmt.Errorf("error retrieving billing invoice list : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving billing invoice list : %v", err) } data := &BillingInvoicesPrinter{Invoices: invs, Meta: meta} o.Base.Printer.Display(data, err) + + return nil }, } @@ -142,15 +141,16 @@ func NewCmdBilling(base *cli.Base) *cobra.Command { } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { inv, err := o.get() if err != nil { - printer.Error(fmt.Errorf("error getting invoice : %v", err)) - os.Exit(1) + return fmt.Errorf("error getting invoice : %v", err) } data := &BillingInvoicePrinter{Invoice: *inv} o.Base.Printer.Display(data, err) + + return nil }, } @@ -167,23 +167,23 @@ func NewCmdBilling(base *cli.Base) *cobra.Command { } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) id, errConv := strconv.Atoi(args[0]) if errConv != nil { - printer.Error(fmt.Errorf("error converting invoice item id : %v", errConv)) - os.Exit(1) + return fmt.Errorf("error converting invoice item id : %v", errConv) } o.InvoiceItemID = id items, meta, err := o.listInvoiceItems() if err != nil { - printer.Error(fmt.Errorf("error retrieving billing invoice item list : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving billing invoice item list : %v", err) } data := &BillingInvoiceItemsPrinter{InvoiceItems: items, Meta: meta} o.Base.Printer.Display(data, err) + + return nil }, } @@ -217,15 +217,16 @@ func NewCmdBilling(base *cli.Base) *cobra.Command { Aliases: []string{"l"}, Long: historyListLong, Example: historyListExample, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) hs, meta, err := o.listHistory() if err != nil { - printer.Error(fmt.Errorf("error retrieving billing history list : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving billing history list : %v", err) } data := &BillingHistoryPrinter{Billing: hs, Meta: meta} o.Base.Printer.Display(data, err) + + return nil }, } diff --git a/cmd/blockstorage/blockstorage.go b/cmd/blockstorage/blockstorage.go index 7694235..1cbff4c 100644 --- a/cmd/blockstorage/blockstorage.go +++ b/cmd/blockstorage/blockstorage.go @@ -119,16 +119,17 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo Aliases: []string{"l"}, Long: listLong, Example: listExample, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) bss, meta, err := o.list() if err != nil { - printer.Error(fmt.Errorf("error retrieving block storage list : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving block storage list : %v", err) } data := &BlockStoragesPrinter{BlockStorages: bss, Meta: meta} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -153,15 +154,16 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { bs, err := o.get() if err != nil { - printer.Error(fmt.Errorf("error retrieving block storage : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving block storage : %v", err) } data := &BlockStoragePrinter{BlockStorage: bs} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -172,29 +174,25 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo Aliases: []string{"c"}, Long: createLong, Example: createExample, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { reg, errRg := cmd.Flags().GetString("region") if errRg != nil { - printer.Error(fmt.Errorf("error parsing 'region' flag for block storage create : %v", errRg)) - os.Exit(1) + return fmt.Errorf("error parsing 'region' flag for block storage create : %v", errRg) } size, errSz := cmd.Flags().GetInt("size") if errSz != nil { - printer.Error(fmt.Errorf("error parsing 'size' flag for block storage create : %v", errSz)) - os.Exit(1) + return fmt.Errorf("error parsing 'size' flag for block storage create : %v", errSz) } label, errLa := cmd.Flags().GetString("label") if errLa != nil { - printer.Error(fmt.Errorf("error parsing 'label' flag for block storage create : %v", errLa)) - os.Exit(1) + return fmt.Errorf("error parsing 'label' flag for block storage create : %v", errLa) } blockType, errBt := cmd.Flags().GetString("block-type") if errBt != nil { - printer.Error(fmt.Errorf("error parsing 'block-type' flag for block storage create : %v", errBt)) - os.Exit(1) + return fmt.Errorf("error parsing 'block-type' flag for block storage create : %v", errBt) } o.CreateReq = &govultr.BlockStorageCreate{ @@ -206,12 +204,13 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo bs, err := o.create() if err != nil { - printer.Error(fmt.Errorf("error creating block storage : %v", err)) - os.Exit(1) + return fmt.Errorf("error creating block storage : %v", err) } data := &BlockStoragePrinter{BlockStorage: bs} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -250,13 +249,14 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := o.del(); err != nil { - printer.Error(fmt.Errorf("error deleting block storage : %v", err)) - os.Exit(1) + return fmt.Errorf("error deleting block storage : %v", err) } o.Base.Printer.Display(printer.Info("block storage has been deleted"), nil) + + return nil }, } @@ -273,17 +273,15 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { instance, errIe := cmd.Flags().GetString("instance") if errIe != nil { - printer.Error(fmt.Errorf("error parsing 'instance' flag for block storage attach : %v", errIe)) - os.Exit(1) + return fmt.Errorf("error parsing 'instance' flag for block storage attach : %v", errIe) } live, errLe := cmd.Flags().GetBool("live") if errLe != nil { - printer.Error(fmt.Errorf("error parsing 'live' flag for block storage attach : %v", errLe)) - os.Exit(1) + return fmt.Errorf("error parsing 'live' flag for block storage attach : %v", errLe) } o.AttachReq = &govultr.BlockStorageAttach{ @@ -292,11 +290,12 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo } if err := o.attach(); err != nil { - printer.Error(fmt.Errorf("error attaching block storage : %v", err)) - os.Exit(1) + return fmt.Errorf("error attaching block storage : %v", err) } o.Base.Printer.Display(printer.Info("block storage has been attached"), nil) + + return nil }, } @@ -320,11 +319,10 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { live, errLe := cmd.Flags().GetBool("live") if errLe != nil { - printer.Error(fmt.Errorf("error parsing 'live' flag for block storage detach : %v", errLe)) - os.Exit(1) + return fmt.Errorf("error parsing 'live' flag for block storage detach : %v", errLe) } o.DetachReq = &govultr.BlockStorageDetach{ @@ -332,11 +330,12 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo } if err := o.detach(); err != nil { - printer.Error(fmt.Errorf("error detaching block storage : %v", err)) - os.Exit(1) + return fmt.Errorf("error detaching block storage : %v", err) } o.Base.Printer.Display(printer.Info("block storage has been detached"), nil) + + return nil }, } @@ -354,11 +353,10 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { label, errLl := cmd.Flags().GetString("label") if errLl != nil { - printer.Error(fmt.Errorf("error parsing 'label' flag for block storage : %v", errLl)) - os.Exit(1) + return fmt.Errorf("error parsing 'label' flag for block storage : %v", errLl) } o.UpdateReq = &govultr.BlockStorageUpdate{ @@ -366,11 +364,12 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo } if err := o.update(); err != nil { - printer.Error(fmt.Errorf("error updating block storage label : %v", err)) - os.Exit(1) + return fmt.Errorf("error updating block storage label : %v", err) } o.Base.Printer.Display(printer.Info("block storage label has been updated"), nil) + + return nil }, } @@ -393,11 +392,10 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { size, errSz := cmd.Flags().GetInt("size") if errSz != nil { - printer.Error(fmt.Errorf("error parsing 'size' flag for block storage resize : %v", errSz)) - os.Exit(1) + return fmt.Errorf("error parsing 'size' flag for block storage resize : %v", errSz) } o.UpdateReq = &govultr.BlockStorageUpdate{ @@ -405,11 +403,12 @@ func NewCmdBlockStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo } if err := o.update(); err != nil { - printer.Error(fmt.Errorf("error resizing block storage : %v", err)) - os.Exit(1) + return fmt.Errorf("error resizing block storage : %v", err) } o.Base.Printer.Display(printer.Info("block storage has been resized"), nil) + + return nil }, } diff --git a/cmd/dns/dns.go b/cmd/dns/dns.go index 16382bb..f9e44aa 100644 --- a/cmd/dns/dns.go +++ b/cmd/dns/dns.go @@ -53,17 +53,18 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo domainList := &cobra.Command{ Use: "list", Short: "Get a list of domains", - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) dms, meta, err := o.domainList() if err != nil { - printer.Error(fmt.Errorf("error retrieving domain list : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving domain list : %v", err) } data := &DNSDomainsPrinter{Domains: dms, Meta: meta} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -80,15 +81,16 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo Use: "get ", Short: "Get a domain", Long: ``, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { dm, err := o.domainGet() if err != nil { - printer.Error(fmt.Errorf("error retrieving domain : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving domain : %v", err) } data := &DNSDomainPrinter{Domain: *dm} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -98,17 +100,15 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo Short: "Create a domain", Long: createLong, Example: createExample, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { domain, errDo := cmd.Flags().GetString("domain") if errDo != nil { - printer.Error(fmt.Errorf("error parsing 'domain' flag for domain create : %v", errDo)) - os.Exit(1) + return fmt.Errorf("error parsing 'domain' flag for domain create : %v", errDo) } ip, errIP := cmd.Flags().GetString("ip") if errIP != nil { - printer.Error(fmt.Errorf("error parsing 'ip' flag for domain create : %v", errIP)) - os.Exit(1) + return fmt.Errorf("error parsing 'ip' flag for domain create : %v", errIP) } o.DomainCreateReq = &govultr.DomainReq{ @@ -118,18 +118,19 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo dm, err := o.domainCreate() if err != nil { - printer.Error(fmt.Errorf("error creating dns domain : %v", err)) - os.Exit(1) + return fmt.Errorf("error creating dns domain : %v", err) } data := &DNSDomainPrinter{Domain: *dm} o.Base.Printer.Display(data, nil) + + return nil }, } domainCreate.Flags().StringP("domain", "d", "", "name of the domain") if err := domainCreate.MarkFlagRequired("domain"); err != nil { - printer.Error(fmt.Errorf("error marking domain create 'domain' flag required: %v", err)) + fmt.Printf("error marking domain create 'domain' flag required: %v", err) os.Exit(1) } domainCreate.Flags().StringP("ip", "i", "", "instance ip you want to assign this domain to") @@ -145,13 +146,14 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := o.domainDelete(); err != nil { - printer.Error(fmt.Errorf("error delete dns domain : %v", err)) - os.Exit(1) + return fmt.Errorf("error delete dns domain : %v", err) } o.Base.Printer.Display(printer.Info("dns domain has been deleted"), nil) + + return nil }, } @@ -166,17 +168,15 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { enabled, errEn := cmd.Flags().GetBool("enabled") if errEn != nil { - printer.Error(fmt.Errorf("error parsing 'enabled' flag for dnssec : %v", errEn)) - os.Exit(1) + return fmt.Errorf("error parsing 'enabled' flag for dnssec : %v", errEn) } disabled, errDi := cmd.Flags().GetBool("disabled") if errEn != nil { - printer.Error(fmt.Errorf("error parsing 'disabled' flag for dnssec : %v", errDi)) - os.Exit(1) + return fmt.Errorf("error parsing 'disabled' flag for dnssec : %v", errDi) } if cmd.Flags().Changed("enabled") { @@ -196,11 +196,12 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } if err := o.domainUpdate(); err != nil { - printer.Error(fmt.Errorf("error toggling dnssec : %v", err)) - os.Exit(1) + return fmt.Errorf("error toggling dnssec : %v", err) } o.Base.Printer.Display(printer.Info("dns domain DNSSEC has been updated"), nil) + + return nil }, } @@ -220,15 +221,16 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { info, err := o.domainDNSSECGet() if err != nil { - printer.Error(fmt.Errorf("error getting domain dnssec info : %v", err)) - os.Exit(1) + return fmt.Errorf("error getting domain dnssec info : %v", err) } data := &DNSSECPrinter{SEC: info} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -243,15 +245,16 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { info, err := o.domainSOAGet() if err != nil { - printer.Error(fmt.Errorf("error getting domain soa info : %v", err)) - os.Exit(1) + return fmt.Errorf("error getting domain soa info : %v", err) } data := &DNSSOAPrinter{SOA: *info} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -266,17 +269,15 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { ns, errNs := cmd.Flags().GetString("ns-primary") if errNs != nil { - printer.Error(fmt.Errorf("error parsing 'ns-primary' flag for domain soa : %v", errNs)) - os.Exit(1) + return fmt.Errorf("error parsing 'ns-primary' flag for domain soa : %v", errNs) } email, errEm := cmd.Flags().GetString("email") if errEm != nil { - printer.Error(fmt.Errorf("error parsing 'email' flag for domain soa : %v", errEm)) - os.Exit(1) + return fmt.Errorf("error parsing 'email' flag for domain soa : %v", errEm) } o.SOAUpdateReq = &govultr.Soa{ @@ -285,11 +286,12 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } if err := o.domainSOAUpdate(); err != nil { - printer.Error(fmt.Errorf("error updating domain soa : %v", err)) - os.Exit(1) + return fmt.Errorf("error updating domain soa : %v", err) } o.Base.Printer.Display(printer.Info("domain soa has been updated"), nil) + + return nil }, } @@ -324,17 +326,18 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) recs, meta, err := o.recordList() if err != nil { - printer.Error(fmt.Errorf("error retrieiving domain records : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieiving domain records : %v", err) } data := &DNSRecordsPrinter{Records: recs, Meta: meta} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -357,15 +360,16 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { rec, err := o.recordGet() if err != nil { - printer.Error(fmt.Errorf("error while getting domain record : %v", err)) - os.Exit(1) + return fmt.Errorf("error while getting domain record : %v", err) } data := &DNSRecordPrinter{Record: *rec} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -379,35 +383,30 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { rType, errTy := cmd.Flags().GetString("type") if errTy != nil { - printer.Error(fmt.Errorf("error parsing 'type' flag for domain record create : %v", errTy)) - os.Exit(1) + return fmt.Errorf("error parsing 'type' flag for domain record create : %v", errTy) } name, errNa := cmd.Flags().GetString("name") if errNa != nil { - printer.Error(fmt.Errorf("error parsing 'name' flag for domain record create : %v", errNa)) - os.Exit(1) + return fmt.Errorf("error parsing 'name' flag for domain record create : %v", errNa) } dt, errDa := cmd.Flags().GetString("data") if errDa != nil { - printer.Error(fmt.Errorf("error parsing 'data' flag for domain record create : %v", errDa)) - os.Exit(1) + return fmt.Errorf("error parsing 'data' flag for domain record create : %v", errDa) } ttl, errTt := cmd.Flags().GetInt("ttl") if errTt != nil { - printer.Error(fmt.Errorf("error parsing 'ttl' flag for domain record create : %v", errTt)) - os.Exit(1) + return fmt.Errorf("error parsing 'ttl' flag for domain record create : %v", errTt) } priority, errPr := cmd.Flags().GetInt("priority") if errPr != nil { - printer.Error(fmt.Errorf("error parsing 'priority' flag for domain record create : %v", errPr)) - os.Exit(1) + return fmt.Errorf("error parsing 'priority' flag for domain record create : %v", errPr) } o.RecordReq = &govultr.DomainRecordReq{ @@ -420,42 +419,43 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo rec, err := o.recordCreate() if err != nil { - printer.Error(fmt.Errorf("error creating domain record : %v", err)) - os.Exit(1) + return fmt.Errorf("error creating domain record : %v", err) } data := &DNSRecordPrinter{Record: *rec} o.Base.Printer.Display(data, nil) + + return nil }, } recordCreate.Flags().StringP("type", "t", "", "type for the record") if err := recordCreate.MarkFlagRequired("type"); err != nil { - printer.Error(fmt.Errorf("error marking dns record create 'type' flag required: %v", err)) + fmt.Printf("error marking dns record create 'type' flag required: %v", err) os.Exit(1) } recordCreate.Flags().StringP("name", "n", "", "name of the record") if err := recordCreate.MarkFlagRequired("name"); err != nil { - printer.Error(fmt.Errorf("error marking dns record create 'name' flag required: %v", err)) + fmt.Printf("error marking dns record create 'name' flag required: %v", err) os.Exit(1) } recordCreate.Flags().StringP("data", "d", "", "data for the record") if err := recordCreate.MarkFlagRequired("data"); err != nil { - printer.Error(fmt.Errorf("error marking dns record create 'data' flag required: %v", err)) + fmt.Printf("error marking dns record create 'data' flag required: %v", err) os.Exit(1) } recordCreate.Flags().IntP("ttl", "l", 0, "ttl for the record") if err := recordCreate.MarkFlagRequired("ttl"); err != nil { - printer.Error(fmt.Errorf("error marking dns record create 'ttl' flag required: %v", err)) + fmt.Printf("error marking dns record create 'ttl' flag required: %v", err) os.Exit(1) } recordCreate.Flags().IntP("priority", "p", 0, "only required for MX and SRV") if err := recordCreate.MarkFlagRequired("priority"); err != nil { - printer.Error(fmt.Errorf("error marking dns record create 'priority' flag required: %v", err)) + fmt.Printf("error marking dns record create 'priority' flag required: %v", err) os.Exit(1) } @@ -470,13 +470,14 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := o.recordDelete(); err != nil { - printer.Error(fmt.Errorf("error deleting domain record : %v", err)) - os.Exit(1) + return fmt.Errorf("error deleting domain record : %v", err) } o.Base.Printer.Display(printer.Info("domain record has been deleted"), nil) + + return nil }, } @@ -490,29 +491,25 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { name, errNa := cmd.Flags().GetString("name") if errNa != nil { - printer.Error(fmt.Errorf("error parsing 'name' flag for domain record update : %v", errNa)) - os.Exit(1) + return fmt.Errorf("error parsing 'name' flag for domain record update : %v", errNa) } dt, errDa := cmd.Flags().GetString("data") if errDa != nil { - printer.Error(fmt.Errorf("error parsing 'data' flag for domain record update : %v", errDa)) - os.Exit(1) + return fmt.Errorf("error parsing 'data' flag for domain record update : %v", errDa) } ttl, errTt := cmd.Flags().GetInt("ttl") if errTt != nil { - printer.Error(fmt.Errorf("error parsing 'ttl' flag for domain record update : %v", errTt)) - os.Exit(1) + return fmt.Errorf("error parsing 'ttl' flag for domain record update : %v", errTt) } priority, errPr := cmd.Flags().GetInt("priority") if errPr != nil { - printer.Error(fmt.Errorf("error parsing 'priority' flag for domain record update : %v", errPr)) - os.Exit(1) + return fmt.Errorf("error parsing 'priority' flag for domain record update : %v", errPr) } o.RecordReq = &govultr.DomainRecordReq{} @@ -534,11 +531,12 @@ func NewCmdDNS(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo } if err := o.recordUpdate(); err != nil { - printer.Error(fmt.Errorf("error updating domain record : %v", errPr)) - os.Exit(1) + return fmt.Errorf("error updating domain record : %v", errPr) } o.Base.Printer.Display(printer.Info("domain record has been updated"), nil) + + return nil }, } diff --git a/cmd/firewall/firewall.go b/cmd/firewall/firewall.go index 2285d45..560b553 100644 --- a/cmd/firewall/firewall.go +++ b/cmd/firewall/firewall.go @@ -95,17 +95,18 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo Use: "list", Short: "List all firewall groups", Aliases: []string{"l"}, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) groups, meta, err := o.listGroups() if err != nil { - printer.Error(fmt.Errorf("error retrieving firewall group list : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving firewall group list : %v", err) } data := &FirewallGroupsPrinter{Groups: groups, Meta: meta} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -127,15 +128,16 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { group, err := o.getGroup() if err != nil { - printer.Error(fmt.Errorf("error getting firewall group : %v", err)) - os.Exit(1) + return fmt.Errorf("error getting firewall group : %v", err) } data := &FirewallGroupPrinter{Group: *group} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -144,11 +146,10 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo Use: "create", Short: "Create a firewall group", Aliases: []string{"c"}, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { description, errDe := cmd.Flags().GetString("description") if errDe != nil { - printer.Error(fmt.Errorf("error parsing 'description' flag for firewall group create: %v", errDe)) - os.Exit(1) + return fmt.Errorf("error parsing 'description' flag for firewall group create: %v", errDe) } o.GroupReq = &govultr.FirewallGroupReq{ @@ -157,12 +158,13 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo grp, err := o.createGroup() if err != nil { - printer.Error(fmt.Errorf("error creating firewall group : %v", err)) - os.Exit(1) + return fmt.Errorf("error creating firewall group : %v", err) } data := &FirewallGroupPrinter{Group: *grp} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -179,11 +181,10 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { description, errDe := cmd.Flags().GetString("description") if errDe != nil { - printer.Error(fmt.Errorf("error parsing 'description' flag for firewall group update : %v", errDe)) - os.Exit(1) + return fmt.Errorf("error parsing 'description' flag for firewall group update : %v", errDe) } o.GroupReq = &govultr.FirewallGroupReq{ @@ -191,17 +192,18 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo } if err := o.updateGroup(); err != nil { - printer.Error(fmt.Errorf("error updating firewall group : %v", err)) - os.Exit(1) + return fmt.Errorf("error updating firewall group : %v", err) } o.Base.Printer.Display(printer.Info("firewall group has been updated"), nil) + + return nil }, } groupUpdate.Flags().StringP("description", "d", "", "Description of firewall group.") if err := groupUpdate.MarkFlagRequired("description"); err != nil { - printer.Error(fmt.Errorf("error marking firewall group 'description' flag required: %v", err)) + fmt.Printf("error marking firewall group 'description' flag required: %v", err) os.Exit(1) } @@ -216,13 +218,14 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := o.deleteGroup(); err != nil { - printer.Error(fmt.Errorf("error deleting firewall group : %v", err)) - os.Exit(1) + return fmt.Errorf("error deleting firewall group : %v", err) } o.Base.Printer.Display(printer.Info("firewall group has been deleted"), nil) + + return nil }, } @@ -256,17 +259,18 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { o.Base.Options = utils.GetPaging(cmd) rules, meta, err := o.listRules() if err != nil { - printer.Error(fmt.Errorf("error retrieving firewall rule list : %v", err)) - os.Exit(1) + return fmt.Errorf("error retrieving firewall rule list : %v", err) } data := &FirewallRulesPrinter{Rules: rules, Meta: meta} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -290,15 +294,16 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { rule, err := o.getRule() if err != nil { - printer.Error(fmt.Errorf("error getting firewall rule : %v", err)) - os.Exit(1) + return fmt.Errorf("error getting firewall rule : %v", err) } data := &FirewallRulePrinter{Rule: *rule} o.Base.Printer.Display(data, nil) + + return nil }, } @@ -315,47 +320,40 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { protocol, errPr := cmd.Flags().GetString("protocol") if errPr != nil { - printer.Error(fmt.Errorf("error parsing 'protocol' flag for firewall group create : %v", errPr)) - os.Exit(1) + return fmt.Errorf("error parsing 'protocol' flag for firewall group create : %v", errPr) } subnet, errSu := cmd.Flags().GetString("subnet") if errSu != nil { - printer.Error(fmt.Errorf("error parsing 'subnet' flag for firewall group create : %v", errSu)) - os.Exit(1) + return fmt.Errorf("error parsing 'subnet' flag for firewall group create : %v", errSu) } ipType, errIP := cmd.Flags().GetString("ip-type") if errIP != nil { - printer.Error(fmt.Errorf("error parsing 'ip-type' flag for firewall group create : %v", errIP)) - os.Exit(1) + return fmt.Errorf("error parsing 'ip-type' flag for firewall group create : %v", errIP) } size, errSi := cmd.Flags().GetInt("size") if errSi != nil { - printer.Error(fmt.Errorf("error parsing 'size' flag for firewall group create : %v", errSi)) - os.Exit(1) + return fmt.Errorf("error parsing 'size' flag for firewall group create : %v", errSi) } source, errSo := cmd.Flags().GetString("source") if errSo != nil { - printer.Error(fmt.Errorf("error parsing 'source' flag for firewall group create : %v", errSo)) - os.Exit(1) + return fmt.Errorf("error parsing 'source' flag for firewall group create : %v", errSo) } port, errPo := cmd.Flags().GetString("port") if errPo != nil { - printer.Error(fmt.Errorf("error parsing 'port' flag for firewall group create : %v", errPo)) - os.Exit(1) + return fmt.Errorf("error parsing 'port' flag for firewall group create : %v", errPo) } notes, errNo := cmd.Flags().GetString("notes") if errNo != nil { - printer.Error(fmt.Errorf("error parsing 'notes' flag for firewall group create : %v", errNo)) - os.Exit(1) + return fmt.Errorf("error parsing 'notes' flag for firewall group create : %v", errNo) } o.RuleReq = &govultr.FirewallRuleReq{ @@ -374,8 +372,7 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo } if ipType == "" { - printer.Error(fmt.Errorf("a firewall rule requires an IP type. Pass an --ip-type value of v4 or v6")) - os.Exit(1) + return fmt.Errorf("a firewall rule requires an IP type. Pass an --ip-type value of v4 or v6") } if ipType != "" { @@ -384,30 +381,31 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo rule, err := o.createRule() if err != nil { - printer.Error(fmt.Errorf("error creating firewall rule : %v", err)) - os.Exit(1) + return fmt.Errorf("error creating firewall rule : %v", err) } data := &FirewallRulePrinter{Rule: *rule} o.Base.Printer.Display(data, nil) + + return nil }, } ruleCreate.Flags().StringP("protocol", "p", "", "Protocol type. Possible values: 'icmp', 'tcp', 'udp', 'gre'.") if err := ruleCreate.MarkFlagRequired("protocol"); err != nil { - printer.Error(fmt.Errorf("error marking firewall rule create 'protocol' flag required : %v", err)) + fmt.Printf("error marking firewall rule create 'protocol' flag required : %v", err) os.Exit(1) } ruleCreate.Flags().StringP("subnet", "s", "", "The IPv4 network in CIDR notation.") if err := ruleCreate.MarkFlagRequired("subnet"); err != nil { - printer.Error(fmt.Errorf("error marking firewall rule create 'subnet' flag required : %v", err)) + fmt.Printf("error marking firewall rule create 'subnet' flag required : %v", err) os.Exit(1) } ruleCreate.Flags().IntP("size", "z", 0, "The number of bits for the netmask in CIDR notation.") if err := ruleCreate.MarkFlagRequired("size"); err != nil { - printer.Error(fmt.Errorf("error marking firewall rule create 'size' flag required : %v", err)) + fmt.Printf("error marking firewall rule create 'size' flag required : %v", err) os.Exit(1) } @@ -420,7 +418,7 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo ruleCreate.Flags().StringP("ip-type", "t", "", "The type of IP rule - v4 or v6.") if err := ruleCreate.MarkFlagRequired("ip-type"); err != nil { - printer.Error(fmt.Errorf("error marking firewall rule create 'ip-type' flag required : %v", err)) + fmt.Printf("error marking firewall rule create 'ip-type' flag required : %v", err) os.Exit(1) } @@ -446,13 +444,14 @@ func NewCmdFirewall(base *cli.Base) *cobra.Command { //nolint:gocyclo } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := o.deleteRule(); err != nil { - printer.Error(fmt.Errorf("error deleting firewall rule : %v", err)) - os.Exit(1) + return fmt.Errorf("error deleting firewall rule : %v", err) } o.Base.Printer.Display(printer.Info("firewall rule deleted"), nil) + + return nil }, }