Merge pull request #435 from docker/extend_compose_api

Extend the compose API
This commit is contained in:
Anca Iordache 2020-08-07 11:53:32 +02:00 committed by GitHub
commit 7c8984beaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 159 additions and 0 deletions

View File

@ -39,6 +39,7 @@ import (
"github.com/docker/api/context/cloud" "github.com/docker/api/context/cloud"
"github.com/docker/api/context/store" "github.com/docker/api/context/store"
"github.com/docker/api/errdefs" "github.com/docker/api/errdefs"
ecstypes "github.com/docker/ecs-plugin/pkg/compose"
) )
const ( const (
@ -366,6 +367,14 @@ func (cs *aciComposeService) Down(ctx context.Context, opts cli.ProjectOptions)
return err return err
} }
func (cs *aciComposeService) Ps(ctx context.Context, opts cli.ProjectOptions) ([]ecstypes.ServiceStatus, error) {
return nil, errdefs.ErrNotImplemented
}
func (cs *aciComposeService) Logs(ctx context.Context, opts cli.ProjectOptions) error {
return errdefs.ErrNotImplemented
}
type aciCloudService struct { type aciCloudService struct {
loginService *login.AzureLoginService loginService *login.AzureLoginService
} }

View File

@ -41,6 +41,8 @@ func Command() *cobra.Command {
command.AddCommand( command.AddCommand(
upCommand(), upCommand(),
downCommand(), downCommand(),
psCommand(),
logsCommand(),
) )
return command return command

56
cli/cmd/compose/logs.go Normal file
View File

@ -0,0 +1,56 @@
/*
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"
"errors"
"github.com/compose-spec/compose-go/cli"
"github.com/spf13/cobra"
"github.com/docker/api/client"
)
func logsCommand() *cobra.Command {
opts := cli.ProjectOptions{}
logsCmd := &cobra.Command{
Use: "logs",
RunE: func(cmd *cobra.Command, args []string) error {
return runLogs(cmd.Context(), opts)
},
}
logsCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name")
logsCmd.Flags().StringVar(&opts.WorkingDir, "workdir", ".", "Work dir")
logsCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
return logsCmd
}
func runLogs(ctx context.Context, opts cli.ProjectOptions) error {
c, err := client.New(ctx)
if err != nil {
return err
}
composeService := c.ComposeService()
if composeService == nil {
return errors.New("compose not implemented in current context")
}
return composeService.Logs(ctx, opts)
}

77
cli/cmd/compose/ps.go Normal file
View File

@ -0,0 +1,77 @@
/*
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"
"errors"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"github.com/compose-spec/compose-go/cli"
"github.com/spf13/cobra"
"github.com/docker/api/client"
)
func psCommand() *cobra.Command {
opts := cli.ProjectOptions{}
psCmd := &cobra.Command{
Use: "ps",
RunE: func(cmd *cobra.Command, args []string) error {
return runPs(cmd.Context(), opts)
},
}
psCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name")
psCmd.Flags().StringVar(&opts.WorkingDir, "workdir", ".", "Work dir")
psCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
return psCmd
}
func runPs(ctx context.Context, opts cli.ProjectOptions) error {
c, err := client.New(ctx)
if err != nil {
return err
}
composeService := c.ComposeService()
if composeService == nil {
return errors.New("compose not implemented in current context")
}
serviceList, err := composeService.Ps(ctx, opts)
if err != nil {
return err
}
err = printSection(os.Stdout, func(w io.Writer) {
for _, service := range serviceList {
fmt.Fprintf(w, "%s\t%s\t%d/%d\t%s\n", service.ID, service.Name, service.Replicas, service.Desired, strings.Join(service.Ports, ", "))
}
}, "ID", "NAME", "REPLICAS", "PORTS")
return err
}
func printSection(out io.Writer, printer func(io.Writer), headers ...string) error {
w := tabwriter.NewWriter(out, 20, 1, 3, ' ', 0)
fmt.Fprintln(w, strings.Join(headers, "\t"))
printer(w)
return w.Flush()
}

View File

@ -20,6 +20,7 @@ import (
"context" "context"
"github.com/compose-spec/compose-go/cli" "github.com/compose-spec/compose-go/cli"
types "github.com/docker/ecs-plugin/pkg/compose"
) )
// Service manages a compose project // Service manages a compose project
@ -28,4 +29,8 @@ type Service interface {
Up(ctx context.Context, opts cli.ProjectOptions) error Up(ctx context.Context, opts cli.ProjectOptions) error
// Down executes the equivalent to a `compose down` // Down executes the equivalent to a `compose down`
Down(ctx context.Context, opts cli.ProjectOptions) error Down(ctx context.Context, opts cli.ProjectOptions) error
// Logs executes the equivalent to a `compose logs`
Logs(ctx context.Context, opts cli.ProjectOptions) error
// Ps executes the equivalent to a `compose ps`
Ps(ctx context.Context, opts cli.ProjectOptions) ([]types.ServiceStatus, error)
} }

View File

@ -26,10 +26,12 @@ import (
"github.com/compose-spec/compose-go/cli" "github.com/compose-spec/compose-go/cli"
"github.com/docker/api/context/cloud" "github.com/docker/api/context/cloud"
"github.com/docker/api/errdefs"
"github.com/docker/api/backend" "github.com/docker/api/backend"
"github.com/docker/api/compose" "github.com/docker/api/compose"
"github.com/docker/api/containers" "github.com/docker/api/containers"
ecstypes "github.com/docker/ecs-plugin/pkg/compose"
) )
type apiService struct { type apiService struct {
@ -129,3 +131,11 @@ func (cs *composeService) Down(ctx context.Context, opts cli.ProjectOptions) err
fmt.Printf("Down command on project %q", prj.Name) fmt.Printf("Down command on project %q", prj.Name)
return nil return nil
} }
func (cs *composeService) Ps(ctx context.Context, opts cli.ProjectOptions) ([]ecstypes.ServiceStatus, error) {
return nil, errdefs.ErrNotImplemented
}
func (cs *composeService) Logs(ctx context.Context, opts cli.ProjectOptions) error {
return errdefs.ErrNotImplemented
}