From 2b06c0c42c694f991addd14bbbaa3236828b6ddb Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 27 Dec 2022 16:27:13 +0100 Subject: [PATCH] cli/command/service: use strings.Cut Signed-off-by: Sebastiaan van Stijn --- cli/command/service/opts.go | 27 ++++++++++++++++----------- cli/command/service/scale.go | 5 ++--- cli/command/service/update.go | 12 ++++-------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/cli/command/service/opts.go b/cli/command/service/opts.go index 05b3b8055d..fa44b84962 100644 --- a/cli/command/service/opts.go +++ b/cli/command/service/opts.go @@ -93,17 +93,17 @@ func (opts *placementPrefOpts) String() string { // Note: in the future strategies other than "spread", may be supported, // as well as additional comma-separated options. func (opts *placementPrefOpts) Set(value string) error { - fields := strings.Split(value, "=") - if len(fields) != 2 { + strategy, arg, ok := strings.Cut(value, "=") + if !ok || strategy == "" { return errors.New(`placement preference must be of the format "="`) } - if fields[0] != "spread" { - return errors.Errorf("unsupported placement preference %s (only spread is supported)", fields[0]) + if strategy != "spread" { + return errors.Errorf("unsupported placement preference %s (only spread is supported)", strategy) } opts.prefs = append(opts.prefs, swarm.PlacementPreference{ Spread: &swarm.SpreadOver{ - SpreadDescriptor: fields[1], + SpreadDescriptor: arg, }, }) opts.strings = append(opts.strings, value) @@ -121,8 +121,11 @@ type ShlexOpt []string // Set the value func (s *ShlexOpt) Set(value string) error { valueSlice, err := shlex.Split(value) - *s = ShlexOpt(valueSlice) - return err + if err != nil { + return err + } + *s = valueSlice + return nil } // Type returns the tyep of the value @@ -475,10 +478,12 @@ func (opts *healthCheckOptions) toHealthConfig() (*container.HealthConfig, error // // This assumes input value (:) has already been validated func convertExtraHostsToSwarmHosts(extraHosts []string) []string { - hosts := []string{} + hosts := make([]string, 0, len(extraHosts)) for _, extraHost := range extraHosts { - parts := strings.SplitN(extraHost, ":", 2) - hosts = append(hosts, fmt.Sprintf("%s %s", parts[1], parts[0])) + host, ip, ok := strings.Cut(extraHost, ":") + if ok { + hosts = append(hosts, ip+" "+host) + } } return hosts } @@ -628,7 +633,7 @@ func (options *serviceOptions) makeEnv() ([]string, error) { } currentEnv := make([]string, 0, len(envVariables)) for _, env := range envVariables { // need to process each var, in order - k := strings.SplitN(env, "=", 2)[0] + k, _, _ := strings.Cut(env, "=") for i, current := range currentEnv { // remove duplicates if current == env { continue // no update required, may hide this behind flag to preserve order of envVariables diff --git a/cli/command/service/scale.go b/cli/command/service/scale.go index 4bc00e5050..f2625445eb 100644 --- a/cli/command/service/scale.go +++ b/cli/command/service/scale.go @@ -43,7 +43,7 @@ func scaleArgs(cmd *cobra.Command, args []string) error { return err } for _, arg := range args { - if parts := strings.SplitN(arg, "=", 2); len(parts) != 2 { + if k, v, ok := strings.Cut(arg, "="); !ok || k == "" || v == "" { return errors.Errorf( "Invalid scale specifier '%s'.\nSee '%s --help'.\n\nUsage: %s\n\n%s", arg, @@ -62,8 +62,7 @@ func runScale(dockerCli command.Cli, options *scaleOptions, args []string) error ctx := context.Background() for _, arg := range args { - parts := strings.SplitN(arg, "=", 2) - serviceID, scaleStr := parts[0], parts[1] + serviceID, scaleStr, _ := strings.Cut(arg, "=") // validate input arg scale number scale, err := strconv.ParseUint(scaleStr, 10, 64) diff --git a/cli/command/service/update.go b/cli/command/service/update.go index ff6f2fcf95..482b91bc2f 100644 --- a/cli/command/service/update.go +++ b/cli/command/service/update.go @@ -879,8 +879,8 @@ func removeConfigs(flags *pflag.FlagSet, spec *swarm.ContainerSpec, credSpecName } func envKey(value string) string { - kv := strings.SplitN(value, "=", 2) - return kv[0] + k, _, _ := strings.Cut(value, "=") + return k } func buildToRemoveSet(flags *pflag.FlagSet, flag string) map[string]struct{} { @@ -1174,12 +1174,8 @@ func updateHosts(flags *pflag.FlagSet, hosts *[]string) error { if flags.Changed(flagHostRemove) { extraHostsToRemove := flags.Lookup(flagHostRemove).Value.(*opts.ListOpts).GetAll() for _, entry := range extraHostsToRemove { - v := strings.SplitN(entry, ":", 2) - if len(v) > 1 { - toRemove = append(toRemove, hostMapping{IPAddr: v[1], Host: v[0]}) - } else { - toRemove = append(toRemove, hostMapping{Host: v[0]}) - } + hostName, ipAddr, _ := strings.Cut(entry, ":") + toRemove = append(toRemove, hostMapping{IPAddr: ipAddr, Host: hostName}) } }