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,
// 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 "<strategy>=<arg>"`)
}
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 (<host>:<ip>) 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

View File

@ -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)

View File

@ -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})
}
}