Skip to content

Commit

Permalink
Merge pull request #162 from equinix/silent-usage
Browse files Browse the repository at this point in the history
silence usage messages when usage was valid and API errors are returned
  • Loading branch information
displague authored Dec 13, 2021
2 parents e66e434 + 1165570 commit a0698e1
Show file tree
Hide file tree
Showing 45 changed files with 106 additions and 46 deletions.
8 changes: 6 additions & 2 deletions internal/capacity/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func (c *Client) Check() *cobra.Command {
if facility != "" {
facilities = append(facilities, facility)
}

if (len(facilities) > 0) == (len(metros) > 0) {
return errors.New("Either facilities or metros should be set")
}
cmd.SilenceUsage = true

if plan != "" {
plans = append(plans, plan)
}
Expand Down Expand Up @@ -87,8 +93,6 @@ func (c *Client) Check() *cobra.Command {
)
}
}
} else {
return errors.New("Either facility or metro should be set")
}

availability, _, err := checker(req)
Expand Down
1 change: 1 addition & 0 deletions internal/capacity/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Retrieve capacities:
metal capacity get { --metro | --facility }
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
var err error
lister := c.Service.List
fieldName := "Facility"
Expand Down
2 changes: 2 additions & 0 deletions internal/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func NewCommand() *cobra.Command {
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
switch args[0] {
case "bash":
return cmd.Root().GenBashCompletion(os.Stdout)
Expand All @@ -82,6 +83,7 @@ func NewCommand() *cobra.Command {
return fmt.Errorf("unknown shell: %q", args[0])
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
return nil
},
}
Expand Down
1 change: 1 addition & 0 deletions internal/devices/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ metal device create --hostname [hostname] --plan [plan] --metro [metro_code] --f
if userdata != "" && userdataFile != "" {
return fmt.Errorf("Either userdata or userdata-file should be set")
}
cmd.SilenceUsage = true

if userdataFile != "" {
userdataRaw, readErr := ioutil.ReadFile(userdataFile)
Expand Down
1 change: 1 addition & 0 deletions internal/devices/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (c *Client) Delete() *cobra.Command {
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if !force {
prompt := promptui.Prompt{
Label: fmt.Sprintf("Are you sure you want to delete device %s: ", deviceID),
Expand Down
1 change: 1 addition & 0 deletions internal/devices/reboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ metal device reboot --id [device_UUID]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_, err := c.Service.Reboot(deviceID)
if err != nil {
return errors.Wrap(err, "Could not reboot Device")
Expand Down
28 changes: 15 additions & 13 deletions internal/devices/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ metal device get --id [device_UUID]

if deviceID == "" && projectID == "" {
return fmt.Errorf("Either id or project-id should be set.")
} else if deviceID != "" {
}
cmd.SilenceUsage = true

if deviceID != "" {
device, _, err := c.Service.Get(deviceID, nil)
if err != nil {
return errors.Wrap(err, "Could not get Devices")
Expand All @@ -55,21 +58,20 @@ metal device get --id [device_UUID]
data[0] = []string{device.ID, device.Hostname, device.OS.Name, device.State, device.Created}

return c.Out.Output(device, header, &data)
} else if projectID != "" {
devices, _, err := c.Service.List(projectID, c.Servicer.ListOptions(nil, nil))
if err != nil {
return errors.Wrap(err, "Could not list Devices")
}
data := make([][]string, len(devices))
}

for i, dc := range devices {
data[i] = []string{dc.ID, dc.Hostname, dc.OS.Name, dc.State, dc.Created}
}
header := []string{"ID", "Hostname", "OS", "State", "Created"}
devices, _, err := c.Service.List(projectID, c.Servicer.ListOptions(nil, nil))
if err != nil {
return errors.Wrap(err, "Could not list Devices")
}
data := make([][]string, len(devices))

return c.Out.Output(devices, header, &data)
for i, dc := range devices {
data[i] = []string{dc.ID, dc.Hostname, dc.OS.Name, dc.State, dc.Created}
}
return nil
header := []string{"ID", "Hostname", "OS", "State", "Created"}

return c.Out.Output(devices, header, &data)
},
}

Expand Down
1 change: 1 addition & 0 deletions internal/devices/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ metal device start --id [device_UUID]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_, err := c.Service.PowerOn(deviceID)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/devices/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (c *Client) Stop() *cobra.Command {
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_, err := c.Service.PowerOff(deviceID)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/devices/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ metal device update --id [device_UUID] --hostname [new_hostname]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := &packngo.DeviceUpdateRequest{}

if hostname != "" {
Expand Down
2 changes: 2 additions & 0 deletions internal/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ func NewCommand() *cobra.Command {
DisableFlagsInUseLine: true,
Args: cobra.ExactValidArgs(1),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
dest := args[0]
return doc.GenMarkdownTree(cmd.Parent(), dest)
},
Expand Down
1 change: 1 addition & 0 deletions internal/events/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ metal event get
When using "--json" or "--yaml", "--include=relationships" is implied.
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
var events []packngo.Event
var err error
header := []string{"ID", "Body", "Type", "Created"}
Expand Down
1 change: 1 addition & 0 deletions internal/facilities/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ metal facilities get
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
facilities, _, err := c.Service.List(c.Servicer.ListOptions(nil, nil))
if err != nil {
return errors.Wrap(err, "Could not list Facilities")
Expand Down
1 change: 1 addition & 0 deletions internal/hardware/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (c *Client) Move() *cobra.Command {
metal hardware_reservation move -i [hardware_reservation_UUID] -p [project_UUID]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
header := []string{"ID", "Facility", "Plan", "Created"}
r, _, err := c.Service.Move(hardwareReservationID, projectID)
if err != nil {
Expand Down
26 changes: 14 additions & 12 deletions internal/hardware/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ When using "--json" or "--yaml", "--include=project,facility,device" is implied.

if hardwareReservationID == "" && projectID == "" {
return fmt.Errorf("Either id or project-id should be set.")
} else if hardwareReservationID != "" {
}

cmd.SilenceUsage = true
if hardwareReservationID != "" {
getOpts := &packngo.GetOptions{Includes: listOpt.Includes, Excludes: listOpt.Excludes}
r, _, err := c.Service.Get(hardwareReservationID, getOpts)
if err != nil {
Expand All @@ -72,21 +75,20 @@ When using "--json" or "--yaml", "--include=project,facility,device" is implied.
data[0] = []string{r.ID, r.Facility.Code, r.Plan.Name, r.CreatedAt.String()}

return c.Out.Output(r, header, &data)
} else if projectID != "" {
reservations, _, err := c.Service.List(projectID, listOpt)
if err != nil {
return errors.Wrap(err, "Could not list Hardware Reservations")
}
}

data := make([][]string, len(reservations))
reservations, _, err := c.Service.List(projectID, listOpt)
if err != nil {
return errors.Wrap(err, "Could not list Hardware Reservations")
}

for i, r := range reservations {
data[i] = []string{r.ID, r.Facility.Code, r.Plan.Name, r.CreatedAt.String()}
}
data := make([][]string, len(reservations))

return c.Out.Output(reservations, header, &data)
for i, r := range reservations {
data[i] = []string{r.ID, r.Facility.Code, r.Plan.Name, r.CreatedAt.String()}
}
return nil

return c.Out.Output(reservations, header, &data)
},
}

Expand Down
1 change: 1 addition & 0 deletions internal/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (c *Client) NewCommand() *cobra.Command {
`,
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
cmd.SilenceUsage = true
config, _ := cmd.Flags().GetString("config")
if config == "" {
Expand Down
1 change: 1 addition & 0 deletions internal/ips/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ metal ip assign -d [device-id] -a [ip-address]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
assignment, _, err := c.DeviceService.Assign(deviceID, &packngo.AddressStruct{Address: address})
if err != nil {
return errors.Wrap(err, "Could not assign Device IP address")
Expand Down
1 change: 1 addition & 0 deletions internal/ips/available.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ metal ip available --reservation-id [reservation_id] --cidr [size_of_subnet]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
result, _, err := c.ProjectService.AvailableAddresses(reservationID, &packngo.AvailableRequest{CIDR: cidr})

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/ips/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ metal ip remove --id [reservation-UUID]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_, err := c.ProjectService.Remove(reservationID)
if err != nil {
return errors.Wrap(err, "Could not remove IP address Reservation")
Expand Down
1 change: 1 addition & 0 deletions internal/ips/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ metal ip request --quantity [quantity] --facility [facility_code] --type [addres
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := &packngo.IPReservationRequest{
Type: ttype,
Quantity: quantity,
Expand Down
39 changes: 22 additions & 17 deletions internal/ips/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ metal ip get --reservation-id [reservation_UUID]
RunE: func(cmd *cobra.Command, args []string) error {
if assignmentID != "" && reservationID != "" {
return fmt.Errorf("Either assignment-id or reservation-id can be set.")
} else if assignmentID != "" {
}

if assignmentID == "" && reservationID == "" && projectID == "" {
return fmt.Errorf("Either project-id or assignment-id or reservation-id must be set.")
}

cmd.SilenceUsage = true
if assignmentID != "" {
ip, _, err := c.ProjectService.Get(assignmentID, nil)
if err != nil {
return errors.Wrap(err, "Could not get Device IP address")
Expand All @@ -82,27 +89,25 @@ metal ip get --reservation-id [reservation_UUID]
header := []string{"ID", "Address", "Facility", "Public", "Created"}

return c.Out.Output(ip, header, &data)
} else if projectID != "" {
ips, _, err := c.ProjectService.List(projectID, nil)
if err != nil {
return errors.Wrap(err, "Could not list Project IP addresses")
}
}

data := make([][]string, len(ips))
ips, _, err := c.ProjectService.List(projectID, nil)
if err != nil {
return errors.Wrap(err, "Could not list Project IP addresses")
}

for i, ip := range ips {
code := ""
if ip.Facility != nil {
code = ip.Facility.Code
}
data[i] = []string{ip.ID, ip.Address, code, strconv.FormatBool(ip.Public), ip.Created}
}
header := []string{"ID", "Address", "Facility", "Public", "Created"}
data := make([][]string, len(ips))

return c.Out.Output(ips, header, &data)
for i, ip := range ips {
code := ""
if ip.Facility != nil {
code = ip.Facility.Code
}
data[i] = []string{ip.ID, ip.Address, code, strconv.FormatBool(ip.Public), ip.Created}
}
header := []string{"ID", "Address", "Facility", "Public", "Created"}

return fmt.Errorf("Either project-id or assignment-id or reservation-id must be set.")
return c.Out.Output(ips, header, &data)
},
}

Expand Down
1 change: 1 addition & 0 deletions internal/ips/unassign.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ metal ip unassign --id [assignment-UUID]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_, err := c.DeviceService.Unassign(assignmentID)
if err != nil {
return errors.Wrap(err, "Could not unassign IP address")
Expand Down
1 change: 1 addition & 0 deletions internal/metros/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ metal metros get
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
metros, _, err := c.Service.List(c.Servicer.ListOptions(nil, nil))
if err != nil {
return errors.Wrap(err, "Could not list Metros")
Expand Down
1 change: 1 addition & 0 deletions internal/organizations/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ metal organization create -n [name]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := &packngo.OrganizationCreateRequest{
Name: name,
}
Expand Down
1 change: 1 addition & 0 deletions internal/organizations/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ metal organization delete -i [organization_UUID]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if !force {
prompt := promptui.Prompt{
Label: fmt.Sprintf("Are you sure you want to delete organization %s: ", organizationID),
Expand Down
1 change: 1 addition & 0 deletions internal/organizations/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ metal organization payment-methods --id [organization_UUID]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
paymentMethods, _, err := c.Service.ListPaymentMethods(organizationID)
if err != nil {
return errors.Wrap(err, "Could not list Payment Methods")
Expand Down
1 change: 1 addition & 0 deletions internal/organizations/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ metal organization get -i [organization-id]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
listOpts := c.Servicer.ListOptions(nil, nil)
if organizationID == "" {
orgs, _, err := c.Service.List(listOpts)
Expand Down
1 change: 1 addition & 0 deletions internal/organizations/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ metal organization update --id [organization_UUID] --name [new_name]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := &packngo.OrganizationUpdateRequest{}

if name != "" {
Expand Down
1 change: 1 addition & 0 deletions internal/os/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (c *Client) Retrieve() *cobra.Command {
Long: `Example:
metal operating-systems get`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
oss, _, err := c.Service.List()
if err != nil {
return errors.Wrap(err, "Could not list OperatingSystems")
Expand Down
1 change: 1 addition & 0 deletions internal/plans/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (c *Client) Retrieve() *cobra.Command {
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
plans, _, err := c.Service.List(c.Servicer.ListOptions(nil, nil))
if err != nil {
return errors.Wrap(err, "Could not list Plans")
Expand Down
1 change: 1 addition & 0 deletions internal/projects/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ metal project create --name [project_name]
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := packngo.ProjectCreateRequest{
Name: name,
}
Expand Down
Loading

0 comments on commit a0698e1

Please sign in to comment.