Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit fa7e063

Browse files
committed
Add checks for unsupported flags
Signed-off-by: Ulysses Souza <[email protected]>
1 parent 53f6927 commit fa7e063

File tree

19 files changed

+491
-197
lines changed

19 files changed

+491
-197
lines changed

aci/compose.go

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
"net/http"
2323

2424
"github.com/compose-spec/compose-go/types"
25+
"github.com/docker/compose-cli/utils"
2526
"github.com/docker/compose/v2/pkg/api"
2627
"github.com/docker/compose/v2/pkg/progress"
27-
"github.com/pkg/errors"
2828
"github.com/sirupsen/logrus"
2929

3030
"github.com/docker/compose-cli/aci/convert"
@@ -86,11 +86,36 @@ func (cs *aciComposeService) Copy(ctx context.Context, project *types.Project, o
8686
}
8787

8888
func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error {
89+
if err := checkUnsupportedUpOptions(options); err != nil {
90+
return err
91+
}
8992
return progress.Run(ctx, func(ctx context.Context) error {
9093
return cs.up(ctx, project)
9194
})
9295
}
9396

97+
func checkUnsupportedUpOptions(o api.UpOptions) error {
98+
var errs error
99+
checks := []struct {
100+
toCheck, expected interface{}
101+
option string
102+
}{
103+
{o.Start.CascadeStop, false, "abort-on-container-exit"},
104+
{o.Create.RecreateDependencies, "", "always-recreate-deps"},
105+
{len(o.Start.AttachTo), 0, "attach-dependencies"},
106+
{len(o.Start.ExitCodeFrom), 0, "exit-code-from"},
107+
{o.Create.Recreate, "", "force-recreate"},
108+
{o.Create.QuietPull, false, "quiet-pull"},
109+
{o.Create.RemoveOrphans, false, "remove-orphans"},
110+
{o.Create.Inherit, true, "renew-anon-volumes"},
111+
{o.Create.Timeout, nil, "timeout"},
112+
}
113+
for _, c := range checks {
114+
errs = utils.CheckUnsupported(errs, c.toCheck, c.expected, "up", c.option)
115+
}
116+
return errs
117+
}
118+
94119
func (cs *aciComposeService) up(ctx context.Context, project *types.Project) error {
95120
logrus.Debugf("Up on project with name %q", project.Name)
96121

@@ -130,11 +155,8 @@ func (cs aciComposeService) warnKeepVolumeOnDown(ctx context.Context, projectNam
130155
}
131156

132157
func (cs *aciComposeService) Down(ctx context.Context, projectName string, options api.DownOptions) error {
133-
if options.Volumes {
134-
return errors.Wrap(api.ErrNotImplemented, "--volumes option is not supported on ACI")
135-
}
136-
if options.Images != "" {
137-
return errors.Wrap(api.ErrNotImplemented, "--rmi option is not supported on ACI")
158+
if err := checkUnsupportedDownOptions(options); err != nil {
159+
return err
138160
}
139161
return progress.Run(ctx, func(ctx context.Context) error {
140162
logrus.Debugf("Down on project with name %q", projectName)
@@ -155,7 +177,17 @@ func (cs *aciComposeService) Down(ctx context.Context, projectName string, optio
155177
})
156178
}
157179

180+
func checkUnsupportedDownOptions(o api.DownOptions) error {
181+
var errs error
182+
errs = utils.CheckUnsupported(errs, o.Volumes, false, "down", "volumes")
183+
errs = utils.CheckUnsupported(errs, o.Images, "", "down", "images")
184+
return errs
185+
}
186+
158187
func (cs *aciComposeService) Ps(ctx context.Context, projectName string, options api.PsOptions) ([]api.ContainerSummary, error) {
188+
if err := checkUnsupportedPsOptions(options); err != nil {
189+
return nil, err
190+
}
159191
groupsClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
160192
if err != nil {
161193
return nil, err
@@ -198,7 +230,14 @@ func (cs *aciComposeService) Ps(ctx context.Context, projectName string, options
198230
return res, nil
199231
}
200232

233+
func checkUnsupportedPsOptions(o api.PsOptions) error {
234+
return utils.CheckUnsupported(nil, o.All, false, "ps", "all")
235+
}
236+
201237
func (cs *aciComposeService) List(ctx context.Context, opts api.ListOptions) ([]api.Stack, error) {
238+
if err := checkUnsupportedListOptions(opts); err != nil {
239+
return nil, err
240+
}
202241
containerGroups, err := getACIContainerGroups(ctx, cs.ctx.SubscriptionID, cs.ctx.ResourceGroup)
203242
if err != nil {
204243
return nil, err
@@ -226,6 +265,10 @@ func (cs *aciComposeService) List(ctx context.Context, opts api.ListOptions) ([]
226265
return stacks, nil
227266
}
228267

268+
func checkUnsupportedListOptions(o api.ListOptions) error {
269+
return utils.CheckUnsupported(nil, o.All, false, "ls", "all")
270+
}
271+
229272
func (cs *aciComposeService) Logs(ctx context.Context, projectName string, consumer api.LogConsumer, options api.LogOptions) error {
230273
return api.ErrNotImplemented
231274
}

cli/config/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ type ConfigFlags struct {
3535

3636
// AddConfigFlags adds persistent (global) flags
3737
func (c *ConfigFlags) AddConfigFlags(flags *pflag.FlagSet) {
38-
flags.StringVar(&c.Config, config.ConfigFlagName, confDir(), "Location of the client config files `DIRECTORY`")
38+
flags.StringVar(&c.Config, config.ConfigFlagName, ConfDir(), "Location of the client config files `DIRECTORY`")
3939
}
4040

41-
func confDir() string {
41+
func ConfDir() string {
4242
env := os.Getenv("DOCKER_CONFIG")
4343
if env != "" {
4444
return env

cli/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func exit(ctx string, err error, ctype string) {
324324

325325
if errors.Is(err, api.ErrNotImplemented) {
326326
name := metrics.GetCommand(os.Args[1:])
327-
fmt.Fprintf(os.Stderr, "Command %q not available in current context (%s)\n", name, ctx)
327+
fmt.Fprintf(os.Stderr, "Command %q not available in current context (%s). %q\n", name, ctx, err)
328328

329329
os.Exit(1)
330330
}

cmd/compose/down.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"time"
23+
24+
"github.com/compose-spec/compose-go/types"
25+
"github.com/spf13/cobra"
26+
27+
"github.com/docker/compose-cli/pkg/api"
28+
)
29+
30+
type downOptions struct {
31+
*projectOptions
32+
removeOrphans bool
33+
timeChanged bool
34+
timeout int
35+
volumes bool
36+
images string
37+
}
38+
39+
func downCommand(p *projectOptions, backend api.Service) *cobra.Command {
40+
opts := downOptions{
41+
projectOptions: p,
42+
}
43+
downCmd := &cobra.Command{
44+
Use: "down",
45+
Short: "Stop and remove containers, networks",
46+
PreRun: func(cmd *cobra.Command, args []string) {
47+
opts.timeChanged = cmd.Flags().Changed("timeout")
48+
},
49+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
50+
if opts.images != "" {
51+
if opts.images != "all" && opts.images != "local" {
52+
return fmt.Errorf("invalid value for --rmi: %q", opts.images)
53+
}
54+
}
55+
return nil
56+
}),
57+
RunE: Adapt(func(ctx context.Context, args []string) error {
58+
return runDown(ctx, backend, opts)
59+
}),
60+
ValidArgsFunction: noCompletion(),
61+
}
62+
flags := downCmd.Flags()
63+
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
64+
flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
65+
flags.BoolVarP(&opts.volumes, "volumes", "v", false, "Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.")
66+
flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`)
67+
return downCmd
68+
}
69+
70+
func runDown(ctx context.Context, backend api.Service, opts downOptions) error {
71+
name := opts.ProjectName
72+
var project *types.Project
73+
if opts.ProjectName == "" {
74+
p, err := opts.toProject(nil)
75+
if err != nil {
76+
return err
77+
}
78+
project = p
79+
name = p.Name
80+
}
81+
82+
var timeout *time.Duration
83+
if opts.timeChanged {
84+
timeoutValue := time.Duration(opts.timeout) * time.Second
85+
timeout = &timeoutValue
86+
}
87+
return backend.Down(ctx, name, api.DownOptions{
88+
RemoveOrphans: opts.removeOrphans,
89+
Project: project,
90+
Timeout: timeout,
91+
Images: opts.images,
92+
Volumes: opts.volumes,
93+
})
94+
}

ecs/cloudformation.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/compose-spec/compose-go/types"
3939
"github.com/distribution/distribution/v3/reference"
4040
cliconfig "github.com/docker/cli/cli/config"
41+
"github.com/docker/compose-cli/utils"
4142
"github.com/docker/compose/v2/pkg/api"
4243
"github.com/opencontainers/go-digest"
4344
"sigs.k8s.io/kustomize/kyaml/yaml"
@@ -46,11 +47,10 @@ import (
4647
"github.com/docker/compose-cli/api/config"
4748
)
4849

49-
func (b *ecsAPIService) Kill(ctx context.Context, project *types.Project, options api.KillOptions) error {
50-
return api.ErrNotImplemented
51-
}
52-
5350
func (b *ecsAPIService) Convert(ctx context.Context, project *types.Project, options api.ConvertOptions) ([]byte, error) {
51+
if err := checkUnsupportedConvertOptions(options); err != nil {
52+
return nil, err
53+
}
5454
err := b.resolveServiceImagesDigests(ctx, project)
5555
if err != nil {
5656
return nil, err
@@ -102,6 +102,10 @@ func (b *ecsAPIService) Convert(ctx context.Context, project *types.Project, opt
102102
return bytes, err
103103
}
104104

105+
func checkUnsupportedConvertOptions(o api.ConvertOptions) error {
106+
return utils.CheckUnsupported(nil, o.Output, "", "convert", "output")
107+
}
108+
105109
func (b *ecsAPIService) resolveServiceImagesDigests(ctx context.Context, project *types.Project) error {
106110
configFile, err := cliconfig.Load(config.Dir())
107111
if err != nil {
@@ -368,8 +372,8 @@ func (b *ecsAPIService) createListener(service types.ServiceConfig, port types.S
368372
strings.ToUpper(port.Protocol),
369373
port.Target,
370374
)
371-
//add listener to dependsOn
372-
//https://stackoverflow.com/questions/53971873/the-target-group-does-not-have-an-associated-load-balancer
375+
// add listener to dependsOn
376+
// https://stackoverflow.com/questions/53971873/the-target-group-does-not-have-an-associated-load-balancer
373377
template.Resources[listenerName] = &elasticloadbalancingv2.Listener{
374378
DefaultActions: []elasticloadbalancingv2.Listener_Action{
375379
{

ecs/down.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@ package ecs
1919
import (
2020
"context"
2121

22+
"github.com/docker/compose-cli/utils"
2223
"github.com/docker/compose/v2/pkg/api"
2324
"github.com/docker/compose/v2/pkg/progress"
24-
"github.com/pkg/errors"
2525
)
2626

2727
func (b *ecsAPIService) Down(ctx context.Context, projectName string, options api.DownOptions) error {
28-
if options.Volumes {
29-
return errors.Wrap(api.ErrNotImplemented, "--volumes option is not supported on ECS")
30-
}
31-
if options.Images != "" {
32-
return errors.Wrap(api.ErrNotImplemented, "--rmi option is not supported on ECS")
28+
if err := checkUnsupportedDownOptions(options); err != nil {
29+
return err
3330
}
3431
return progress.Run(ctx, func(ctx context.Context) error {
3532
return b.down(ctx, projectName)
@@ -89,3 +86,21 @@ func doDelete(ctx context.Context, delete func(ctx context.Context, arn string)
8986
return nil
9087
}
9188
}
89+
90+
func checkUnsupportedDownOptions(o api.DownOptions) error {
91+
var errs error
92+
checks := []struct {
93+
toCheck, expected interface{}
94+
option string
95+
}{
96+
{o.Volumes, false, "volumes"},
97+
{o.Images, "", "images"},
98+
{o.RemoveOrphans, false, "remove-orphans"},
99+
{o.Timeout, nil, "timeout"},
100+
}
101+
for _, c := range checks {
102+
errs = utils.CheckUnsupported(errs, c.toCheck, c.expected, "down", c.option)
103+
}
104+
105+
return errs
106+
}

ecs/exec.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

ecs/images.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

ecs/list.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ import (
2020
"context"
2121
"fmt"
2222

23+
"github.com/docker/compose-cli/utils"
2324
"github.com/docker/compose/v2/pkg/api"
2425
)
2526

2627
func (b *ecsAPIService) List(ctx context.Context, opts api.ListOptions) ([]api.Stack, error) {
28+
if err := checkUnsupportedListOptions(opts); err != nil {
29+
return nil, err
30+
}
2731
stacks, err := b.aws.ListStacks(ctx)
2832
if err != nil {
2933
return nil, err
@@ -41,6 +45,10 @@ func (b *ecsAPIService) List(ctx context.Context, opts api.ListOptions) ([]api.S
4145

4246
}
4347

48+
func checkUnsupportedListOptions(o api.ListOptions) error {
49+
return utils.CheckUnsupported(nil, o.All, false, "ls", "all")
50+
}
51+
4452
func (b *ecsAPIService) checkStackState(ctx context.Context, name string) error {
4553
resources, err := b.aws.ListStackResources(ctx, name)
4654
if err != nil {

0 commit comments

Comments
 (0)