From 49e7f2d45ddc5a0c1c3361eb337c4850841407ff Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 15 Jun 2021 09:57:38 +0200 Subject: [PATCH] Move compose v2 implementation under pkg/compose with dependencies Signed-off-by: Nicolas De Loof --- aci/aci.go | 2 +- aci/compose.go | 2 +- aci/context.go | 2 +- aci/volumes.go | 2 +- cli/cmd/run/run.go | 2 +- cli/cmd/volume/command.go | 6 +- cli/main.go | 17 +-- cli/metrics/generatecommands/main.go | 2 +- cli/metrics/metrics.go | 2 +- cli/mobycli/exec.go | 13 +- cli/server/metrics.go | 7 +- cmd/compose/compose.go | 12 +- cmd/compose/convert.go | 4 +- cmd/compose/images.go | 7 +- cmd/compose/ps.go | 2 +- cmd/compose/pull.go | 2 +- cmd/compose/run.go | 2 +- cmd/compose/up.go | 5 +- cmd/main.go | 7 +- ecs/context.go | 6 +- ecs/context_test.go | 2 +- ecs/down.go | 2 +- ecs/local/backend.go | 4 +- ecs/up.go | 2 +- ecs/wait.go | 4 +- kube/client/client.go | 3 +- kube/client/utils.go | 3 +- kube/compose.go | 5 +- kube/context.go | 2 +- local/backend.go | 4 +- local/compose/build_win.go | 111 ------------------ local/moby/convert.go | 60 ---------- {local => pkg}/compose/attach.go | 11 +- {local => pkg}/compose/build.go | 21 ++-- pkg/compose/build_win.go | 28 +++++ {local => pkg}/compose/compose.go | 0 {local/moby => pkg/compose}/container.go | 2 +- {local => pkg}/compose/containers.go | 2 +- {local => pkg}/compose/convergence.go | 17 ++- {local => pkg}/compose/convergence_test.go | 0 pkg/compose/convert.go | 83 +++++++++++++ {local => pkg}/compose/cp.go | 0 {local => pkg}/compose/create.go | 13 +- {local => pkg}/compose/create_test.go | 0 {local => pkg}/compose/dependencies.go | 2 +- {local => pkg}/compose/dependencies_test.go | 0 {local => pkg}/compose/down.go | 2 +- {local => pkg}/compose/down_test.go | 2 +- .../compose/errors.go | 20 ++-- {local => pkg}/compose/events.go | 4 +- {local => pkg}/compose/exec.go | 0 {local => pkg}/compose/filters.go | 0 {utils => pkg/compose}/hash.go | 2 +- {local => pkg}/compose/images.go | 2 +- {local => pkg}/compose/kill.go | 2 +- {local => pkg}/compose/kill_test.go | 7 +- {local => pkg}/compose/logs.go | 4 +- {local => pkg}/compose/ls.go | 0 {local => pkg}/compose/ls_test.go | 0 .../definitions.go => pkg/compose/metrics.go | 3 +- {local => pkg}/compose/pause.go | 2 +- {local => pkg}/compose/port.go | 0 {local => pkg}/compose/printer.go | 0 {local => pkg}/compose/ps.go | 0 {local => pkg}/compose/ps_test.go | 2 +- {local => pkg}/compose/pull.go | 9 +- {local => pkg}/compose/push.go | 10 +- {local => pkg}/compose/remove.go | 11 +- {local => pkg}/compose/resize.go | 0 {local => pkg}/compose/restart.go | 8 +- {local => pkg}/compose/run.go | 0 {local => pkg}/compose/start.go | 2 +- {local => pkg}/compose/status.go | 0 {local => pkg}/compose/stop.go | 2 +- {local => pkg}/compose/stop_test.go | 5 +- .../compose/testdata/docker-compose.yml | 0 {local => pkg}/compose/top.go | 0 {local => pkg}/compose/up.go | 2 +- {local => pkg}/mocks/mock_docker_api.go | 0 {api => pkg}/progress/event.go | 0 {api => pkg}/progress/noop.go | 0 {api => pkg}/progress/plain.go | 0 {api => pkg}/progress/spinner.go | 0 {api => pkg}/progress/tty.go | 2 +- {api => pkg}/progress/tty_test.go | 0 {api => pkg}/progress/writer.go | 0 {api => pkg}/progress/writer_test.go | 0 {utils => pkg}/prompt/prompt.go | 2 +- {utils => pkg}/prompt/prompt_mock.go | 2 +- {utils => pkg/utils}/scan_suggest.go | 0 {utils => pkg/utils}/stringutils.go | 0 {utils => pkg/utils}/writer.go | 0 {utils => pkg/utils}/writer_test.go | 0 93 files changed, 261 insertions(+), 331 deletions(-) delete mode 100644 local/compose/build_win.go rename {local => pkg}/compose/attach.go (94%) rename {local => pkg}/compose/build.go (93%) create mode 100644 pkg/compose/build_win.go rename {local => pkg}/compose/compose.go (100%) rename {local/moby => pkg/compose}/container.go (99%) rename {local => pkg}/compose/containers.go (98%) rename {local => pkg}/compose/convergence.go (97%) rename {local => pkg}/compose/convergence_test.go (100%) create mode 100644 pkg/compose/convert.go rename {local => pkg}/compose/cp.go (100%) rename {local => pkg}/compose/create.go (98%) rename {local => pkg}/compose/create_test.go (100%) rename {local => pkg}/compose/dependencies.go (99%) rename {local => pkg}/compose/dependencies_test.go (100%) rename {local => pkg}/compose/down.go (99%) rename {local => pkg}/compose/down_test.go (99%) rename cli/metrics/compose_errors.go => pkg/compose/errors.go (76%) rename {local => pkg}/compose/events.go (97%) rename {local => pkg}/compose/exec.go (100%) rename {local => pkg}/compose/filters.go (100%) rename {utils => pkg/compose}/hash.go (98%) rename {local => pkg}/compose/images.go (98%) rename {local => pkg}/compose/kill.go (97%) rename {local => pkg}/compose/kill_test.go (98%) rename {local => pkg}/compose/logs.go (97%) rename {local => pkg}/compose/ls.go (100%) rename {local => pkg}/compose/ls_test.go (100%) rename cli/metrics/definitions.go => pkg/compose/metrics.go (99%) rename {local => pkg}/compose/pause.go (98%) rename {local => pkg}/compose/port.go (100%) rename {local => pkg}/compose/printer.go (100%) rename {local => pkg}/compose/ps.go (100%) rename {local => pkg}/compose/ps_test.go (98%) rename {local => pkg}/compose/pull.go (94%) rename {local => pkg}/compose/push.go (92%) rename {local => pkg}/compose/remove.go (92%) rename {local => pkg}/compose/resize.go (100%) rename {local => pkg}/compose/restart.go (94%) rename {local => pkg}/compose/run.go (100%) rename {local => pkg}/compose/start.go (98%) rename {local => pkg}/compose/status.go (100%) rename {local => pkg}/compose/stop.go (97%) rename {local => pkg}/compose/stop_test.go (97%) rename {local => pkg}/compose/testdata/docker-compose.yml (100%) rename {local => pkg}/compose/top.go (100%) rename {local => pkg}/compose/up.go (98%) rename {local => pkg}/mocks/mock_docker_api.go (100%) rename {api => pkg}/progress/event.go (100%) rename {api => pkg}/progress/noop.go (100%) rename {api => pkg}/progress/plain.go (100%) rename {api => pkg}/progress/spinner.go (100%) rename {api => pkg}/progress/tty.go (99%) rename {api => pkg}/progress/tty_test.go (100%) rename {api => pkg}/progress/writer.go (100%) rename {api => pkg}/progress/writer_test.go (100%) rename {utils => pkg}/prompt/prompt.go (96%) rename {utils => pkg}/prompt/prompt_mock.go (97%) rename {utils => pkg/utils}/scan_suggest.go (100%) rename {utils => pkg/utils}/stringutils.go (100%) rename {utils => pkg/utils}/writer.go (100%) rename {utils => pkg/utils}/writer_test.go (100%) diff --git a/aci/aci.go b/aci/aci.go index 78e9303f..9bb98eaf 100644 --- a/aci/aci.go +++ b/aci/aci.go @@ -39,8 +39,8 @@ import ( "github.com/docker/compose-cli/api/client" "github.com/docker/compose-cli/api/containers" "github.com/docker/compose-cli/api/context/store" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" ) func createACIContainers(ctx context.Context, aciContext store.AciContext, groupDefinition containerinstance.ContainerGroup) error { diff --git a/aci/compose.go b/aci/compose.go index 5f357595..e3a83135 100644 --- a/aci/compose.go +++ b/aci/compose.go @@ -28,8 +28,8 @@ import ( "github.com/docker/compose-cli/aci/convert" "github.com/docker/compose-cli/aci/login" "github.com/docker/compose-cli/api/context/store" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" "github.com/docker/compose-cli/utils/formatter" ) diff --git a/aci/context.go b/aci/context.go index 9345cefd..42f7b627 100644 --- a/aci/context.go +++ b/aci/context.go @@ -29,7 +29,7 @@ import ( "github.com/docker/compose-cli/api/context/store" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils/prompt" + "github.com/docker/compose-cli/pkg/prompt" ) // ContextParams options for creating ACI context diff --git a/aci/volumes.go b/aci/volumes.go index 13c9d9a7..530c836a 100644 --- a/aci/volumes.go +++ b/aci/volumes.go @@ -30,9 +30,9 @@ import ( "github.com/docker/compose-cli/aci/login" "github.com/docker/compose-cli/api/context/store" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/api/volumes" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" ) type aciVolumeService struct { diff --git a/cli/cmd/run/run.go b/cli/cmd/run/run.go index b00e4054..3e83376a 100644 --- a/cli/cmd/run/run.go +++ b/cli/cmd/run/run.go @@ -29,8 +29,8 @@ import ( "github.com/docker/compose-cli/api/client" "github.com/docker/compose-cli/api/containers" "github.com/docker/compose-cli/api/context/store" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/cli/options/run" + "github.com/docker/compose-cli/pkg/progress" ) // Command runs a container diff --git a/cli/cmd/volume/command.go b/cli/cmd/volume/command.go index 9b35b521..cc062c7b 100644 --- a/cli/cmd/volume/command.go +++ b/cli/cmd/volume/command.go @@ -23,10 +23,10 @@ import ( "github.com/docker/compose-cli/aci" "github.com/docker/compose-cli/api/client" "github.com/docker/compose-cli/api/context/store" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/cli/formatter" - formatter2 "github.com/docker/compose-cli/cli/formatter" + format "github.com/docker/compose-cli/cli/formatter" "github.com/docker/compose-cli/ecs" + "github.com/docker/compose-cli/pkg/progress" "github.com/hashicorp/go-multierror" "github.com/spf13/cobra" @@ -146,7 +146,7 @@ func inspectVolume() *cobra.Command { if err != nil { return err } - outJSON, err := formatter2.ToStandardJSON(v) + outJSON, err := format.ToStandardJSON(v) if err != nil { return err } diff --git a/cli/main.go b/cli/main.go index 86a7bae1..e98568d4 100644 --- a/cli/main.go +++ b/cli/main.go @@ -51,6 +51,7 @@ import ( compose2 "github.com/docker/compose-cli/cmd/compose" "github.com/docker/compose-cli/local" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/compose" // Backend registrations _ "github.com/docker/compose-cli/aci" @@ -237,7 +238,7 @@ func main() { if err = root.ExecuteContext(ctx); err != nil { handleError(ctx, err, ctype, currentContext, cc, root) } - metrics.Track(ctype, os.Args[1:], metrics.SuccessStatus) + metrics.Track(ctype, os.Args[1:], compose.SuccessStatus) } func customizeCliForACI(command *cobra.Command, proxy *api.ServiceProxy) { @@ -271,7 +272,7 @@ func getBackend(ctype string, configDir string, opts cliopts.GlobalOpts) (backen func handleError(ctx context.Context, err error, ctype string, currentContext string, cc *store.DockerContext, root *cobra.Command) { // if user canceled request, simply exit without any error message if api.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) { - metrics.Track(ctype, os.Args[1:], metrics.CanceledStatus) + metrics.Track(ctype, os.Args[1:], compose.CanceledStatus) os.Exit(130) } if ctype == store.AwsContextType { @@ -293,20 +294,20 @@ $ docker context create %s `, cc.Type(), store.EcsContextType), ctype) func exit(ctx string, err error, ctype string) { if exit, ok := err.(cli.StatusError); ok { - metrics.Track(ctype, os.Args[1:], metrics.SuccessStatus) + metrics.Track(ctype, os.Args[1:], compose.SuccessStatus) os.Exit(exit.StatusCode) } - var composeErr metrics.ComposeError - metricsStatus := metrics.FailureStatus + var composeErr compose.Error + metricsStatus := compose.FailureStatus exitCode := 1 if errors.As(err, &composeErr) { metricsStatus = composeErr.GetMetricsFailureCategory().MetricsStatus exitCode = composeErr.GetMetricsFailureCategory().ExitCode } if strings.HasPrefix(err.Error(), "unknown shorthand flag:") || strings.HasPrefix(err.Error(), "unknown flag:") || strings.HasPrefix(err.Error(), "unknown docker command:") { - metricsStatus = metrics.CommandSyntaxFailure.MetricsStatus - exitCode = metrics.CommandSyntaxFailure.ExitCode + metricsStatus = compose.CommandSyntaxFailure.MetricsStatus + exitCode = compose.CommandSyntaxFailure.ExitCode } metrics.Track(ctype, os.Args[1:], metricsStatus) @@ -343,7 +344,7 @@ func checkIfUnknownCommandExistInDefaultContext(err error, currentContext string if mobycli.IsDefaultContextCommand(dockerCommand) { fmt.Fprintf(os.Stderr, "Command %q not available in current context (%s), you can use the \"default\" context to run this command\n", dockerCommand, currentContext) - metrics.Track(contextType, os.Args[1:], metrics.FailureStatus) + metrics.Track(contextType, os.Args[1:], compose.FailureStatus) os.Exit(1) } } diff --git a/cli/metrics/generatecommands/main.go b/cli/metrics/generatecommands/main.go index c24c6305..caa29197 100644 --- a/cli/metrics/generatecommands/main.go +++ b/cli/metrics/generatecommands/main.go @@ -21,7 +21,7 @@ import ( "os/exec" "strings" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/utils" ) var managementCommands = []string{"ecs", "scan"} diff --git a/cli/metrics/metrics.go b/cli/metrics/metrics.go index 1811e0c3..fcd3967a 100644 --- a/cli/metrics/metrics.go +++ b/cli/metrics/metrics.go @@ -20,7 +20,7 @@ import ( "os" "strings" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/utils" ) // Track sends the tracking analytics to Docker Desktop diff --git a/cli/mobycli/exec.go b/cli/mobycli/exec.go index f6f2cdc6..89b8bb64 100644 --- a/cli/mobycli/exec.go +++ b/cli/mobycli/exec.go @@ -24,13 +24,14 @@ import ( "os/signal" "regexp" - "github.com/spf13/cobra" - apicontext "github.com/docker/compose-cli/api/context" "github.com/docker/compose-cli/api/context/store" "github.com/docker/compose-cli/cli/metrics" "github.com/docker/compose-cli/cli/mobycli/resolvepath" - "github.com/docker/compose-cli/utils" + "github.com/spf13/cobra" + + "github.com/docker/compose-cli/pkg/compose" + "github.com/docker/compose-cli/pkg/utils" ) var delegatedContextTypes = []string{store.DefaultContextType} @@ -68,10 +69,10 @@ func Exec(root *cobra.Command) { if err != nil { if exiterr, ok := err.(*exec.ExitError); ok { exitCode := exiterr.ExitCode() - metrics.Track(store.DefaultContextType, os.Args[1:], metrics.ByExitCode(exitCode).MetricsStatus) + metrics.Track(store.DefaultContextType, os.Args[1:], compose.ByExitCode(exitCode).MetricsStatus) os.Exit(exitCode) } - metrics.Track(store.DefaultContextType, os.Args[1:], metrics.FailureStatus) + metrics.Track(store.DefaultContextType, os.Args[1:], compose.FailureStatus) fmt.Fprintln(os.Stderr, err) os.Exit(1) } @@ -79,7 +80,7 @@ func Exec(root *cobra.Command) { if command == "build" && !metrics.HasQuietFlag(os.Args[1:]) { utils.DisplayScanSuggestMsg() } - metrics.Track(store.DefaultContextType, os.Args[1:], metrics.SuccessStatus) + metrics.Track(store.DefaultContextType, os.Args[1:], compose.SuccessStatus) os.Exit(0) } diff --git a/cli/server/metrics.go b/cli/server/metrics.go index 8dd5065e..439eb69a 100644 --- a/cli/server/metrics.go +++ b/cli/server/metrics.go @@ -23,6 +23,7 @@ import ( "github.com/docker/compose-cli/cli/metrics" "github.com/docker/compose-cli/cli/server/proxy" + "github.com/docker/compose-cli/pkg/compose" ) var ( @@ -60,16 +61,16 @@ func metricsServerInterceptor(client metrics.Client) grpc.UnaryServerInterceptor data, err := handler(ctx, req) - status := metrics.SuccessStatus + status := compose.SuccessStatus if err != nil { - status = metrics.FailureStatus + status = compose.FailureStatus } command := methodMapping[info.FullMethod] if command != "" { client.Send(metrics.Command{ Command: command, Context: contextType, - Source: metrics.APISource, + Source: compose.APISource, Status: status, }) } diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 89a39227..99dca23b 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -35,8 +35,8 @@ import ( "github.com/docker/compose-cli/api/context/store" "github.com/docker/compose-cli/cli/formatter" - "github.com/docker/compose-cli/cli/metrics" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/compose" ) //Command defines a compose CLI command as a func with args @@ -58,11 +58,11 @@ func Adapt(fn Command) func(cmd *cobra.Command, args []string) error { }() } err := fn(ctx, args) - var composeErr metrics.ComposeError + var composeErr compose.Error if api.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) { err = dockercli.StatusError{ StatusCode: 130, - Status: metrics.CanceledStatus, + Status: compose.CanceledStatus, } } if errors.As(err, &composeErr) { @@ -155,12 +155,12 @@ func (o *projectOptions) toProjectName() (string, error) { func (o *projectOptions) toProject(services []string, po ...cli.ProjectOptionsFn) (*types.Project, error) { options, err := o.toProjectOptions(po...) if err != nil { - return nil, metrics.WrapComposeError(err) + return nil, compose.WrapComposeError(err) } project, err := cli.ProjectFromOptions(options) if err != nil { - return nil, metrics.WrapComposeError(err) + return nil, compose.WrapComposeError(err) } if len(services) > 0 { @@ -209,7 +209,7 @@ func RootCommand(contextType string, backend api.Service) *cobra.Command { } _ = cmd.Help() return dockercli.StatusError{ - StatusCode: metrics.CommandSyntaxFailure.ExitCode, + StatusCode: compose.CommandSyntaxFailure.ExitCode, Status: fmt.Sprintf("unknown docker command: %q", "compose "+args[0]), } }, diff --git a/cmd/compose/convert.go b/cmd/compose/convert.go index fb15b2ca..8dd606aa 100644 --- a/cmd/compose/convert.go +++ b/cmd/compose/convert.go @@ -33,7 +33,7 @@ import ( "github.com/spf13/cobra" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/compose" ) type convertOptions struct { @@ -180,7 +180,7 @@ func runHash(opts convertOptions) error { return err } for _, s := range project.Services { - hash, err := utils.ServiceHash(s) + hash, err := compose.ServiceHash(s) if err != nil { return err } diff --git a/cmd/compose/images.go b/cmd/compose/images.go index bc57c680..ea77e912 100644 --- a/cmd/compose/images.go +++ b/cmd/compose/images.go @@ -24,14 +24,13 @@ import ( "sort" "strings" + "github.com/docker/docker/pkg/stringid" + "github.com/docker/go-units" "github.com/spf13/cobra" "github.com/docker/compose-cli/cli/formatter" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" - "github.com/docker/docker/pkg/stringid" - - "github.com/docker/go-units" + "github.com/docker/compose-cli/pkg/utils" ) type imageOptions struct { diff --git a/cmd/compose/ps.go b/cmd/compose/ps.go index 5660b3fe..5e89833d 100644 --- a/cmd/compose/ps.go +++ b/cmd/compose/ps.go @@ -28,7 +28,7 @@ import ( "github.com/docker/compose-cli/cli/formatter" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/utils" ) type psOptions struct { diff --git a/cmd/compose/pull.go b/cmd/compose/pull.go index b5cc47fd..6d3417ca 100644 --- a/cmd/compose/pull.go +++ b/cmd/compose/pull.go @@ -25,7 +25,7 @@ import ( "github.com/spf13/cobra" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/utils" ) type pullOptions struct { diff --git a/cmd/compose/run.go b/cmd/compose/run.go index 397f6499..61b50ee6 100644 --- a/cmd/compose/run.go +++ b/cmd/compose/run.go @@ -29,8 +29,8 @@ import ( "github.com/spf13/cobra" "github.com/docker/cli/cli" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" ) type runOptions struct { diff --git a/cmd/compose/up.go b/cmd/compose/up.go index dd030937..a1e7fcbf 100644 --- a/cmd/compose/up.go +++ b/cmd/compose/up.go @@ -24,10 +24,11 @@ import ( "strings" "github.com/compose-spec/compose-go/types" + "github.com/spf13/cobra" + "github.com/docker/compose-cli/cli/formatter" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" - "github.com/spf13/cobra" + "github.com/docker/compose-cli/pkg/utils" ) // composeOptions hold options common to `up` and `run` to run compose project diff --git a/cmd/main.go b/cmd/main.go index 322b7d76..93ee8265 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -26,11 +26,10 @@ import ( "github.com/spf13/cobra" "github.com/docker/compose-cli/api/context/store" - "github.com/docker/compose-cli/cli/metrics" commands "github.com/docker/compose-cli/cmd/compose" "github.com/docker/compose-cli/internal" - impl "github.com/docker/compose-cli/local/compose" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/compose" ) func main() { @@ -42,7 +41,7 @@ func main() { if err := plugin.PersistentPreRunE(cmd, args); err != nil { return err } - lazyInit.WithService(impl.NewComposeService(dockerCli.Client(), dockerCli.ConfigFile())) + lazyInit.WithService(compose.NewComposeService(dockerCli.Client(), dockerCli.ConfigFile())) if originalPreRun != nil { return originalPreRun(cmd, args) } @@ -50,7 +49,7 @@ func main() { } cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error { return dockercli.StatusError{ - StatusCode: metrics.CommandSyntaxFailure.ExitCode, + StatusCode: compose.CommandSyntaxFailure.ExitCode, Status: err.Error(), } }) diff --git a/ecs/context.go b/ecs/context.go index 1b07a9b9..4566b591 100644 --- a/ecs/context.go +++ b/ecs/context.go @@ -24,9 +24,7 @@ import ( "sort" "strings" - "github.com/docker/compose-cli/api/context/store" - "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils/prompt" + "github.com/docker/compose-cli/pkg/prompt" "github.com/AlecAivazis/survey/v2/terminal" "github.com/aws/aws-sdk-go/aws" @@ -34,6 +32,8 @@ import ( "github.com/aws/aws-sdk-go/aws/defaults" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/docker/compose-cli/api/context/store" + "github.com/docker/compose-cli/pkg/api" "github.com/pkg/errors" "gopkg.in/ini.v1" ) diff --git a/ecs/context_test.go b/ecs/context_test.go index 9982a54f..1889e70f 100644 --- a/ecs/context_test.go +++ b/ecs/context_test.go @@ -22,7 +22,7 @@ import ( "testing" "github.com/docker/compose-cli/api/context/store" - "github.com/docker/compose-cli/utils/prompt" + "github.com/docker/compose-cli/pkg/prompt" "github.com/golang/mock/gomock" "gotest.tools/v3/assert" diff --git a/ecs/down.go b/ecs/down.go index a399cffb..3c12ec0e 100644 --- a/ecs/down.go +++ b/ecs/down.go @@ -21,8 +21,8 @@ import ( "github.com/pkg/errors" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" ) func (b *ecsAPIService) Down(ctx context.Context, projectName string, options api.DownOptions) error { diff --git a/ecs/local/backend.go b/ecs/local/backend.go index 17767c62..04e20ad7 100644 --- a/ecs/local/backend.go +++ b/ecs/local/backend.go @@ -29,8 +29,8 @@ import ( "github.com/docker/compose-cli/api/resources" "github.com/docker/compose-cli/api/secrets" "github.com/docker/compose-cli/api/volumes" - local_compose "github.com/docker/compose-cli/local/compose" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/compose" ) const backendType = store.EcsLocalSimulationContextType @@ -52,7 +52,7 @@ func service() (backend.Service, error) { return &ecsLocalSimulation{ moby: apiClient, - compose: local_compose.NewComposeService(apiClient, cliconfig.LoadDefaultConfigFile(os.Stderr)), + compose: compose.NewComposeService(apiClient, cliconfig.LoadDefaultConfigFile(os.Stderr)), }, nil } diff --git a/ecs/up.go b/ecs/up.go index c73f3d50..c3a6f285 100644 --- a/ecs/up.go +++ b/ecs/up.go @@ -26,8 +26,8 @@ import ( "github.com/compose-spec/compose-go/types" "github.com/sirupsen/logrus" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" ) func (b *ecsAPIService) Build(ctx context.Context, project *types.Project, options api.BuildOptions) error { diff --git a/ecs/wait.go b/ecs/wait.go index 3d3e7069..3974035e 100644 --- a/ecs/wait.go +++ b/ecs/wait.go @@ -23,10 +23,10 @@ import ( "strings" "time" + "github.com/docker/compose-cli/pkg/progress" + "github.com/aws/aws-sdk-go/aws" "github.com/iancoleman/strcase" - - "github.com/docker/compose-cli/api/progress" ) func (b *ecsAPIService) WaitStackCompletion(ctx context.Context, name string, operation int, ignored ...string) error { //nolint:gocyclo diff --git a/kube/client/client.go b/kube/client/client.go index f78748ae..5b24a726 100644 --- a/kube/client/client.go +++ b/kube/client/client.go @@ -28,7 +28,8 @@ import ( "time" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/utils" + "golang.org/x/sync/errgroup" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" diff --git a/kube/client/utils.go b/kube/client/utils.go index 18eb4bc3..ae4c0866 100644 --- a/kube/client/utils.go +++ b/kube/client/utils.go @@ -23,7 +23,8 @@ import ( "time" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/utils" + corev1 "k8s.io/api/core/v1" ) diff --git a/kube/compose.go b/kube/compose.go index 98efeb8e..960889a4 100644 --- a/kube/compose.go +++ b/kube/compose.go @@ -28,11 +28,12 @@ import ( apicontext "github.com/docker/compose-cli/api/context" "github.com/docker/compose-cli/api/context/store" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/kube/client" "github.com/docker/compose-cli/kube/helm" "github.com/docker/compose-cli/kube/resources" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" + utils2 "github.com/docker/compose-cli/pkg/utils" "github.com/docker/compose-cli/utils" ) @@ -175,7 +176,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a state = progress.Working } w.Event(progress.NewEvent(pod, state, message)) - if !utils.StringContains(events, pod) { + if !utils2.StringContains(events, pod) { events = append(events, pod) } }, diff --git a/kube/context.go b/kube/context.go index 0c80cf28..0159036a 100644 --- a/kube/context.go +++ b/kube/context.go @@ -26,7 +26,7 @@ import ( "github.com/docker/compose-cli/api/context/store" "github.com/docker/compose-cli/kube/resources" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils/prompt" + "github.com/docker/compose-cli/pkg/prompt" ) // ContextParams options for creating a Kubernetes context diff --git a/local/backend.go b/local/backend.go index 0e8b21e0..b92ee349 100644 --- a/local/backend.go +++ b/local/backend.go @@ -30,8 +30,8 @@ import ( "github.com/docker/compose-cli/api/secrets" "github.com/docker/compose-cli/api/volumes" cliopts "github.com/docker/compose-cli/cli/options" - local_compose "github.com/docker/compose-cli/local/compose" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/compose" ) type local struct { @@ -46,7 +46,7 @@ func NewService(apiClient client.APIClient) backend.Service { return &local{ containerService: &containerService{apiClient}, volumeService: &volumeService{apiClient}, - composeService: local_compose.NewComposeService(apiClient, file), + composeService: compose.NewComposeService(apiClient, file), } } diff --git a/local/compose/build_win.go b/local/compose/build_win.go deleted file mode 100644 index 72dba122..00000000 --- a/local/compose/build_win.go +++ /dev/null @@ -1,111 +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 ( - "fmt" - - "github.com/docker/buildx/build" - "github.com/docker/compose-cli/cli/mobycli" -) - -func (s *composeService) windowsBuild(opts map[string]build.Options, mode string) error { - for serviceName, options := range opts { - imageName := serviceName - dockerfile := options.Inputs.DockerfilePath - - if options.Inputs.DockerfilePath == "-" { // image needs to be pulled - imageName := options.Tags[0] - err := shellOutMoby("pull", imageName) - if err != nil { - return err - } - } else { - cmd := &commandBuilder{ - Path: options.Inputs.ContextPath, - } - cmd.addParams("--build-arg", options.BuildArgs) - cmd.addFlag("--pull", options.Pull) - cmd.addArg("--progress", mode) - - cacheFrom := []string{} - for _, cacheImage := range options.CacheFrom { - cacheFrom = append(cacheFrom, cacheImage.Attrs["ref"]) - } - cmd.addList("--cache-from", cacheFrom) - cmd.addArg("--file", dockerfile) - cmd.addParams("--label", options.Labels) - cmd.addArg("--network", options.NetworkMode) - cmd.addArg("--target", options.Target) - cmd.addList("--add-host", options.ExtraHosts) - cmd.addArg("--tag", imageName) - - err := shellOutMoby(cmd.getArguments()...) - if err != nil { - return err - } - } - } - return nil -} - -func shellOutMoby(args ...string) error { - childExit := make(chan bool) - err := mobycli.RunDocker(childExit, args...) - childExit <- true - return err -} - -type commandBuilder struct { - Args []string - Path string -} - -func (c *commandBuilder) addArg(name, value string) { - if value != "" { - c.Args = append(c.Args, name, value) - } -} - -func (c *commandBuilder) addFlag(name string, flag bool) { - if flag { - c.Args = append(c.Args, name) - } -} - -func (c *commandBuilder) addParams(name string, params map[string]string) { - if len(params) > 0 { - for k, v := range params { - c.Args = append(c.Args, name, fmt.Sprintf("%s=%s", k, v)) - } - } -} - -func (c *commandBuilder) addList(name string, values []string) { - if len(values) > 0 { - for _, v := range values { - c.Args = append(c.Args, name, v) - } - } -} - -func (c *commandBuilder) getArguments() []string { - cmd := []string{"build"} - cmd = append(cmd, c.Args...) - cmd = append(cmd, c.Path) - return cmd -} diff --git a/local/moby/convert.go b/local/moby/convert.go index 02c265d4..b4118095 100644 --- a/local/moby/convert.go +++ b/local/moby/convert.go @@ -21,9 +21,7 @@ import ( "sort" "strconv" "strings" - "time" - compose "github.com/compose-spec/compose-go/types" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/go-connections/nat" @@ -96,64 +94,6 @@ func ToPorts(ports []types.Port) []containers.Port { return result } -// ToMobyEnv convert into []string -func ToMobyEnv(environment compose.MappingWithEquals) []string { - var env []string - for k, v := range environment { - if v == nil { - env = append(env, k) - } else { - env = append(env, fmt.Sprintf("%s=%s", k, *v)) - } - } - return env -} - -// ToMobyHealthCheck convert into container.HealthConfig -func ToMobyHealthCheck(check *compose.HealthCheckConfig) *container.HealthConfig { - if check == nil { - return nil - } - var ( - interval time.Duration - timeout time.Duration - period time.Duration - retries int - ) - if check.Interval != nil { - interval = time.Duration(*check.Interval) - } - if check.Timeout != nil { - timeout = time.Duration(*check.Timeout) - } - if check.StartPeriod != nil { - period = time.Duration(*check.StartPeriod) - } - if check.Retries != nil { - retries = int(*check.Retries) - } - test := check.Test - if check.Disable { - test = []string{"NONE"} - } - return &container.HealthConfig{ - Test: test, - Interval: interval, - Timeout: timeout, - StartPeriod: period, - Retries: retries, - } -} - -// ToSeconds convert into seconds -func ToSeconds(d *compose.Duration) *int { - if d == nil { - return nil - } - s := int(time.Duration(*d).Seconds()) - return &s -} - // FromPorts convert to nat.Port / nat.PortBinding func FromPorts(ports []containers.Port) (map[nat.Port]struct{}, map[nat.Port][]nat.PortBinding, error) { var ( diff --git a/local/compose/attach.go b/pkg/compose/attach.go similarity index 94% rename from local/compose/attach.go rename to pkg/compose/attach.go index 9245e247..7226ad0d 100644 --- a/local/compose/attach.go +++ b/pkg/compose/attach.go @@ -22,14 +22,13 @@ import ( "io" "strings" - convert "github.com/docker/compose-cli/local/moby" - "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" - "github.com/compose-spec/compose-go/types" "github.com/docker/cli/cli/streams" + "github.com/docker/compose-cli/pkg/api" moby "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/stdcopy" + + "github.com/docker/compose-cli/pkg/utils" ) func (s *composeService) attach(ctx context.Context, project *types.Project, listener api.ContainerEventListener, selectedServices []string) (Containers, error) { @@ -138,8 +137,8 @@ func (s *composeService) getContainerStreams(ctx context.Context, container stri Logs: false, }) if err == nil { - stdout = convert.ContainerStdout{HijackedResponse: cnx} - stdin = convert.ContainerStdin{HijackedResponse: cnx} + stdout = ContainerStdout{HijackedResponse: cnx} + stdin = ContainerStdin{HijackedResponse: cnx} return stdin, stdout, nil } diff --git a/local/compose/build.go b/pkg/compose/build.go similarity index 93% rename from local/compose/build.go rename to pkg/compose/build.go index bc196cd0..7d978ffd 100644 --- a/local/compose/build.go +++ b/pkg/compose/build.go @@ -27,21 +27,20 @@ import ( "github.com/docker/buildx/driver" _ "github.com/docker/buildx/driver/docker" // required to get default driver registered "github.com/docker/buildx/util/buildflags" - "github.com/docker/buildx/util/progress" + xprogress "github.com/docker/buildx/util/progress" moby "github.com/docker/docker/api/types" bclient "github.com/moby/buildkit/client" "github.com/moby/buildkit/session" "github.com/moby/buildkit/session/auth/authprovider" specs "github.com/opencontainers/image-spec/specs-go/v1" - composeprogress "github.com/docker/compose-cli/api/progress" - "github.com/docker/compose-cli/cli/metrics" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/progress" + "github.com/docker/compose-cli/pkg/utils" ) func (s *composeService) Build(ctx context.Context, project *types.Project, options api.BuildOptions) error { - return composeprogress.Run(ctx, func(ctx context.Context) error { + return progress.Run(ctx, func(ctx context.Context) error { return s.build(ctx, project, options) }) } @@ -108,9 +107,9 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types. return err } - mode := progress.PrinterModeAuto + mode := xprogress.PrinterModeAuto if quietPull { - mode = progress.PrinterModeQuiet + mode = xprogress.PrinterModeQuiet } opts, imagesToBuild, err := s.getBuildOptions(project, images) if err != nil { @@ -193,7 +192,7 @@ func (s *composeService) doBuild(ctx context.Context, project *types.Project, op // no support yet for Windows container builds in Buildkit // https://docs.docker.com/develop/develop-images/build_enhancements/#limitations err := s.windowsBuild(opts, mode) - return nil, metrics.WrapCategorisedComposeError(err, metrics.BuildFailure) + return nil, WrapCategorisedComposeError(err, BuildFailure) } if len(opts) == 0 { return nil, nil @@ -216,7 +215,7 @@ func (s *composeService) doBuild(ctx context.Context, project *types.Project, op // build and will lock progressCtx, cancel := context.WithCancel(context.Background()) defer cancel() - w := progress.NewPrinter(progressCtx, os.Stdout, mode) + w := xprogress.NewPrinter(progressCtx, os.Stdout, mode) // We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here response, err := build.Build(ctx, driverInfo, opts, nil, nil, w) @@ -225,10 +224,10 @@ func (s *composeService) doBuild(ctx context.Context, project *types.Project, op err = errW } if err != nil { - return nil, metrics.WrapCategorisedComposeError(err, metrics.BuildFailure) + return nil, WrapCategorisedComposeError(err, BuildFailure) } - cw := composeprogress.ContextWriter(ctx) + cw := progress.ContextWriter(ctx) for _, c := range observedState { for imageName := range opts { if c.Image == imageName { diff --git a/pkg/compose/build_win.go b/pkg/compose/build_win.go new file mode 100644 index 00000000..3bae1646 --- /dev/null +++ b/pkg/compose/build_win.go @@ -0,0 +1,28 @@ +/* + 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 ( + "github.com/docker/buildx/build" + + "github.com/docker/compose-cli/pkg/api" +) + +func (s *composeService) windowsBuild(opts map[string]build.Options, mode string) error { + // FIXME copy/paste or reuse code from https://github.com/docker/cli/blob/master/cli/command/image/build.go + return api.ErrNotImplemented +} diff --git a/local/compose/compose.go b/pkg/compose/compose.go similarity index 100% rename from local/compose/compose.go rename to pkg/compose/compose.go diff --git a/local/moby/container.go b/pkg/compose/container.go similarity index 99% rename from local/moby/container.go rename to pkg/compose/container.go index faaf0baa..954aed72 100644 --- a/local/moby/container.go +++ b/pkg/compose/container.go @@ -14,7 +14,7 @@ limitations under the License. */ -package moby +package compose import ( "io" diff --git a/local/compose/containers.go b/pkg/compose/containers.go similarity index 98% rename from local/compose/containers.go rename to pkg/compose/containers.go index fa539f54..9ab2f61c 100644 --- a/local/compose/containers.go +++ b/pkg/compose/containers.go @@ -24,7 +24,7 @@ import ( "github.com/docker/docker/api/types/filters" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/utils" ) // Containers is a set of moby Container diff --git a/local/compose/convergence.go b/pkg/compose/convergence.go similarity index 97% rename from local/compose/convergence.go rename to pkg/compose/convergence.go index 9e1e514b..37a169b3 100644 --- a/local/compose/convergence.go +++ b/pkg/compose/convergence.go @@ -31,10 +31,9 @@ import ( "github.com/sirupsen/logrus" "golang.org/x/sync/errgroup" - "github.com/docker/compose-cli/api/progress" - status "github.com/docker/compose-cli/local/moby" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/progress" + "github.com/docker/compose-cli/pkg/utils" ) const ( @@ -99,7 +98,7 @@ func (s *composeService) ensureService(ctx context.Context, project *types.Proje return nil } - expected, err := utils.ServiceHash(service) + expected, err := ServiceHash(service) if err != nil { return err } @@ -118,11 +117,11 @@ func (s *composeService) ensureService(ctx context.Context, project *types.Proje w := progress.ContextWriter(ctx) switch container.State { - case status.ContainerRunning: + case ContainerRunning: w.Event(progress.RunningEvent(name)) - case status.ContainerCreated: - case status.ContainerRestarting: - case status.ContainerExited: + case ContainerCreated: + case ContainerRestarting: + case ContainerExited: w.Event(progress.CreatedEvent(name)) default: eg.Go(func() error { @@ -420,7 +419,7 @@ func (s *composeService) startService(ctx context.Context, project *types.Projec eg, ctx := errgroup.WithContext(ctx) for _, c := range containers { container := c - if container.State == status.ContainerRunning { + if container.State == ContainerRunning { continue } eg.Go(func() error { diff --git a/local/compose/convergence_test.go b/pkg/compose/convergence_test.go similarity index 100% rename from local/compose/convergence_test.go rename to pkg/compose/convergence_test.go diff --git a/pkg/compose/convert.go b/pkg/compose/convert.go new file mode 100644 index 00000000..6f092ee2 --- /dev/null +++ b/pkg/compose/convert.go @@ -0,0 +1,83 @@ +/* + 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 ( + "fmt" + "time" + + compose "github.com/compose-spec/compose-go/types" + "github.com/docker/docker/api/types/container" +) + +// ToMobyEnv convert into []string +func ToMobyEnv(environment compose.MappingWithEquals) []string { + var env []string + for k, v := range environment { + if v == nil { + env = append(env, k) + } else { + env = append(env, fmt.Sprintf("%s=%s", k, *v)) + } + } + return env +} + +// ToMobyHealthCheck convert into container.HealthConfig +func ToMobyHealthCheck(check *compose.HealthCheckConfig) *container.HealthConfig { + if check == nil { + return nil + } + var ( + interval time.Duration + timeout time.Duration + period time.Duration + retries int + ) + if check.Interval != nil { + interval = time.Duration(*check.Interval) + } + if check.Timeout != nil { + timeout = time.Duration(*check.Timeout) + } + if check.StartPeriod != nil { + period = time.Duration(*check.StartPeriod) + } + if check.Retries != nil { + retries = int(*check.Retries) + } + test := check.Test + if check.Disable { + test = []string{"NONE"} + } + return &container.HealthConfig{ + Test: test, + Interval: interval, + Timeout: timeout, + StartPeriod: period, + Retries: retries, + } +} + +// ToSeconds convert into seconds +func ToSeconds(d *compose.Duration) *int { + if d == nil { + return nil + } + s := int(time.Duration(*d).Seconds()) + return &s +} diff --git a/local/compose/cp.go b/pkg/compose/cp.go similarity index 100% rename from local/compose/cp.go rename to pkg/compose/cp.go diff --git a/local/compose/create.go b/pkg/compose/create.go similarity index 98% rename from local/compose/create.go rename to pkg/compose/create.go index 5cb8ef62..4cd480f7 100644 --- a/local/compose/create.go +++ b/pkg/compose/create.go @@ -38,11 +38,10 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/internal" - convert "github.com/docker/compose-cli/local/moby" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/progress" + "github.com/docker/compose-cli/pkg/utils" ) func (s *composeService) Create(ctx context.Context, project *types.Project, options api.CreateOptions) error { @@ -205,7 +204,7 @@ func getImageName(service types.ServiceConfig, projectName string) string { func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project, service types.ServiceConfig, number int, inherit *moby.Container, autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) { - hash, err := utils.ServiceHash(service) + hash, err := ServiceHash(service) if err != nil { return nil, nil, nil, err } @@ -267,10 +266,10 @@ func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project, MacAddress: service.MacAddress, Labels: labels, StopSignal: service.StopSignal, - Env: convert.ToMobyEnv(service.Environment), - Healthcheck: convert.ToMobyHealthCheck(service.HealthCheck), + Env: ToMobyEnv(service.Environment), + Healthcheck: ToMobyHealthCheck(service.HealthCheck), Volumes: volumeMounts, - StopTimeout: convert.ToSeconds(service.StopGracePeriod), + StopTimeout: ToSeconds(service.StopGracePeriod), } portBindings := buildContainerPortBindingOptions(service) diff --git a/local/compose/create_test.go b/pkg/compose/create_test.go similarity index 100% rename from local/compose/create_test.go rename to pkg/compose/create_test.go diff --git a/local/compose/dependencies.go b/pkg/compose/dependencies.go similarity index 99% rename from local/compose/dependencies.go rename to pkg/compose/dependencies.go index 00d7b3ef..d278ff3e 100644 --- a/local/compose/dependencies.go +++ b/pkg/compose/dependencies.go @@ -25,7 +25,7 @@ import ( "github.com/compose-spec/compose-go/types" "golang.org/x/sync/errgroup" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/utils" ) // ServiceStatus indicates the status of a service diff --git a/local/compose/dependencies_test.go b/pkg/compose/dependencies_test.go similarity index 100% rename from local/compose/dependencies_test.go rename to pkg/compose/dependencies_test.go diff --git a/local/compose/down.go b/pkg/compose/down.go similarity index 99% rename from local/compose/down.go rename to pkg/compose/down.go index bd4fcc43..e56b77a6 100644 --- a/local/compose/down.go +++ b/pkg/compose/down.go @@ -29,8 +29,8 @@ import ( "github.com/docker/docker/errdefs" "golang.org/x/sync/errgroup" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" ) type downOp func() error diff --git a/local/compose/down_test.go b/pkg/compose/down_test.go similarity index 99% rename from local/compose/down_test.go rename to pkg/compose/down_test.go index 0361631b..efbff240 100644 --- a/local/compose/down_test.go +++ b/pkg/compose/down_test.go @@ -20,8 +20,8 @@ import ( "context" "testing" - "github.com/docker/compose-cli/local/mocks" compose "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/mocks" moby "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" diff --git a/cli/metrics/compose_errors.go b/pkg/compose/errors.go similarity index 76% rename from cli/metrics/compose_errors.go rename to pkg/compose/errors.go index 7a1b0643..d97fe713 100644 --- a/cli/metrics/compose_errors.go +++ b/pkg/compose/errors.go @@ -14,18 +14,18 @@ limitations under the License. */ -package metrics +package compose import ( "io/fs" "github.com/pkg/errors" - composeerrdefs "github.com/compose-spec/compose-go/errdefs" + "github.com/compose-spec/compose-go/errdefs" ) -// ComposeError error to categorize failures and extract metrics info -type ComposeError struct { +// Error error to categorize failures and extract metrics info +type Error struct { Err error Category *FailureCategory } @@ -35,7 +35,7 @@ func WrapComposeError(err error) error { if err == nil { return nil } - return ComposeError{ + return Error{ Err: err, } } @@ -45,19 +45,19 @@ func WrapCategorisedComposeError(err error, failure FailureCategory) error { if err == nil { return nil } - return ComposeError{ + return Error{ Err: err, Category: &failure, } } // Unwrap get underlying error -func (e ComposeError) Unwrap() error { return e.Err } +func (e Error) Unwrap() error { return e.Err } -func (e ComposeError) Error() string { return e.Err.Error() } +func (e Error) Error() string { return e.Err.Error() } // GetMetricsFailureCategory get metrics status and error code corresponding to this error -func (e ComposeError) GetMetricsFailureCategory() FailureCategory { +func (e Error) GetMetricsFailureCategory() FailureCategory { if e.Category != nil { return *e.Category } @@ -65,7 +65,7 @@ func (e ComposeError) GetMetricsFailureCategory() FailureCategory { if errors.As(e.Err, &pathError) { return FileNotFoundFailure } - if composeerrdefs.IsNotFoundError(e.Err) { + if errdefs.IsNotFoundError(e.Err) { return FileNotFoundFailure } return ComposeParseFailure diff --git a/local/compose/events.go b/pkg/compose/events.go similarity index 97% rename from local/compose/events.go rename to pkg/compose/events.go index 1da8b3e2..a17fe707 100644 --- a/local/compose/events.go +++ b/pkg/compose/events.go @@ -22,10 +22,10 @@ import ( "time" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" - moby "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" + + "github.com/docker/compose-cli/pkg/utils" ) func (s *composeService) Events(ctx context.Context, project string, options api.EventsOptions) error { diff --git a/local/compose/exec.go b/pkg/compose/exec.go similarity index 100% rename from local/compose/exec.go rename to pkg/compose/exec.go diff --git a/local/compose/filters.go b/pkg/compose/filters.go similarity index 100% rename from local/compose/filters.go rename to pkg/compose/filters.go diff --git a/utils/hash.go b/pkg/compose/hash.go similarity index 98% rename from utils/hash.go rename to pkg/compose/hash.go index ffe30cff..56f7e3e9 100644 --- a/utils/hash.go +++ b/pkg/compose/hash.go @@ -14,7 +14,7 @@ limitations under the License. */ -package utils +package compose import ( "encoding/json" diff --git a/local/compose/images.go b/pkg/compose/images.go similarity index 98% rename from local/compose/images.go rename to pkg/compose/images.go index 844711e2..92351570 100644 --- a/local/compose/images.go +++ b/pkg/compose/images.go @@ -28,7 +28,7 @@ import ( "golang.org/x/sync/errgroup" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/utils" ) func (s *composeService) Images(ctx context.Context, projectName string, options api.ImagesOptions) ([]api.ImageSummary, error) { diff --git a/local/compose/kill.go b/pkg/compose/kill.go similarity index 97% rename from local/compose/kill.go rename to pkg/compose/kill.go index e9a1f87f..e542dcd5 100644 --- a/local/compose/kill.go +++ b/pkg/compose/kill.go @@ -23,8 +23,8 @@ import ( moby "github.com/docker/docker/api/types" "golang.org/x/sync/errgroup" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" ) func (s *composeService) Kill(ctx context.Context, project *types.Project, options api.KillOptions) error { diff --git a/local/compose/kill_test.go b/pkg/compose/kill_test.go similarity index 98% rename from local/compose/kill_test.go rename to pkg/compose/kill_test.go index 101ea64b..cc75f8bf 100644 --- a/local/compose/kill_test.go +++ b/pkg/compose/kill_test.go @@ -21,15 +21,14 @@ import ( "path/filepath" "testing" + "github.com/compose-spec/compose-go/types" + moby "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/golang/mock/gomock" "gotest.tools/v3/assert" - "github.com/compose-spec/compose-go/types" - moby "github.com/docker/docker/api/types" - - "github.com/docker/compose-cli/local/mocks" compose "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/mocks" ) const testProject = "testProject" diff --git a/local/compose/logs.go b/pkg/compose/logs.go similarity index 97% rename from local/compose/logs.go rename to pkg/compose/logs.go index 166026a5..fc8204af 100644 --- a/local/compose/logs.go +++ b/pkg/compose/logs.go @@ -21,11 +21,11 @@ import ( "io" "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" - "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/stdcopy" "golang.org/x/sync/errgroup" + + "github.com/docker/compose-cli/pkg/utils" ) func (s *composeService) Logs(ctx context.Context, projectName string, consumer api.LogConsumer, options api.LogOptions) error { diff --git a/local/compose/ls.go b/pkg/compose/ls.go similarity index 100% rename from local/compose/ls.go rename to pkg/compose/ls.go diff --git a/local/compose/ls_test.go b/pkg/compose/ls_test.go similarity index 100% rename from local/compose/ls_test.go rename to pkg/compose/ls_test.go diff --git a/cli/metrics/definitions.go b/pkg/compose/metrics.go similarity index 99% rename from cli/metrics/definitions.go rename to pkg/compose/metrics.go index 228e42ae..2cdc927e 100644 --- a/cli/metrics/definitions.go +++ b/pkg/compose/metrics.go @@ -14,7 +14,7 @@ limitations under the License. */ -package metrics +package compose // FailureCategory sruct regrouping metrics failure status and specific exit code type FailureCategory struct { @@ -76,5 +76,4 @@ func ByExitCode(exitCode int) FailureCategory { default: return FailureCategory{MetricsStatus: FailureStatus, ExitCode: exitCode} } - } diff --git a/local/compose/pause.go b/pkg/compose/pause.go similarity index 98% rename from local/compose/pause.go rename to pkg/compose/pause.go index ea77e760..cf306113 100644 --- a/local/compose/pause.go +++ b/pkg/compose/pause.go @@ -22,8 +22,8 @@ import ( moby "github.com/docker/docker/api/types" "golang.org/x/sync/errgroup" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" ) func (s *composeService) Pause(ctx context.Context, project string, options api.PauseOptions) error { diff --git a/local/compose/port.go b/pkg/compose/port.go similarity index 100% rename from local/compose/port.go rename to pkg/compose/port.go diff --git a/local/compose/printer.go b/pkg/compose/printer.go similarity index 100% rename from local/compose/printer.go rename to pkg/compose/printer.go diff --git a/local/compose/ps.go b/pkg/compose/ps.go similarity index 100% rename from local/compose/ps.go rename to pkg/compose/ps.go diff --git a/local/compose/ps_test.go b/pkg/compose/ps_test.go similarity index 98% rename from local/compose/ps_test.go rename to pkg/compose/ps_test.go index 6e05abbb..ec389af0 100644 --- a/local/compose/ps_test.go +++ b/pkg/compose/ps_test.go @@ -26,8 +26,8 @@ import ( moby "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" - "github.com/docker/compose-cli/local/mocks" compose "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/mocks" ) func TestPs(t *testing.T) { diff --git a/local/compose/pull.go b/pkg/compose/pull.go similarity index 94% rename from local/compose/pull.go rename to pkg/compose/pull.go index 2651b562..50327010 100644 --- a/local/compose/pull.go +++ b/pkg/compose/pull.go @@ -32,9 +32,8 @@ import ( "github.com/docker/docker/registry" "golang.org/x/sync/errgroup" - "github.com/docker/compose-cli/api/progress" - "github.com/docker/compose-cli/cli/metrics" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" ) func (s *composeService) Pull(ctx context.Context, project *types.Project, opts api.PullOptions) error { @@ -125,7 +124,7 @@ func (s *composeService) pullServiceImage(ctx context.Context, service types.Ser Status: progress.Error, Text: "Error", }) - return metrics.WrapCategorisedComposeError(err, metrics.PullFailure) + return WrapCategorisedComposeError(err, PullFailure) } dec := json.NewDecoder(stream) @@ -135,10 +134,10 @@ func (s *composeService) pullServiceImage(ctx context.Context, service types.Ser if err == io.EOF { break } - return metrics.WrapCategorisedComposeError(err, metrics.PullFailure) + return WrapCategorisedComposeError(err, PullFailure) } if jm.Error != nil { - return metrics.WrapCategorisedComposeError(errors.New(jm.Error.Message), metrics.PullFailure) + return WrapCategorisedComposeError(errors.New(jm.Error.Message), PullFailure) } if !quietPull { toPullProgressEvent(service.Name, jm, w) diff --git a/local/compose/push.go b/pkg/compose/push.go similarity index 92% rename from local/compose/push.go rename to pkg/compose/push.go index 1e5b51da..3ed58686 100644 --- a/local/compose/push.go +++ b/pkg/compose/push.go @@ -26,16 +26,14 @@ import ( "github.com/compose-spec/compose-go/types" "github.com/distribution/distribution/v3/reference" "github.com/docker/buildx/driver" - cliconfig "github.com/docker/cli/cli/config" moby "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/registry" "github.com/pkg/errors" "golang.org/x/sync/errgroup" - "github.com/docker/compose-cli/api/config" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" ) func (s *composeService) Push(ctx context.Context, project *types.Project, options api.PushOptions) error { @@ -45,10 +43,6 @@ func (s *composeService) Push(ctx context.Context, project *types.Project, optio } func (s *composeService) push(ctx context.Context, project *types.Project, options api.PushOptions) error { - configFile, err := cliconfig.Load(config.Dir()) - if err != nil { - return err - } eg, ctx := errgroup.WithContext(ctx) info, err := s.apiClient.Info(ctx) @@ -71,7 +65,7 @@ func (s *composeService) push(ctx context.Context, project *types.Project, optio } service := service eg.Go(func() error { - err := s.pushServiceImage(ctx, service, info, configFile, w) + err := s.pushServiceImage(ctx, service, info, s.configFile, w) if err != nil { if !options.IgnoreFailures { return err diff --git a/local/compose/remove.go b/pkg/compose/remove.go similarity index 92% rename from local/compose/remove.go rename to pkg/compose/remove.go index 30255987..7c586a17 100644 --- a/local/compose/remove.go +++ b/pkg/compose/remove.go @@ -21,14 +21,13 @@ import ( "fmt" "strings" - "github.com/docker/compose-cli/api/progress" - status "github.com/docker/compose-cli/local/moby" - "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils/prompt" - "github.com/compose-spec/compose-go/types" + "github.com/docker/compose-cli/pkg/api" moby "github.com/docker/docker/api/types" "golang.org/x/sync/errgroup" + + "github.com/docker/compose-cli/pkg/progress" + "github.com/docker/compose-cli/pkg/prompt" ) func (s *composeService) Remove(ctx context.Context, project *types.Project, options api.RemoveOptions) error { @@ -43,7 +42,7 @@ func (s *composeService) Remove(ctx context.Context, project *types.Project, opt } stoppedContainers := containers.filter(func(c moby.Container) bool { - return c.State != status.ContainerRunning + return c.State != ContainerRunning }) var names []string diff --git a/local/compose/resize.go b/pkg/compose/resize.go similarity index 100% rename from local/compose/resize.go rename to pkg/compose/resize.go diff --git a/local/compose/restart.go b/pkg/compose/restart.go similarity index 94% rename from local/compose/restart.go rename to pkg/compose/restart.go index d760506c..241b7870 100644 --- a/local/compose/restart.go +++ b/pkg/compose/restart.go @@ -19,11 +19,11 @@ package compose import ( "context" - "github.com/docker/compose-cli/api/progress" - "github.com/docker/compose-cli/pkg/api" - "github.com/docker/compose-cli/utils" - "github.com/compose-spec/compose-go/types" + "github.com/docker/compose-cli/pkg/api" + + "github.com/docker/compose-cli/pkg/progress" + "github.com/docker/compose-cli/pkg/utils" ) func (s *composeService) Restart(ctx context.Context, project *types.Project, options api.RestartOptions) error { diff --git a/local/compose/run.go b/pkg/compose/run.go similarity index 100% rename from local/compose/run.go rename to pkg/compose/run.go diff --git a/local/compose/start.go b/pkg/compose/start.go similarity index 98% rename from local/compose/start.go rename to pkg/compose/start.go index 9461ad16..a882a1e7 100644 --- a/local/compose/start.go +++ b/pkg/compose/start.go @@ -24,8 +24,8 @@ import ( "github.com/pkg/errors" "golang.org/x/sync/errgroup" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" ) func (s *composeService) Start(ctx context.Context, project *types.Project, options api.StartOptions) error { diff --git a/local/compose/status.go b/pkg/compose/status.go similarity index 100% rename from local/compose/status.go rename to pkg/compose/status.go diff --git a/local/compose/stop.go b/pkg/compose/stop.go similarity index 97% rename from local/compose/stop.go rename to pkg/compose/stop.go index d0e68a72..056a1f29 100644 --- a/local/compose/stop.go +++ b/pkg/compose/stop.go @@ -19,8 +19,8 @@ package compose import ( "context" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" "github.com/compose-spec/compose-go/types" ) diff --git a/local/compose/stop_test.go b/pkg/compose/stop_test.go similarity index 97% rename from local/compose/stop_test.go rename to pkg/compose/stop_test.go index 92c6d3f9..c52b42ef 100644 --- a/local/compose/stop_test.go +++ b/pkg/compose/stop_test.go @@ -21,12 +21,11 @@ import ( "testing" "time" - moby "github.com/docker/docker/api/types" - - "github.com/docker/compose-cli/local/mocks" compose "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/mocks" "github.com/compose-spec/compose-go/types" + moby "github.com/docker/docker/api/types" "github.com/golang/mock/gomock" "gotest.tools/v3/assert" ) diff --git a/local/compose/testdata/docker-compose.yml b/pkg/compose/testdata/docker-compose.yml similarity index 100% rename from local/compose/testdata/docker-compose.yml rename to pkg/compose/testdata/docker-compose.yml diff --git a/local/compose/top.go b/pkg/compose/top.go similarity index 100% rename from local/compose/top.go rename to pkg/compose/top.go diff --git a/local/compose/up.go b/pkg/compose/up.go similarity index 98% rename from local/compose/up.go rename to pkg/compose/up.go index 6da29a90..8db486b1 100644 --- a/local/compose/up.go +++ b/pkg/compose/up.go @@ -23,8 +23,8 @@ import ( "os/signal" "syscall" - "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/pkg/api" + "github.com/docker/compose-cli/pkg/progress" "github.com/compose-spec/compose-go/types" "github.com/docker/cli/cli" diff --git a/local/mocks/mock_docker_api.go b/pkg/mocks/mock_docker_api.go similarity index 100% rename from local/mocks/mock_docker_api.go rename to pkg/mocks/mock_docker_api.go diff --git a/api/progress/event.go b/pkg/progress/event.go similarity index 100% rename from api/progress/event.go rename to pkg/progress/event.go diff --git a/api/progress/noop.go b/pkg/progress/noop.go similarity index 100% rename from api/progress/noop.go rename to pkg/progress/noop.go diff --git a/api/progress/plain.go b/pkg/progress/plain.go similarity index 100% rename from api/progress/plain.go rename to pkg/progress/plain.go diff --git a/api/progress/spinner.go b/pkg/progress/spinner.go similarity index 100% rename from api/progress/spinner.go rename to pkg/progress/spinner.go diff --git a/api/progress/tty.go b/pkg/progress/tty.go similarity index 99% rename from api/progress/tty.go rename to pkg/progress/tty.go index 0dd907a6..f0407254 100644 --- a/api/progress/tty.go +++ b/pkg/progress/tty.go @@ -25,7 +25,7 @@ import ( "sync" "time" - "github.com/docker/compose-cli/utils" + "github.com/docker/compose-cli/pkg/utils" "github.com/buger/goterm" "github.com/morikuni/aec" diff --git a/api/progress/tty_test.go b/pkg/progress/tty_test.go similarity index 100% rename from api/progress/tty_test.go rename to pkg/progress/tty_test.go diff --git a/api/progress/writer.go b/pkg/progress/writer.go similarity index 100% rename from api/progress/writer.go rename to pkg/progress/writer.go diff --git a/api/progress/writer_test.go b/pkg/progress/writer_test.go similarity index 100% rename from api/progress/writer_test.go rename to pkg/progress/writer_test.go diff --git a/utils/prompt/prompt.go b/pkg/prompt/prompt.go similarity index 96% rename from utils/prompt/prompt.go rename to pkg/prompt/prompt.go index f30fbce3..fd4142cd 100644 --- a/utils/prompt/prompt.go +++ b/pkg/prompt/prompt.go @@ -20,7 +20,7 @@ import ( "github.com/AlecAivazis/survey/v2" ) -//go:generate mockgen -destination=./prompt_mock.go -self_package "github.com/docker/compose-cli/prompt" -package=prompt . UI +//go:generate mockgen -destination=./prompt_mock.go -self_package "github.com/docker/compose-cli/pkg/prompt" -package=prompt . UI // UI - prompt user input type UI interface { diff --git a/utils/prompt/prompt_mock.go b/pkg/prompt/prompt_mock.go similarity index 97% rename from utils/prompt/prompt_mock.go rename to pkg/prompt/prompt_mock.go index 7e547177..9acee1dd 100644 --- a/utils/prompt/prompt_mock.go +++ b/pkg/prompt/prompt_mock.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Container: github.com/docker/compose-cli/prompt (interfaces: UI) +// Container: github.com/docker/compose-cli/pkg/prompt (interfaces: UI) // Package prompt is a generated GoMock package. package prompt diff --git a/utils/scan_suggest.go b/pkg/utils/scan_suggest.go similarity index 100% rename from utils/scan_suggest.go rename to pkg/utils/scan_suggest.go diff --git a/utils/stringutils.go b/pkg/utils/stringutils.go similarity index 100% rename from utils/stringutils.go rename to pkg/utils/stringutils.go diff --git a/utils/writer.go b/pkg/utils/writer.go similarity index 100% rename from utils/writer.go rename to pkg/utils/writer.go diff --git a/utils/writer_test.go b/pkg/utils/writer_test.go similarity index 100% rename from utils/writer_test.go rename to pkg/utils/writer_test.go