Skip to content

Commit 9d53ed8

Browse files
committed
Add --scale to compose create, refactor scale option
Signed-off-by: Laura Brehm <[email protected]>
1 parent d5d9f67 commit 9d53ed8

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

cmd/compose/create.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22+
"strconv"
23+
"strings"
2224
"time"
2325

2426
"github.com/compose-spec/compose-go/types"
@@ -41,6 +43,7 @@ type createOptions struct {
4143
timeChanged bool
4244
timeout int
4345
quietPull bool
46+
scale []string
4447
}
4548

4649
func createCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
@@ -59,7 +62,9 @@ func createCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
5962
return nil
6063
}),
6164
RunE: p.WithProject(func(ctx context.Context, project *types.Project) error {
62-
opts.Apply(project)
65+
if err := opts.Apply(project); err != nil {
66+
return err
67+
}
6368
return backend.Create(ctx, project, api.CreateOptions{
6469
RemoveOrphans: opts.removeOrphans,
6570
IgnoreOrphans: opts.ignoreOrphans,
@@ -79,6 +84,7 @@ func createCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
7984
flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
8085
flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
8186
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
87+
flags.StringArrayVar(&opts.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
8288
return cmd
8389
}
8490

@@ -110,7 +116,7 @@ func (opts createOptions) GetTimeout() *time.Duration {
110116
return nil
111117
}
112118

113-
func (opts createOptions) Apply(project *types.Project) {
119+
func (opts createOptions) Apply(project *types.Project) error {
114120
if opts.pullChanged {
115121
for i, service := range project.Services {
116122
service.PullPolicy = opts.Pull
@@ -135,4 +141,20 @@ func (opts createOptions) Apply(project *types.Project) {
135141
project.Services[i] = service
136142
}
137143
}
144+
for _, scale := range opts.scale {
145+
split := strings.Split(scale, "=")
146+
if len(split) != 2 {
147+
return fmt.Errorf("invalid --scale option %q. Should be SERVICE=NUM", scale)
148+
}
149+
name := split[0]
150+
replicas, err := strconv.Atoi(split[1])
151+
if err != nil {
152+
return err
153+
}
154+
err = setServiceScale(project, name, uint64(replicas))
155+
if err != nil {
156+
return err
157+
}
158+
}
159+
return nil
138160
}

cmd/compose/run.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
197197
return err
198198
}
199199

200-
createOpts.Apply(project)
200+
err = createOpts.Apply(project)
201+
if err != nil {
202+
return err
203+
}
201204

202205
err = progress.Run(ctx, func(ctx context.Context) error {
203206
return startDependencies(ctx, backend, *project, opts.Service, opts.ignoreOrphans)

cmd/compose/up.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22-
"strconv"
23-
"strings"
2422

2523
"github.com/docker/compose/v2/cmd/formatter"
2624

@@ -43,7 +41,6 @@ type upOptions struct {
4341
noDeps bool
4442
cascadeStop bool
4543
exitCodeFrom string
46-
scale []string
4744
noColor bool
4845
noPrefix bool
4946
attachDependencies bool
@@ -68,22 +65,6 @@ func (opts upOptions) apply(project *types.Project, services []string) error {
6865
}
6966
}
7067

71-
for _, scale := range opts.scale {
72-
split := strings.Split(scale, "=")
73-
if len(split) != 2 {
74-
return fmt.Errorf("invalid --scale option %q. Should be SERVICE=NUM", scale)
75-
}
76-
name := split[0]
77-
replicas, err := strconv.Atoi(split[1])
78-
if err != nil {
79-
return err
80-
}
81-
err = setServiceScale(project, name, uint64(replicas))
82-
if err != nil {
83-
return err
84-
}
85-
}
86-
8768
return nil
8869
}
8970

@@ -113,7 +94,7 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
11394
flags.BoolVar(&create.noBuild, "no-build", false, "Don't build an image, even if it's missing.")
11495
flags.StringVar(&create.Pull, "pull", "missing", `Pull image before running ("always"|"missing"|"never")`)
11596
flags.BoolVar(&create.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
116-
flags.StringArrayVar(&up.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
97+
flags.StringArrayVar(&create.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
11798
flags.BoolVar(&up.noColor, "no-color", false, "Produce monochrome output.")
11899
flags.BoolVar(&up.noPrefix, "no-log-prefix", false, "Don't print prefix in logs.")
119100
flags.BoolVar(&create.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
@@ -165,9 +146,12 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
165146
return fmt.Errorf("no service selected")
166147
}
167148

168-
createOptions.Apply(project)
149+
err := createOptions.Apply(project)
150+
if err != nil {
151+
return err
152+
}
169153

170-
err := upOptions.apply(project, services)
154+
err = upOptions.apply(project, services)
171155
if err != nil {
172156
return err
173157
}

cmd/compose/up_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func TestApplyScaleOpt(t *testing.T) {
3434
},
3535
},
3636
}
37-
opt := upOptions{scale: []string{"foo=2"}}
38-
err := opt.apply(&p, nil)
37+
opt := createOptions{scale: []string{"foo=2"}}
38+
err := opt.Apply(&p)
3939
assert.NilError(t, err)
4040
foo, err := p.GetService("foo")
4141
assert.NilError(t, err)

0 commit comments

Comments
 (0)