From 144a6494e572d53e84fb82d5da7d8026f72ca0fb Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 30 Aug 2024 17:44:50 -0400 Subject: [PATCH 01/14] lxc/console: use errors.New() where appropriate Signed-off-by: Simon Deziel --- lxc/console.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lxc/console.go b/lxc/console.go index 263fa1cca71e..57253b50116c 100644 --- a/lxc/console.go +++ b/lxc/console.go @@ -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{} From 820a3abab652a5dc5718b8632459aa83c4551d85 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 30 Aug 2024 17:44:57 -0400 Subject: [PATCH 02/14] lxc/project: use errors.New() where appropriate Signed-off-by: Simon Deziel --- lxc/project.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lxc/project.go b/lxc/project.go index 1c3bd1754d4b..3fad70eef138 100644 --- a/lxc/project.go +++ b/lxc/project.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "io" "os" @@ -135,7 +136,7 @@ func (c *cmdProjectCreate) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing project name")) + return errors.New(i18n.G("Missing project name")) } // Create the project @@ -207,7 +208,7 @@ func (c *cmdProjectDelete) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing project name")) + return errors.New(i18n.G("Missing project name")) } // Delete the project @@ -289,7 +290,7 @@ func (c *cmdProjectEdit) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing project name")) + return errors.New(i18n.G("Missing project name")) } // If stdin isn't a terminal, read text from it @@ -393,7 +394,7 @@ func (c *cmdProjectGet) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing project name")) + return errors.New(i18n.G("Missing project name")) } // Get the configuration key @@ -567,7 +568,7 @@ func (c *cmdProjectRename) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing project name")) + return errors.New(i18n.G("Missing project name")) } // Rename the project @@ -627,7 +628,7 @@ func (c *cmdProjectSet) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing project name")) + return errors.New(i18n.G("Missing project name")) } // Get the project @@ -734,7 +735,7 @@ func (c *cmdProjectShow) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing project name")) + return errors.New(i18n.G("Missing project name")) } // Show the project @@ -847,7 +848,7 @@ func (c *cmdProjectInfo) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing project name")) + return errors.New(i18n.G("Missing project name")) } // Get the current allocations From b3916dea82c13e15fb163c767d84ec7cab0809d8 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 30 Aug 2024 17:45:04 -0400 Subject: [PATCH 03/14] lxc/storage_bucket: use errors.New() where appropriate Signed-off-by: Simon Deziel --- lxc/storage_bucket.go | 55 ++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/lxc/storage_bucket.go b/lxc/storage_bucket.go index 3a8582877831..8117ebf42fbb 100644 --- a/lxc/storage_bucket.go +++ b/lxc/storage_bucket.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "io" "os" @@ -109,11 +110,11 @@ func (c *cmdStorageBucketCreate) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } if args[1] == "" { - return fmt.Errorf("%s", i18n.G("Missing bucket name")) + return errors.New(i18n.G("Missing bucket name")) } // If stdin isn't a terminal, read yaml from it. @@ -209,11 +210,11 @@ func (c *cmdStorageBucketDelete) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } if args[1] == "" { - return fmt.Errorf("%s", i18n.G("Missing bucket name")) + return errors.New(i18n.G("Missing bucket name")) } client := resource.server @@ -285,11 +286,11 @@ func (c *cmdStorageBucketEdit) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } if args[1] == "" { - return fmt.Errorf("%s", i18n.G("Missing bucket name")) + return errors.New(i18n.G("Missing bucket name")) } client := resource.server @@ -404,11 +405,11 @@ func (c *cmdStorageBucketGet) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } if args[1] == "" { - return fmt.Errorf("%s", i18n.G("Missing bucket name")) + return errors.New(i18n.G("Missing bucket name")) } client := resource.server @@ -479,7 +480,7 @@ func (c *cmdStorageBucketList) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } client := resource.server @@ -561,11 +562,11 @@ func (c *cmdStorageBucketSet) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } if args[1] == "" { - return fmt.Errorf("%s", i18n.G("Missing bucket name")) + return errors.New(i18n.G("Missing bucket name")) } client := resource.server @@ -653,11 +654,11 @@ func (c *cmdStorageBucketShow) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } if args[1] == "" { - return fmt.Errorf("%s", i18n.G("Missing bucket name")) + return errors.New(i18n.G("Missing bucket name")) } client := resource.server @@ -795,11 +796,11 @@ func (c *cmdStorageBucketKeyList) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } if args[1] == "" { - return fmt.Errorf("%s", i18n.G("Missing bucket name")) + return errors.New(i18n.G("Missing bucket name")) } client := resource.server @@ -882,15 +883,15 @@ func (c *cmdStorageBucketKeyCreate) runAdd(cmd *cobra.Command, args []string) er resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } if args[1] == "" { - return fmt.Errorf("%s", i18n.G("Missing bucket name")) + return errors.New(i18n.G("Missing bucket name")) } if args[2] == "" { - return fmt.Errorf("%s", i18n.G("Missing key name")) + return errors.New(i18n.G("Missing key name")) } client := resource.server @@ -979,15 +980,15 @@ func (c *cmdStorageBucketKeyDelete) runRemove(cmd *cobra.Command, args []string) resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } if args[1] == "" { - return fmt.Errorf("%s", i18n.G("Missing bucket name")) + return errors.New(i18n.G("Missing bucket name")) } if args[2] == "" { - return fmt.Errorf("%s", i18n.G("Missing key name")) + return errors.New(i18n.G("Missing key name")) } client := resource.server @@ -1058,15 +1059,15 @@ func (c *cmdStorageBucketKeyEdit) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } if args[1] == "" { - return fmt.Errorf("%s", i18n.G("Missing bucket name")) + return errors.New(i18n.G("Missing bucket name")) } if args[2] == "" { - return fmt.Errorf("%s", i18n.G("Missing key name")) + return errors.New(i18n.G("Missing key name")) } client := resource.server @@ -1181,15 +1182,15 @@ func (c *cmdStorageBucketKeyShow) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf("%s", i18n.G("Missing pool name")) + return errors.New(i18n.G("Missing pool name")) } if args[1] == "" { - return fmt.Errorf("%s", i18n.G("Missing bucket name")) + return errors.New(i18n.G("Missing bucket name")) } if args[2] == "" { - return fmt.Errorf("%s", i18n.G("Missing key name")) + return errors.New(i18n.G("Missing key name")) } client := resource.server From cda4473b291e04b7dee601eecacce2e39b34f467 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 30 Aug 2024 17:45:14 -0400 Subject: [PATCH 04/14] lxd/device/proxy: use errors.New() where appropriate Signed-off-by: Simon Deziel --- lxd/device/proxy.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lxd/device/proxy.go b/lxd/device/proxy.go index 5b7b40e3e73c..00d21ed8adc1 100644 --- a/lxd/device/proxy.go +++ b/lxd/device/proxy.go @@ -3,6 +3,7 @@ package device import ( "bufio" "context" + "errors" "fmt" "net" "os" @@ -404,7 +405,7 @@ func (d *proxy) checkProcStarted(logPath string) (bool, error) { } if strings.HasPrefix(line, "Error:") { - return false, fmt.Errorf("%s", line) + return false, errors.New(line) } } From 5459b2e91585fcad55c67f337085c4eb49b5222c Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 30 Aug 2024 17:45:21 -0400 Subject: [PATCH 05/14] lxd/instances_put: use errors.New() where appropriate Signed-off-by: Simon Deziel --- lxd/instances_put.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lxd/instances_put.go b/lxd/instances_put.go index fa8dec315ef8..92784e40b3cd 100644 --- a/lxd/instances_put.go +++ b/lxd/instances_put.go @@ -3,6 +3,7 @@ package main import ( "context" "encoding/json" + "errors" "fmt" "net/http" "strings" @@ -21,8 +22,8 @@ import ( "github.com/canonical/lxd/shared/version" ) -func coalesceErrors(local bool, errors map[string]error) error { - if len(errors) == 0 { +func coalesceErrors(local bool, errs map[string]error) error { + if len(errs) == 0 { return nil } @@ -31,7 +32,7 @@ func coalesceErrors(local bool, errors map[string]error) error { errorMsg += "The following instances failed to update state:\n" } - for instName, err := range errors { + for instName, err := range errs { if local { errorMsg += fmt.Sprintf(" - Instance: %s: %v\n", instName, err) } else { @@ -39,7 +40,7 @@ func coalesceErrors(local bool, errors map[string]error) error { } } - return fmt.Errorf("%s", errorMsg) + return errors.New(errorMsg) } // swagger:operation PUT /1.0/instances instances instances_put From f3fe0325bc0784a63a77b4f8acdca2682267fe77 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 30 Aug 2024 17:45:35 -0400 Subject: [PATCH 06/14] lxd/profiles_utils: use errors.New() where appropriate Signed-off-by: Simon Deziel --- lxd/profiles_utils.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lxd/profiles_utils.go b/lxd/profiles_utils.go index 89c7f31b81e4..e976fc22eb4c 100644 --- a/lxd/profiles_utils.go +++ b/lxd/profiles_utils.go @@ -2,6 +2,7 @@ package main import ( "context" + "errors" "fmt" "github.com/canonical/lxd/lxd/db" @@ -151,7 +152,7 @@ func doProfileUpdate(s *state.State, p api.Project, profileName string, id int64 msg += fmt.Sprintf(" - Project: %s, Instance: %s: %v\n", inst.Project, inst.Name, err) } - return fmt.Errorf("%s", msg) + return errors.New(msg) } return nil @@ -196,7 +197,7 @@ func doProfileUpdateCluster(s *state.State, projectName string, profileName stri msg += fmt.Sprintf(" - Project: %s, Instance: %s: %v\n", inst.Project, inst.Name, err) } - return fmt.Errorf("%s", msg) + return errors.New(msg) } return nil From 34f14b865e6f762f568c46e20b7d281978d7a5d3 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 3 Sep 2024 11:20:41 -0400 Subject: [PATCH 07/14] lxc/config_device: use errors.New() where appropriate Signed-off-by: Simon Deziel --- lxc/config_device.go | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/lxc/config_device.go b/lxc/config_device.go index f9314af68998..e31f7772c770 100644 --- a/lxc/config_device.go +++ b/lxc/config_device.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "strings" @@ -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 @@ -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 @@ -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 @@ -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 @@ -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]) @@ -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]) @@ -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 @@ -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 @@ -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 { @@ -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 @@ -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) @@ -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) @@ -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 @@ -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 { @@ -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 { @@ -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 From b9e08717793264908974f6dd3f0d8235b957884f Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 3 Sep 2024 11:26:43 -0400 Subject: [PATCH 08/14] lxc/network: use errors.New() where appropriate Signed-off-by: Simon Deziel --- lxc/network.go | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/lxc/network.go b/lxc/network.go index 82672200a6db..68fe93981b4f 100644 --- a/lxc/network.go +++ b/lxc/network.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "io" "os" @@ -156,7 +157,7 @@ func (c *cmdNetworkAttach) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // Default name is same as network @@ -241,7 +242,7 @@ func (c *cmdNetworkAttachProfile) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // Default name is same as network @@ -393,7 +394,7 @@ func (c *cmdNetworkDelete) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // Delete the network @@ -443,7 +444,7 @@ func (c *cmdNetworkDetach) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // Default name is same as network @@ -463,7 +464,7 @@ func (c *cmdNetworkDetach) run(cmd *cobra.Command, args []string) error { for n, d := range inst.Devices { if d["type"] == "nic" && (d["parent"] == resource.name || d["network"] == resource.name) { if devName != "" { - return fmt.Errorf(i18n.G("More than one device matches, specify the device name")) + return errors.New(i18n.G("More than one device matches, specify the device name")) } devName = n @@ -472,16 +473,16 @@ func (c *cmdNetworkDetach) run(cmd *cobra.Command, args []string) error { } if devName == "" { - return fmt.Errorf(i18n.G("No device found for this network")) + return errors.New(i18n.G("No device found for this network")) } device, ok := inst.Devices[devName] if !ok { - return fmt.Errorf(i18n.G("The specified device doesn't exist")) + return errors.New(i18n.G("The specified device doesn't exist")) } if device["type"] != "nic" || (device["parent"] != resource.name && device["network"] != resource.name) { - return fmt.Errorf(i18n.G("The specified device doesn't match the network")) + return errors.New(i18n.G("The specified device doesn't match the network")) } // Remove the device @@ -528,7 +529,7 @@ func (c *cmdNetworkDetachProfile) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // Default name is same as network @@ -548,7 +549,7 @@ func (c *cmdNetworkDetachProfile) run(cmd *cobra.Command, args []string) error { for n, d := range profile.Devices { if d["type"] == "nic" && (d["parent"] == resource.name || d["network"] == resource.name) { if devName != "" { - return fmt.Errorf(i18n.G("More than one device matches, specify the device name")) + return errors.New(i18n.G("More than one device matches, specify the device name")) } devName = n @@ -557,16 +558,16 @@ func (c *cmdNetworkDetachProfile) run(cmd *cobra.Command, args []string) error { } if devName == "" { - return fmt.Errorf(i18n.G("No device found for this network")) + return errors.New(i18n.G("No device found for this network")) } device, ok := profile.Devices[devName] if !ok { - return fmt.Errorf(i18n.G("The specified device doesn't exist")) + return errors.New(i18n.G("The specified device doesn't exist")) } if device["type"] != "nic" || (device["parent"] != resource.name && device["network"] != resource.name) { - return fmt.Errorf(i18n.G("The specified device doesn't match the network")) + return errors.New(i18n.G("The specified device doesn't match the network")) } // Remove the device @@ -633,7 +634,7 @@ func (c *cmdNetworkEdit) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // If stdin isn't a terminal, read text from it @@ -659,7 +660,7 @@ func (c *cmdNetworkEdit) run(cmd *cobra.Command, args []string) error { } if !network.Managed { - return fmt.Errorf(i18n.G("Only managed networks can be modified")) + return errors.New(i18n.G("Only managed networks can be modified")) } data, err := yaml.Marshal(&network) @@ -744,7 +745,7 @@ func (c *cmdNetworkGet) run(cmd *cobra.Command, args []string) error { client := resource.server if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // Get the network key @@ -812,13 +813,13 @@ func (c *cmdNetworkInfo) run(cmd *cobra.Command, args []string) error { client := resource.server if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // Targeting. if c.network.flagTarget != "" { if !client.IsClustered() { - return fmt.Errorf(i18n.G("To use --target, the destination remote must be a cluster")) + return errors.New(i18n.G("To use --target, the destination remote must be a cluster")) } client = client.UseTarget(c.network.flagTarget) @@ -940,7 +941,7 @@ func (c *cmdNetworkList) run(cmd *cobra.Command, args []string) error { // List the networks if resource.name != "" { - return fmt.Errorf(i18n.G("Filtering isn't supported yet")) + return errors.New(i18n.G("Filtering isn't supported yet")) } networks, err := resource.server.GetNetworks() @@ -1027,7 +1028,7 @@ func (c *cmdNetworkListLeases) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // List DHCP leases @@ -1097,7 +1098,7 @@ func (c *cmdNetworkRename) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // Rename the network @@ -1155,7 +1156,7 @@ func (c *cmdNetworkSet) run(cmd *cobra.Command, args []string) error { client := resource.server if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // Handle targeting @@ -1170,7 +1171,7 @@ func (c *cmdNetworkSet) run(cmd *cobra.Command, args []string) error { } if !network.Managed { - return fmt.Errorf(i18n.G("Only managed networks can be modified")) + return errors.New(i18n.G("Only managed networks can be modified")) } // Set the keys @@ -1239,7 +1240,7 @@ func (c *cmdNetworkShow) run(cmd *cobra.Command, args []string) error { client := resource.server if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } // Show the network config From 6b7443a858feccb0e176317b109ecc69ddca4bf9 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 3 Sep 2024 11:28:22 -0400 Subject: [PATCH 09/14] lxc/network_peer: use errors.New() where appropriate Signed-off-by: Simon Deziel --- lxc/network_peer.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lxc/network_peer.go b/lxc/network_peer.go index 8d4e1a742ea4..832a8f5def0f 100644 --- a/lxc/network_peer.go +++ b/lxc/network_peer.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "io" "os" @@ -107,7 +108,7 @@ func (c *cmdNetworkPeerList) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } peers, err := resource.server.GetNetworkPeers(resource.name) @@ -177,11 +178,11 @@ func (c *cmdNetworkPeerShow) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } if args[1] == "" { - return fmt.Errorf(i18n.G("Missing peer name")) + return errors.New(i18n.G("Missing peer name")) } client := resource.server @@ -234,15 +235,15 @@ func (c *cmdNetworkPeerCreate) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } if args[1] == "" { - return fmt.Errorf(i18n.G("Missing peer name")) + return errors.New(i18n.G("Missing peer name")) } if args[2] == "" { - return fmt.Errorf(i18n.G("Missing target network")) + return errors.New(i18n.G("Missing target network")) } targetParts := strings.SplitN(args[2], "/", 2) @@ -352,11 +353,11 @@ func (c *cmdNetworkPeerGet) run(cmd *cobra.Command, args []string) error { client := resource.server if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } if args[1] == "" { - return fmt.Errorf(i18n.G("Missing peer name")) + return errors.New(i18n.G("Missing peer name")) } // Get the current config. @@ -423,11 +424,11 @@ func (c *cmdNetworkPeerSet) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } if args[1] == "" { - return fmt.Errorf(i18n.G("Missing peer name")) + return errors.New(i18n.G("Missing peer name")) } client := resource.server @@ -553,11 +554,11 @@ func (c *cmdNetworkPeerEdit) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } if args[1] == "" { - return fmt.Errorf(i18n.G("Missing peer name")) + return errors.New(i18n.G("Missing peer name")) } client := resource.server @@ -662,11 +663,11 @@ func (c *cmdNetworkPeerDelete) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing network name")) + return errors.New(i18n.G("Missing network name")) } if args[1] == "" { - return fmt.Errorf(i18n.G("Missing peer name")) + return errors.New(i18n.G("Missing peer name")) } client := resource.server From 01abfec5ad58bef3fc0df92d3b851398ae0a9a5b Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 30 Aug 2024 17:51:38 -0400 Subject: [PATCH 10/14] lxc/cluster: use errors.New() where appropriate Signed-off-by: Simon Deziel --- lxc/cluster.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lxc/cluster.go b/lxc/cluster.go index 5ca280025e1c..bd9bd5b8e575 100644 --- a/lxc/cluster.go +++ b/lxc/cluster.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "errors" "fmt" "io" "os" @@ -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 @@ -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 @@ -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 @@ -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. @@ -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 @@ -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 == "" { @@ -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. @@ -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. @@ -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) { @@ -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 { From bc273f008edada386967b892fb00980afd1c02b1 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 3 Sep 2024 11:32:10 -0400 Subject: [PATCH 11/14] lxc/profile: use errors.New() where appropriate Signed-off-by: Simon Deziel --- lxc/profile.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lxc/profile.go b/lxc/profile.go index 7d6df8fdad3a..fc65de8df7cd 100644 --- a/lxc/profile.go +++ b/lxc/profile.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "io" "net/http" @@ -125,7 +126,7 @@ func (c *cmdProfileAdd) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing instance name")) + return errors.New(i18n.G("Missing instance name")) } // Add the profile @@ -198,7 +199,7 @@ func (c *cmdProfileAssign) run(cmd *cobra.Command, args []string) error { // Assign the profiles if resource.name == "" { - return fmt.Errorf(i18n.G("Missing instance name")) + return errors.New(i18n.G("Missing instance name")) } inst, etag, err := resource.server.GetInstance(resource.name) @@ -274,7 +275,7 @@ func (c *cmdProfileCopy) run(cmd *cobra.Command, args []string) error { dest := resources[1] if source.name == "" { - return fmt.Errorf(i18n.G("Missing source profile name")) + return errors.New(i18n.G("Missing source profile name")) } if dest.name == "" { @@ -360,7 +361,7 @@ func (c *cmdProfileCreate) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing profile name")) + return errors.New(i18n.G("Missing profile name")) } // Create the profile @@ -415,7 +416,7 @@ func (c *cmdProfileDelete) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing profile name")) + return errors.New(i18n.G("Missing profile name")) } // Delete the profile @@ -489,7 +490,7 @@ func (c *cmdProfileEdit) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing profile name")) + return errors.New(i18n.G("Missing profile name")) } // If stdin isn't a terminal, read text from it @@ -594,7 +595,7 @@ func (c *cmdProfileGet) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing profile name")) + return errors.New(i18n.G("Missing profile name")) } // Get the configuration key @@ -715,7 +716,7 @@ func (c *cmdProfileRemove) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing instance name")) + return errors.New(i18n.G("Missing instance name")) } // Remove the profile @@ -791,7 +792,7 @@ func (c *cmdProfileRename) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing profile name")) + return errors.New(i18n.G("Missing profile name")) } // Rename the profile @@ -846,7 +847,7 @@ func (c *cmdProfileSet) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing profile name")) + return errors.New(i18n.G("Missing profile name")) } // Get the profile @@ -919,7 +920,7 @@ func (c *cmdProfileShow) run(cmd *cobra.Command, args []string) error { resource := resources[0] if resource.name == "" { - return fmt.Errorf(i18n.G("Missing profile name")) + return errors.New(i18n.G("Missing profile name")) } // Show the profile From b83b57eda8c9b4cab54e5874424dcb0927e47bc3 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 3 Sep 2024 11:33:13 -0400 Subject: [PATCH 12/14] lxc/network: remove unneeded nil check Signed-off-by: Simon Deziel --- lxc/network.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lxc/network.go b/lxc/network.go index 68fe93981b4f..2347a65418db 100644 --- a/lxc/network.go +++ b/lxc/network.go @@ -838,7 +838,7 @@ func (c *cmdNetworkInfo) run(cmd *cobra.Command, args []string) error { fmt.Printf(i18n.G("Type: %s")+"\n", state.Type) // IP addresses. - if state.Addresses != nil && len(state.Addresses) > 0 { + if len(state.Addresses) > 0 { fmt.Println("") fmt.Println(i18n.G("IP addresses:")) for _, addr := range state.Addresses { From 228a5f43cbda83313f07294cb01f0d8d16bf9991 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 3 Sep 2024 11:35:13 -0400 Subject: [PATCH 13/14] lxd/main_forknet: add comment for exported function Signed-off-by: Simon Deziel --- lxd/main_forknet.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lxd/main_forknet.go b/lxd/main_forknet.go index 8c17ecd0b98a..309b1d413ea0 100644 --- a/lxd/main_forknet.go +++ b/lxd/main_forknet.go @@ -119,6 +119,7 @@ type cmdForknet struct { global *cmdGlobal } +// Command performs container network operations. func (c *cmdForknet) Command() *cobra.Command { // Main subcommand cmd := &cobra.Command{} @@ -152,6 +153,7 @@ func (c *cmdForknet) Command() *cobra.Command { return cmd } +// RunInfo prints the information about a NIC. func (c *cmdForknet) RunInfo(cmd *cobra.Command, args []string) error { hostInterfaces, _ := net.Interfaces() networks, err := netutils.NetnsGetifaddrs(-1, hostInterfaces) @@ -169,6 +171,7 @@ func (c *cmdForknet) RunInfo(cmd *cobra.Command, args []string) error { return nil } +// RunDetach detaches a NIC from the host. func (c *cmdForknet) RunDetach(cmd *cobra.Command, args []string) error { lxdPID := args[1] ifName := args[2] From e1d86573570810d47f63c5504b734db89e80895c Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 3 Sep 2024 11:36:41 -0400 Subject: [PATCH 14/14] lxd/main_init_dump: add comment for exported function Signed-off-by: Simon Deziel --- lxd/main_init_dump.go | 1 + 1 file changed, 1 insertion(+) diff --git a/lxd/main_init_dump.go b/lxd/main_init_dump.go index 36dc85a0806f..680f6b4a6f80 100644 --- a/lxd/main_init_dump.go +++ b/lxd/main_init_dump.go @@ -9,6 +9,7 @@ import ( "github.com/canonical/lxd/shared/api" ) +// RunDump dumps the server configuration. func (c *cmdInit) RunDump(d lxd.InstanceServer) error { currentServer, _, err := d.GetServer() if err != nil {