cli/command/service: use strings.Cut

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-12-27 16:27:13 +01:00
parent f29992c0f1
commit 2b06c0c42c
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 22 additions and 22 deletions

View File

@ -93,17 +93,17 @@ func (opts *placementPrefOpts) String() string {
// Note: in the future strategies other than "spread", may be supported, // Note: in the future strategies other than "spread", may be supported,
// as well as additional comma-separated options. // as well as additional comma-separated options.
func (opts *placementPrefOpts) Set(value string) error { func (opts *placementPrefOpts) Set(value string) error {
fields := strings.Split(value, "=") strategy, arg, ok := strings.Cut(value, "=")
if len(fields) != 2 { if !ok || strategy == "" {
return errors.New(`placement preference must be of the format "<strategy>=<arg>"`) return errors.New(`placement preference must be of the format "<strategy>=<arg>"`)
} }
if fields[0] != "spread" { if strategy != "spread" {
return errors.Errorf("unsupported placement preference %s (only spread is supported)", fields[0]) return errors.Errorf("unsupported placement preference %s (only spread is supported)", strategy)
} }
opts.prefs = append(opts.prefs, swarm.PlacementPreference{ opts.prefs = append(opts.prefs, swarm.PlacementPreference{
Spread: &swarm.SpreadOver{ Spread: &swarm.SpreadOver{
SpreadDescriptor: fields[1], SpreadDescriptor: arg,
}, },
}) })
opts.strings = append(opts.strings, value) opts.strings = append(opts.strings, value)
@ -121,8 +121,11 @@ type ShlexOpt []string
// Set the value // Set the value
func (s *ShlexOpt) Set(value string) error { func (s *ShlexOpt) Set(value string) error {
valueSlice, err := shlex.Split(value) valueSlice, err := shlex.Split(value)
*s = ShlexOpt(valueSlice) if err != nil {
return err return err
}
*s = valueSlice
return nil
} }
// Type returns the tyep of the value // Type returns the tyep of the value
@ -475,10 +478,12 @@ func (opts *healthCheckOptions) toHealthConfig() (*container.HealthConfig, error
// //
// This assumes input value (<host>:<ip>) has already been validated // This assumes input value (<host>:<ip>) has already been validated
func convertExtraHostsToSwarmHosts(extraHosts []string) []string { func convertExtraHostsToSwarmHosts(extraHosts []string) []string {
hosts := []string{} hosts := make([]string, 0, len(extraHosts))
for _, extraHost := range extraHosts { for _, extraHost := range extraHosts {
parts := strings.SplitN(extraHost, ":", 2) host, ip, ok := strings.Cut(extraHost, ":")
hosts = append(hosts, fmt.Sprintf("%s %s", parts[1], parts[0])) if ok {
hosts = append(hosts, ip+" "+host)
}
} }
return hosts return hosts
} }
@ -628,7 +633,7 @@ func (options *serviceOptions) makeEnv() ([]string, error) {
} }
currentEnv := make([]string, 0, len(envVariables)) currentEnv := make([]string, 0, len(envVariables))
for _, env := range envVariables { // need to process each var, in order 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 for i, current := range currentEnv { // remove duplicates
if current == env { if current == env {
continue // no update required, may hide this behind flag to preserve order of envVariables continue // no update required, may hide this behind flag to preserve order of envVariables

View File

@ -43,7 +43,7 @@ func scaleArgs(cmd *cobra.Command, args []string) error {
return err return err
} }
for _, arg := range args { 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( return errors.Errorf(
"Invalid scale specifier '%s'.\nSee '%s --help'.\n\nUsage: %s\n\n%s", "Invalid scale specifier '%s'.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
arg, arg,
@ -62,8 +62,7 @@ func runScale(dockerCli command.Cli, options *scaleOptions, args []string) error
ctx := context.Background() ctx := context.Background()
for _, arg := range args { for _, arg := range args {
parts := strings.SplitN(arg, "=", 2) serviceID, scaleStr, _ := strings.Cut(arg, "=")
serviceID, scaleStr := parts[0], parts[1]
// validate input arg scale number // validate input arg scale number
scale, err := strconv.ParseUint(scaleStr, 10, 64) scale, err := strconv.ParseUint(scaleStr, 10, 64)

View File

@ -879,8 +879,8 @@ func removeConfigs(flags *pflag.FlagSet, spec *swarm.ContainerSpec, credSpecName
} }
func envKey(value string) string { func envKey(value string) string {
kv := strings.SplitN(value, "=", 2) k, _, _ := strings.Cut(value, "=")
return kv[0] return k
} }
func buildToRemoveSet(flags *pflag.FlagSet, flag string) map[string]struct{} { 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) { if flags.Changed(flagHostRemove) {
extraHostsToRemove := flags.Lookup(flagHostRemove).Value.(*opts.ListOpts).GetAll() extraHostsToRemove := flags.Lookup(flagHostRemove).Value.(*opts.ListOpts).GetAll()
for _, entry := range extraHostsToRemove { for _, entry := range extraHostsToRemove {
v := strings.SplitN(entry, ":", 2) hostName, ipAddr, _ := strings.Cut(entry, ":")
if len(v) > 1 { toRemove = append(toRemove, hostMapping{IPAddr: ipAddr, Host: hostName})
toRemove = append(toRemove, hostMapping{IPAddr: v[1], Host: v[0]})
} else {
toRemove = append(toRemove, hostMapping{Host: v[0]})
}
} }
} }