Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cmd/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,11 @@ func (opts createOptions) Apply(project *types.Project) error {

func applyScaleOpts(project *types.Project, opts []string) error {
for _, scale := range opts {
split := strings.Split(scale, "=")
if len(split) != 2 {
name, val, ok := strings.Cut(scale, "=")
if !ok || val == "" {
return fmt.Errorf("invalid --scale option %q. Should be SERVICE=NUM", scale)
}
name := split[0]
replicas, err := strconv.Atoi(split[1])
replicas, err := strconv.Atoi(val)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/compose/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ func extractEnvCLIDefined(cmdEnvs []string) map[string]string {
// Parse command-line environment variables
cmdEnvMap := make(map[string]string)
for _, env := range cmdEnvs {
parts := strings.SplitN(env, "=", 2)
if len(parts) == 2 {
cmdEnvMap[parts[0]] = parts[1]
key, val, ok := strings.Cut(env, "=")
if ok {
cmdEnvMap[key] = val
}
}
return cmdEnvMap
Expand Down
12 changes: 6 additions & 6 deletions cmd/compose/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ func (p *psOptions) parseFilter() error {
if p.Filter == "" {
return nil
}
parts := strings.SplitN(p.Filter, "=", 2)
if len(parts) != 2 {
key, val, ok := strings.Cut(p.Filter, "=")
if !ok {
return errors.New("arguments to --filter should be in form KEY=VAL")
}
switch parts[0] {
switch key {
case "status":
p.Status = append(p.Status, parts[1])
p.Status = append(p.Status, val)
return nil
case "source":
return api.ErrNotImplemented
default:
return fmt.Errorf("unknown filter %s", parts[0])
return fmt.Errorf("unknown filter %s", key)
}
return nil
}

func psCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
Expand Down
6 changes: 3 additions & 3 deletions cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ func runRun(ctx context.Context, backend api.Compose, project *types.Project, op

labels := types.Labels{}
for _, s := range options.labels {
parts := strings.SplitN(s, "=", 2)
if len(parts) != 2 {
key, val, ok := strings.Cut(s, "=")
if !ok {
return fmt.Errorf("label must be set as KEY=VALUE")
}
labels[parts[0]] = parts[1]
labels[key] = val
}

var buildForRun *api.BuildOptions
Expand Down
17 changes: 7 additions & 10 deletions pkg/compose/convergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,11 +774,10 @@ func (s *composeService) getLinks(ctx context.Context, projectName string, servi
}

for _, rawLink := range service.Links {
linkSplit := strings.Split(rawLink, ":")
linkServiceName := linkSplit[0]
linkName := linkServiceName
if len(linkSplit) == 2 {
linkName = linkSplit[1] // linkName if informed like in: "serviceName:linkName"
// linkName if informed like in: "serviceName[:linkName]"
linkServiceName, linkName, ok := strings.Cut(rawLink, ":")
if !ok {
linkName = linkServiceName
}
cnts, err := getServiceContainers(linkServiceName)
if err != nil {
Expand Down Expand Up @@ -810,11 +809,9 @@ func (s *composeService) getLinks(ctx context.Context, projectName string, servi
}

for _, rawExtLink := range service.ExternalLinks {
extLinkSplit := strings.Split(rawExtLink, ":")
externalLink := extLinkSplit[0]
linkName := externalLink
if len(extLinkSplit) == 2 {
linkName = extLinkSplit[1]
externalLink, linkName, ok := strings.Cut(rawExtLink, ":")
if !ok {
linkName = externalLink
}
links = append(links, format(externalLink, linkName))
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/compose/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,15 @@ func splitCpArg(arg string) (ctr, path string) {
return "", arg
}

parts := strings.SplitN(arg, ":", 2)
ctr, path, ok := strings.Cut(arg, ":")

if len(parts) == 1 || strings.HasPrefix(parts[0], ".") {
if !ok || strings.HasPrefix(ctr, ".") {
// Either there's no `:` in the arg
// OR it's an explicit local relative path like `./file:name.txt`.
return "", arg
}

return parts[0], parts[1]
return ctr, path
}

func resolveLocalPath(localPath string) (absPath string, err error) {
Expand Down
15 changes: 6 additions & 9 deletions pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,8 @@ func (s *composeService) getCreateConfigs(ctx context.Context,
} // VOLUMES/MOUNTS/FILESYSTEMS
tmpfs := map[string]string{}
for _, t := range service.Tmpfs {
if arr := strings.SplitN(t, ":", 2); len(arr) > 1 {
tmpfs[arr[0]] = arr[1]
} else {
tmpfs[arr[0]] = ""
}
k, v, _ := strings.Cut(t, ":")
tmpfs[k] = v
}
binds, mounts, err := s.buildContainerVolumes(ctx, *p, service, inherit)
if err != nil {
Expand Down Expand Up @@ -563,13 +560,13 @@ func defaultNetworkSettings(project *types.Project,
func getRestartPolicy(service types.ServiceConfig) container.RestartPolicy {
var restart container.RestartPolicy
if service.Restart != "" {
split := strings.Split(service.Restart, ":")
name, num, ok := strings.Cut(service.Restart, ":")
var attempts int
if len(split) > 1 {
attempts, _ = strconv.Atoi(split[1])
if ok {
attempts, _ = strconv.Atoi(num)
}
restart = container.RestartPolicy{
Name: mapRestartPolicyCondition(split[0]),
Name: mapRestartPolicyCondition(name),
MaximumRetryCount: attempts,
}
}
Expand Down