diff --git a/backend/backend.go b/backend/backend.go index e44b218b8..c060352c7 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -26,6 +26,7 @@ import ( "github.com/docker/api/compose" "github.com/docker/api/containers" "github.com/docker/api/context/cloud" + "github.com/docker/api/errdefs" "github.com/docker/api/secrets" ) @@ -87,7 +88,7 @@ func Get(ctx context.Context, backendType string) (Service, error) { } } - return nil, fmt.Errorf("backend not found for context %q", backendType) + return nil, errdefs.ErrNotFound } // GetCloudService returns the backend registered for a particular type, it returns diff --git a/cli/cmd/compose/compose.go b/cli/cmd/compose/compose.go index 1e2e52e47..49a7840cc 100644 --- a/cli/cmd/compose/compose.go +++ b/cli/cmd/compose/compose.go @@ -21,6 +21,7 @@ import ( "github.com/compose-spec/compose-go/cli" "github.com/pkg/errors" + "github.com/spf13/cobra" "github.com/docker/api/client" @@ -59,6 +60,7 @@ func Command() *cobra.Command { downCommand(), psCommand(), logsCommand(), + convertCommand(), ) return command @@ -73,16 +75,14 @@ func checkComposeSupport(ctx context.Context) error { } return nil } - currentContext := apicontext.CurrentContext(ctx) - s := store.ContextStore(ctx) - cc, err := s.Get(currentContext) - if err != nil { - return err - } - switch cc.Type() { - case store.AwsContextType: - return errors.New("use 'docker ecs compose' on context type " + cc.Type()) - default: + if errdefs.IsNotFoundError(err) { + currentContext := apicontext.CurrentContext(ctx) + s := store.ContextStore(ctx) + cc, err := s.Get(currentContext) + if err != nil { + return err + } return errors.Wrapf(errdefs.ErrNotImplemented, "compose command not supported on context type %q", cc.Type()) } + return err } diff --git a/cli/cmd/compose/convert.go b/cli/cmd/compose/convert.go new file mode 100644 index 000000000..3eb798028 --- /dev/null +++ b/cli/cmd/compose/convert.go @@ -0,0 +1,61 @@ +/* + Copyright 2020 Docker, Inc. + + 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" + + "github.com/compose-spec/compose-go/cli" + "github.com/docker/api/client" + "github.com/spf13/cobra" +) + +func convertCommand() *cobra.Command { + opts := composeOptions{} + convertCmd := &cobra.Command{ + Use: "convert", + Short: "Converts the compose file to a cloud format (default: cloudformation)", + RunE: func(cmd *cobra.Command, args []string) error { + options, err := opts.toProjectOptions() + if err != nil { + return err + } + return runConvert(cmd.Context(), options) + }, + } + convertCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name") + convertCmd.Flags().StringVar(&opts.WorkingDir, "workdir", ".", "Work dir") + convertCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files") + + return convertCmd +} + +func runConvert(ctx context.Context, opts *cli.ProjectOptions) error { + var json []byte + c, err := client.New(ctx) + if err != nil { + return err + } + json, err = c.ComposeService().Convert(ctx, opts) + if err != nil { + return err + } + + fmt.Println(string(json)) + return nil +} diff --git a/example/backend.go b/example/backend.go index 9ddd0573a..16a99d0ee 100644 --- a/example/backend.go +++ b/example/backend.go @@ -51,7 +51,6 @@ func (a *apiService) SecretsService() secrets.Service { return nil } - func init() { backend.Register("example", "example", service, cloud.NotImplementedCloudService) } diff --git a/go.sum b/go.sum index 9312e8a04..faee39fc1 100644 --- a/go.sum +++ b/go.sum @@ -436,7 +436,6 @@ golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 h1:AeiKBIuRw3UomYXSbLy0Mc2dDLfdtbT/IVn4keq83P0= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4= golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= @@ -448,7 +447,6 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -540,7 +538,6 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=