From 3f36556563afb131a1066787cc0cf1e903410815 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Thu, 26 Sep 2024 12:26:13 -0400 Subject: [PATCH 1/4] lxc/config: use strings.Cut() instead of SplitN() in ParseRemote() Signed-off-by: Simon Deziel --- lxc/config/remote.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lxc/config/remote.go b/lxc/config/remote.go index 457cbf0e3857..3c18b6218706 100644 --- a/lxc/config/remote.go +++ b/lxc/config/remote.go @@ -32,22 +32,22 @@ type Remote struct { // ParseRemote splits remote and object. func (c *Config) ParseRemote(raw string) (remoteName string, resourceName string, err error) { - result := strings.SplitN(raw, ":", 2) - if len(result) == 1 { + remote, object, found := strings.Cut(raw, ":") + if !found { return c.DefaultRemote, raw, nil } - _, ok := c.Remotes[result[0]] + _, ok := c.Remotes[remote] if !ok { // Attempt to play nice with snapshots containing ":" - if shared.IsSnapshot(raw) && shared.IsSnapshot(result[0]) { + if shared.IsSnapshot(raw) && shared.IsSnapshot(remote) { return c.DefaultRemote, raw, nil } - return "", "", fmt.Errorf("The remote \"%s\" doesn't exist", result[0]) + return "", "", fmt.Errorf("The remote \"%s\" doesn't exist", remote) } - return result[0], result[1], nil + return remote, object, nil } // GetInstanceServer returns a lxd.InstanceServer for the remote with the given name. From 490f4442fc739c6d0447a34f9a1c46bac298375b Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Thu, 26 Sep 2024 17:11:09 -0400 Subject: [PATCH 2/4] lxc/exec: use strings.Cut() instead of SplitN() Signed-off-by: Simon Deziel --- lxc/exec.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lxc/exec.go b/lxc/exec.go index 15658ea0f13b..771920177a0a 100644 --- a/lxc/exec.go +++ b/lxc/exec.go @@ -124,13 +124,10 @@ func (c *cmdExec) run(cmd *cobra.Command, args []string) error { } for _, arg := range c.flagEnvironment { - pieces := strings.SplitN(arg, "=", 2) - value := "" - if len(pieces) > 1 { - value = pieces[1] + variable, value, found := strings.Cut(arg, "=") + if found { + env[variable] = value } - - env[pieces[0]] = value } // Configure the terminal From 5a3899f9adf359538d655c6d12321393745188a8 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Thu, 26 Sep 2024 17:14:13 -0400 Subject: [PATCH 3/4] lxc/completion: use strings.Cut() instead of SplitN() Signed-off-by: Simon Deziel --- lxc/completion.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lxc/completion.go b/lxc/completion.go index d115eba5c9bb..f4b10f1c6fe7 100644 --- a/lxc/completion.go +++ b/lxc/completion.go @@ -334,7 +334,7 @@ func (g *cmdGlobal) cmpInstancesAndSnapshots(toComplete string) ([]string, cobra resource := resources[0] if strings.Contains(resource.name, shared.SnapshotDelimiter) { - instName := strings.SplitN(resource.name, shared.SnapshotDelimiter, 2)[0] + instName, _, _ := strings.Cut(resource.name, shared.SnapshotDelimiter) snapshots, _ := resource.server.GetInstanceSnapshotNames(instName) for _, snapshot := range snapshots { results = append(results, fmt.Sprintf("%s/%s", instName, snapshot)) From 8825e31c3088c8fb64b685ac8ea06f862326c68d Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Thu, 26 Sep 2024 17:21:07 -0400 Subject: [PATCH 4/4] lxc/project: use strings.Cut() instead of SplitN() Signed-off-by: Simon Deziel --- lxc/project.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lxc/project.go b/lxc/project.go index 5b10f9239c8b..5c8252e1628e 100644 --- a/lxc/project.go +++ b/lxc/project.go @@ -960,7 +960,7 @@ func (c *cmdProjectInfo) run(cmd *cobra.Command, args []string) error { byteLimits := []string{"disk", "memory"} data := [][]string{} for k, v := range projectState.Resources { - shortKey := strings.SplitN(k, ".", 2)[0] + shortKey, _, _ := strings.Cut(k, ".") limit := i18n.G("UNLIMITED") if v.Limit >= 0 { @@ -979,9 +979,9 @@ func (c *cmdProjectInfo) run(cmd *cobra.Command, args []string) error { } columnName := strings.ToUpper(k) - fields := strings.SplitN(columnName, ".", 2) - if len(fields) == 2 { - columnName = fmt.Sprintf("%s (%s)", fields[0], fields[1]) + before, after, found := strings.Cut(columnName, ".") + if found { + columnName = fmt.Sprintf("%s (%s)", before, after) } data = append(data, []string{columnName, limit, usage})