Merge remote-tracking branch 'upstream/main' into error-on-unsupported-flags

This commit is contained in:
Ulysses Souza 2021-09-17 11:50:03 +02:00
Родитель fa7e0632ba 3d8ba5205b
Коммит dc7422e494
8 изменённых файлов: 28 добавлений и 128 удалений

Просмотреть файл

@ -1,94 +0,0 @@
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package compose
import (
"context"
"fmt"
"time"
"github.com/compose-spec/compose-go/types"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/pkg/api"
)
type downOptions struct {
*projectOptions
removeOrphans bool
timeChanged bool
timeout int
volumes bool
images string
}
func downCommand(p *projectOptions, backend api.Service) *cobra.Command {
opts := downOptions{
projectOptions: p,
}
downCmd := &cobra.Command{
Use: "down",
Short: "Stop and remove containers, networks",
PreRun: func(cmd *cobra.Command, args []string) {
opts.timeChanged = cmd.Flags().Changed("timeout")
},
PreRunE: Adapt(func(ctx context.Context, args []string) error {
if opts.images != "" {
if opts.images != "all" && opts.images != "local" {
return fmt.Errorf("invalid value for --rmi: %q", opts.images)
}
}
return nil
}),
RunE: Adapt(func(ctx context.Context, args []string) error {
return runDown(ctx, backend, opts)
}),
ValidArgsFunction: noCompletion(),
}
flags := downCmd.Flags()
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
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.")
flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`)
return downCmd
}
func runDown(ctx context.Context, backend api.Service, opts downOptions) error {
name := opts.ProjectName
var project *types.Project
if opts.ProjectName == "" {
p, err := opts.toProject(nil)
if err != nil {
return err
}
project = p
name = p.Name
}
var timeout *time.Duration
if opts.timeChanged {
timeoutValue := time.Duration(opts.timeout) * time.Second
timeout = &timeoutValue
}
return backend.Down(ctx, name, api.DownOptions{
RemoveOrphans: opts.removeOrphans,
Project: project,
Timeout: timeout,
Images: opts.images,
Volumes: opts.volumes,
})
}

Просмотреть файл

@ -38,13 +38,12 @@ import (
"github.com/compose-spec/compose-go/types"
"github.com/distribution/distribution/v3/reference"
cliconfig "github.com/docker/cli/cli/config"
"github.com/docker/compose-cli/api/config"
"github.com/docker/compose-cli/utils"
"github.com/docker/compose/v2/pkg/api"
"github.com/opencontainers/go-digest"
"sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/kustomize/kyaml/yaml/merge2"
"github.com/docker/compose-cli/api/config"
)
func (b *ecsAPIService) Convert(ctx context.Context, project *types.Project, options api.ConvertOptions) ([]byte, error) {

Просмотреть файл

@ -63,7 +63,5 @@ func (b *ecsAPIService) Ps(ctx context.Context, projectName string, options api.
}
func checkUnsupportedPsOptions(o api.PsOptions) error {
var errs error
errs = utils.CheckUnsupported(errs, o.All, false, "ps", "all")
return errs
return utils.CheckUnsupported(nil, o.All, false, "ps", "all")
}

4
go.mod
Просмотреть файл

@ -19,12 +19,12 @@ require (
github.com/awslabs/goformation/v4 v4.15.6
github.com/buger/goterm v1.0.0
github.com/cnabio/cnab-to-oci v0.3.1-beta1
github.com/compose-spec/compose-go v0.0.0-20210906143156-938b039f7805
github.com/compose-spec/compose-go v0.0.0-20210916141509-a7e1bc322970
github.com/containerd/console v1.0.2
github.com/containerd/containerd v1.5.4
github.com/distribution/distribution/v3 v3.0.0-20210316161203-a01c71e2477e
github.com/docker/cli v20.10.7+incompatible
github.com/docker/compose/v2 v2.0.0-rc.3.0.20210916150857-15cd03448553
github.com/docker/compose/v2 v2.0.0-rc.3.0.20210917090141-18059295f76e
github.com/docker/docker v20.10.7+incompatible
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.4.0

7
go.sum
Просмотреть файл

@ -255,8 +255,9 @@ github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnht
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/compose-spec/compose-go v0.0.0-20210906143156-938b039f7805 h1:iFShT4oPik+NnXdzksa4yLvJYDDZBPMjHPN3qtJaXPA=
github.com/compose-spec/compose-go v0.0.0-20210906143156-938b039f7805/go.mod h1:Hnmn5ZCVA3sSBN2urjCZNNIyNqCPayRGH7PmMSaV2Q0=
github.com/compose-spec/compose-go v0.0.0-20210916141509-a7e1bc322970 h1:HJ3V4Wwayx8uVMdcxurMauknQQkuIYhAnFIqUBLQ+po=
github.com/compose-spec/compose-go v0.0.0-20210916141509-a7e1bc322970/go.mod h1:Hnmn5ZCVA3sSBN2urjCZNNIyNqCPayRGH7PmMSaV2Q0=
github.com/containerd/aufs v0.0.0-20200908144142-dab0cbea06f4/go.mod h1:nukgQABAEopAHvB6j7cnP5zJ+/3aVcE7hCYqvIwAHyE=
github.com/containerd/aufs v0.0.0-20201003224125-76a6863f2989/go.mod h1:AkGGQs9NM2vtYHaUen+NljV0/baGCAPELGm2q9ZXpWU=
github.com/containerd/aufs v0.0.0-20210316121734-20793ff83c97/go.mod h1:kL5kd6KM5TzQjR79jljyi4olc1Vrx6XBlcyj3gNv2PU=
@ -414,8 +415,8 @@ github.com/docker/cli v20.10.7+incompatible h1:pv/3NqibQKphWZiAskMzdz8w0PRbtTaEB
github.com/docker/cli v20.10.7+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/cli-docs-tool v0.1.1/go.mod h1:oMzPNt1wC3TcxuY22GMnOODNOxkwGH51gV3AhqAjFQ4=
github.com/docker/compose-on-kubernetes v0.4.19-0.20190128150448-356b2919c496/go.mod h1:iT2pYfi580XlpaV4KmK0T6+4/9+XoKmk/fhoDod1emE=
github.com/docker/compose/v2 v2.0.0-rc.3.0.20210916150857-15cd03448553 h1:uv/kMiCOXbCaFR8vn6pISh50pPWo01rCeSHVxBXGVes=
github.com/docker/compose/v2 v2.0.0-rc.3.0.20210916150857-15cd03448553/go.mod h1:1ElTlmGxnapGk1DF8EscG9uozzScMMBn3wkDeiQCE5k=
github.com/docker/compose/v2 v2.0.0-rc.3.0.20210917090141-18059295f76e h1:prL1r0VoYZmQyEJmy/4q4GITkBOZDM531/qPqBFN3fI=
github.com/docker/compose/v2 v2.0.0-rc.3.0.20210917090141-18059295f76e/go.mod h1:1ElTlmGxnapGk1DF8EscG9uozzScMMBn3wkDeiQCE5k=
github.com/docker/distribution v0.0.0-20190905152932-14b96e55d84c/go.mod h1:0+TTO4EOBfRPhZXAeF1Vu+W3hHZ8eLp8PgKVZlcvtFY=
github.com/docker/distribution v2.6.0-rc.1.0.20180327202408-83389a148052+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/distribution v2.7.0+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=

Просмотреть файл

@ -356,17 +356,9 @@ func (s *composeService) Exec(ctx context.Context, project *types.Project, opts
func checkUnsupportedExecOptions(o api.RunOptions) error {
var errs error
checks := []struct {
toCheck, expected interface{}
option string
}{
{o.Index, 0, "index"},
{o.Privileged, false, "privileged"},
{o.WorkingDir, "", "workdir"},
}
for _, c := range checks {
errs = utils.CheckUnsupported(errs, c.toCheck, c.expected, "exec", c.option)
}
errs = utils.CheckUnsupported(errs, o.Index, 0, "exec", "index")
errs = utils.CheckUnsupported(errs, o.Privileged, false, "exec", "privileged")
errs = utils.CheckUnsupported(errs, o.WorkingDir, "", "exec", "workdir")
return errs
}

Просмотреть файл

@ -19,16 +19,14 @@ package utils
import (
"fmt"
"strings"
"time"
"github.com/docker/compose-cli/api/context/store"
"github.com/docker/compose-cli/cli/config"
"github.com/docker/compose/v2/pkg/api"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
)
var ErrUnsupportedFlag = errors.New("unsupported flag")
func CheckUnsupported(errs error, toCheck, expectedValue interface{}, commandName, msg string) error {
ctype := store.DefaultContextType
currentContext := config.GetCurrentContext("", config.ConfDir(), nil)
@ -43,16 +41,9 @@ func CheckUnsupported(errs error, toCheck, expectedValue interface{}, commandNam
if cc != nil {
ctype = cc.Type()
}
// Fixes type for posterior comparison
switch toCheck.(type) {
case *time.Duration:
if expectedValue == nil {
var nilDurationPtr *time.Duration
expectedValue = nilDurationPtr
}
}
if toCheck != expectedValue {
return multierror.Append(errs, errors.Wrap(ErrUnsupportedFlag,
if !(isNil(toCheck) && isNil(expectedValue)) && toCheck != expectedValue {
return multierror.Append(errs, errors.Wrap(api.ErrUnsupportedFlag,
fmt.Sprintf(`option "%s --%s" on context type %s.`,
commandName, msg, strings.ToUpper(ctype))))
}

Просмотреть файл

@ -17,6 +17,8 @@
package utils
import (
"reflect"
"github.com/docker/go-units"
)
@ -50,3 +52,14 @@ func (m *MemBytes) Type() string {
func (m *MemBytes) Value() int64 {
return int64(*m)
}
func isNil(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
}