Skip to content

Commit

Permalink
Use errors.New() where appropriate (#14024)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomponline authored Sep 4, 2024
2 parents 5d0135e + e1d8657 commit da184d8
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 123 deletions.
21 changes: 11 additions & 10 deletions lxc/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -155,7 +156,7 @@ func (c *cmdClusterList) run(cmd *cobra.Command, args []string) error {
}

if !cluster.Enabled {
return fmt.Errorf(i18n.G("LXD server isn't part of a cluster"))
return errors.New(i18n.G("LXD server isn't part of a cluster"))
}

// Get the cluster members
Expand Down Expand Up @@ -549,7 +550,7 @@ Are you really sure you want to force removing %s? (yes/no): `), name)
input = strings.TrimSuffix(input, "\n")

if !shared.ValueInSlice(strings.ToLower(input), []string{i18n.G("yes")}) {
return fmt.Errorf(i18n.G("User aborted delete operation"))
return errors.New(i18n.G("User aborted delete operation"))
}

return nil
Expand Down Expand Up @@ -645,7 +646,7 @@ func (c *cmdClusterEnable) run(cmd *cobra.Command, args []string) error {
}

if server.Config["core.https_address"] == "" && server.Config["cluster.https_address"] == "" {
return fmt.Errorf(i18n.G("This LXD server is not available on the network"))
return errors.New(i18n.G("This LXD server is not available on the network"))
}

// Check if already enabled
Expand All @@ -655,7 +656,7 @@ func (c *cmdClusterEnable) run(cmd *cobra.Command, args []string) error {
}

if currentCluster.Enabled {
return fmt.Errorf(i18n.G("This LXD server is already clustered"))
return errors.New(i18n.G("This LXD server is already clustered"))
}

// Enable clustering.
Expand Down Expand Up @@ -719,7 +720,7 @@ func (c *cmdClusterEdit) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing cluster member name"))
return errors.New(i18n.G("Missing cluster member name"))
}

// If stdin isn't a terminal, read text from it
Expand Down Expand Up @@ -826,7 +827,7 @@ func (c *cmdClusterAdd) run(cmd *cobra.Command, args []string) error {

// Determine the machine name.
if resource.name != "" && c.flagName != "" && resource.name != c.flagName {
return fmt.Errorf(i18n.G("Cluster member name was provided as both a flag and as an argument"))
return errors.New(i18n.G("Cluster member name was provided as both a flag and as an argument"))
}

if resource.name == "" {
Expand Down Expand Up @@ -912,7 +913,7 @@ func (c *cmdClusterListTokens) run(cmd *cobra.Command, args []string) error {
}

if !cluster.Enabled {
return fmt.Errorf(i18n.G("LXD server isn't part of a cluster"))
return errors.New(i18n.G("LXD server isn't part of a cluster"))
}

// Get the cluster member join tokens. Use default project as join tokens are created in default project.
Expand Down Expand Up @@ -1006,7 +1007,7 @@ func (c *cmdClusterRevokeToken) run(cmd *cobra.Command, args []string) error {
}

if !cluster.Enabled {
return fmt.Errorf(i18n.G("LXD server isn't part of a cluster"))
return errors.New(i18n.G("LXD server isn't part of a cluster"))
}

// Get the cluster member join tokens. Use default project as join tokens are created in default project.
Expand Down Expand Up @@ -1097,7 +1098,7 @@ func (c *cmdClusterUpdateCertificate) run(cmd *cobra.Command, args []string) err
}

if !cluster.Enabled {
return fmt.Errorf(i18n.G("LXD server isn't part of a cluster"))
return errors.New(i18n.G("LXD server isn't part of a cluster"))
}

if !shared.PathExists(certFile) {
Expand Down Expand Up @@ -1217,7 +1218,7 @@ func (c *cmdClusterEvacuateAction) run(cmd *cobra.Command, args []string) error
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing cluster member name"))
return errors.New(i18n.G("Missing cluster member name"))
}

if !c.flagForce {
Expand Down
41 changes: 21 additions & 20 deletions lxc/config_device.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -116,7 +117,7 @@ func (c *cmdConfigDeviceAdd) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing name"))
return errors.New(i18n.G("Missing name"))
}

// Add the device
Expand Down Expand Up @@ -148,7 +149,7 @@ func (c *cmdConfigDeviceAdd) run(cmd *cobra.Command, args []string) error {

_, ok := profile.Devices[devname]
if ok {
return fmt.Errorf(i18n.G("The device already exists"))
return errors.New(i18n.G("The device already exists"))
}

profile.Devices[devname] = device
Expand All @@ -165,7 +166,7 @@ func (c *cmdConfigDeviceAdd) run(cmd *cobra.Command, args []string) error {

_, ok := inst.Devices[devname]
if ok {
return fmt.Errorf(i18n.G("The device already exists"))
return errors.New(i18n.G("The device already exists"))
}

inst.Devices[devname] = device
Expand Down Expand Up @@ -229,7 +230,7 @@ func (c *cmdConfigDeviceGet) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing name"))
return errors.New(i18n.G("Missing name"))
}

// Get the config key
Expand All @@ -244,7 +245,7 @@ func (c *cmdConfigDeviceGet) run(cmd *cobra.Command, args []string) error {

dev, ok := profile.Devices[devname]
if !ok {
return fmt.Errorf(i18n.G("Device doesn't exist"))
return errors.New(i18n.G("Device doesn't exist"))
}

fmt.Println(dev[key])
Expand All @@ -258,10 +259,10 @@ func (c *cmdConfigDeviceGet) run(cmd *cobra.Command, args []string) error {
if !ok {
_, ok = inst.ExpandedDevices[devname]
if !ok {
return fmt.Errorf(i18n.G("Device doesn't exist"))
return errors.New(i18n.G("Device doesn't exist"))
}

return fmt.Errorf(i18n.G("Device from profile(s) cannot be retrieved for individual instance"))
return errors.New(i18n.G("Device from profile(s) cannot be retrieved for individual instance"))
}

fmt.Println(dev[key])
Expand Down Expand Up @@ -311,7 +312,7 @@ func (c *cmdConfigDeviceList) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing name"))
return errors.New(i18n.G("Missing name"))
}

// List the devices
Expand Down Expand Up @@ -377,7 +378,7 @@ func (c *cmdConfigDeviceOverride) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing name"))
return errors.New(i18n.G("Missing name"))
}

// Override the device
Expand All @@ -389,12 +390,12 @@ func (c *cmdConfigDeviceOverride) run(cmd *cobra.Command, args []string) error {
devname := args[1]
_, ok := inst.Devices[devname]
if ok {
return fmt.Errorf(i18n.G("The device already exists"))
return errors.New(i18n.G("The device already exists"))
}

device, ok := inst.ExpandedDevices[devname]
if !ok {
return fmt.Errorf(i18n.G("The profile device doesn't exist"))
return errors.New(i18n.G("The profile device doesn't exist"))
}

if len(args) > 2 {
Expand Down Expand Up @@ -471,7 +472,7 @@ func (c *cmdConfigDeviceRemove) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing name"))
return errors.New(i18n.G("Missing name"))
}

// Remove the device
Expand All @@ -484,7 +485,7 @@ func (c *cmdConfigDeviceRemove) run(cmd *cobra.Command, args []string) error {
for _, devname := range args[1:] {
_, ok := profile.Devices[devname]
if !ok {
return fmt.Errorf(i18n.G("Device doesn't exist"))
return errors.New(i18n.G("Device doesn't exist"))
}

delete(profile.Devices, devname)
Expand All @@ -505,10 +506,10 @@ func (c *cmdConfigDeviceRemove) run(cmd *cobra.Command, args []string) error {
if !ok {
_, ok := inst.ExpandedDevices[devname]
if !ok {
return fmt.Errorf(i18n.G("Device doesn't exist"))
return errors.New(i18n.G("Device doesn't exist"))
}

return fmt.Errorf(i18n.G("Device from profile(s) cannot be removed from individual instance. Override device or modify profile instead"))
return errors.New(i18n.G("Device from profile(s) cannot be removed from individual instance. Override device or modify profile instead"))
}

delete(inst.Devices, devname)
Expand Down Expand Up @@ -580,7 +581,7 @@ func (c *cmdConfigDeviceSet) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing name"))
return errors.New(i18n.G("Missing name"))
}

// Set the device config key
Expand All @@ -599,7 +600,7 @@ func (c *cmdConfigDeviceSet) run(cmd *cobra.Command, args []string) error {

dev, ok := profile.Devices[devname]
if !ok {
return fmt.Errorf(i18n.G("Device doesn't exist"))
return errors.New(i18n.G("Device doesn't exist"))
}

for k, v := range keys {
Expand All @@ -622,10 +623,10 @@ func (c *cmdConfigDeviceSet) run(cmd *cobra.Command, args []string) error {
if !ok {
_, ok = inst.ExpandedDevices[devname]
if !ok {
return fmt.Errorf(i18n.G("Device doesn't exist"))
return errors.New(i18n.G("Device doesn't exist"))
}

return fmt.Errorf(i18n.G("Device from profile(s) cannot be modified for individual instance. Override device or modify profile instead"))
return errors.New(i18n.G("Device from profile(s) cannot be modified for individual instance. Override device or modify profile instead"))
}

for k, v := range keys {
Expand Down Expand Up @@ -689,7 +690,7 @@ func (c *cmdConfigDeviceShow) run(cmd *cobra.Command, args []string) error {
resource := resources[0]

if resource.name == "" {
return fmt.Errorf(i18n.G("Missing name"))
return errors.New(i18n.G("Missing name"))
}

// Show the devices
Expand Down
2 changes: 1 addition & 1 deletion lxc/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (c *cmdConsole) run(cmd *cobra.Command, args []string) error {
// Show the current log if requested
if c.flagShowLog {
if c.flagType != "console" {
return fmt.Errorf("%s", i18n.G("The --show-log flag is only supported for by 'console' output type"))
return errors.New(i18n.G("The --show-log flag is only supported for by 'console' output type"))
}

console := &lxd.InstanceConsoleLogArgs{}
Expand Down
Loading

0 comments on commit da184d8

Please sign in to comment.