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

Commit ba40ef0

Browse files
authored
Merge pull request #2039 from docker/error-on-unsupported-flags
Error on unsupported flags
2 parents 3d8ba52 + 81c5367 commit ba40ef0

File tree

18 files changed

+362
-173
lines changed

18 files changed

+362
-173
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(ctx, 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(ctx context.Context, 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(ctx, 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(ctx, 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(ctx context.Context, o api.DownOptions) error {
181+
var errs error
182+
errs = utils.CheckUnsupported(ctx, errs, o.Volumes, false, "down", "volumes")
183+
errs = utils.CheckUnsupported(ctx, 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(ctx, 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(ctx context.Context, o api.PsOptions) error {
234+
return utils.CheckUnsupported(ctx, 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(ctx, 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(ctx context.Context, o api.ListOptions) error {
269+
return utils.CheckUnsupported(ctx, 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
}

api/config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ import (
2727
"github.com/docker/compose-cli/api/context/store"
2828
)
2929

30+
// ContextKey defines a type for keys in the context passed
31+
type ContextKey string
32+
33+
// ContextTypeKey is the key for context type stored in context.Context
34+
const ContextTypeKey ContextKey = "context_type"
35+
3036
var configDir string
3137

3238
// WithDir sets the config directory path in the context

cli/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func main() {
214214
if cc != nil {
215215
ctype = cc.Type()
216216
}
217+
ctx = context.WithValue(ctx, config.ContextTypeKey, ctype)
217218

218219
service, err := getBackend(ctype, configDir, opts)
219220
if err != nil {
@@ -324,7 +325,7 @@ func exit(ctx string, err error, ctype string) {
324325

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

329330
os.Exit(1)
330331
}

ecs/cloudformation.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,18 @@ 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/api/config"
42+
"github.com/docker/compose-cli/utils"
4143
"github.com/docker/compose/v2/pkg/api"
4244
"github.com/opencontainers/go-digest"
4345
"sigs.k8s.io/kustomize/kyaml/yaml"
4446
"sigs.k8s.io/kustomize/kyaml/yaml/merge2"
45-
46-
"github.com/docker/compose-cli/api/config"
4747
)
4848

49-
func (b *ecsAPIService) Kill(ctx context.Context, project *types.Project, options api.KillOptions) error {
50-
return api.ErrNotImplemented
51-
}
52-
5349
func (b *ecsAPIService) Convert(ctx context.Context, project *types.Project, options api.ConvertOptions) ([]byte, error) {
50+
if err := checkUnsupportedConvertOptions(ctx, options); err != nil {
51+
return nil, err
52+
}
5453
err := b.resolveServiceImagesDigests(ctx, project)
5554
if err != nil {
5655
return nil, err
@@ -102,6 +101,10 @@ func (b *ecsAPIService) Convert(ctx context.Context, project *types.Project, opt
102101
return bytes, err
103102
}
104103

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

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(ctx, 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(ctx context.Context, 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(ctx, errs, c.toCheck, c.expected, "down", c.option)
103+
}
104+
105+
return errs
106+
}

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(ctx, 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(ctx context.Context, o api.ListOptions) error {
49+
return utils.CheckUnsupported(ctx, 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 {

ecs/logs.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,29 @@ import (
2525
)
2626

2727
func (b *ecsAPIService) Logs(ctx context.Context, projectName string, consumer api.LogConsumer, options api.LogOptions) error {
28+
if err := checkUnsupportedLogOptions(ctx, options); err != nil {
29+
return err
30+
}
2831
if len(options.Services) > 0 {
2932
consumer = utils.FilteredLogConsumer(consumer, options.Services)
3033
}
3134
err := b.aws.GetLogs(ctx, projectName, consumer.Log, options.Follow)
3235
return err
3336
}
37+
38+
func checkUnsupportedLogOptions(ctx context.Context, o api.LogOptions) error {
39+
var errs error
40+
checks := []struct {
41+
toCheck, expected interface{}
42+
option string
43+
}{
44+
{o.Since, "", "since"},
45+
{o.Tail, "", "tail"},
46+
{o.Timestamps, false, "timestamps"},
47+
{o.Until, "", "until"},
48+
}
49+
for _, c := range checks {
50+
errs = utils.CheckUnsupported(ctx, errs, c.toCheck, c.expected, "logs", c.option)
51+
}
52+
return errs
53+
}

0 commit comments

Comments
 (0)