Add ps and logs to compose API

Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2020-08-05 16:32:51 +02:00
parent c6c341ef16
commit 46e58a28d0
6 changed files with 156 additions and 0 deletions

View File

@ -39,6 +39,7 @@ import (
"github.com/docker/api/context/cloud"
"github.com/docker/api/context/store"
"github.com/docker/api/errdefs"
ecstypes "github.com/docker/ecs-plugin/pkg/compose"
)
const (
@ -366,6 +367,14 @@ func (cs *aciComposeService) Down(ctx context.Context, opts cli.ProjectOptions)
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 {
loginService login.AzureLoginService
}

View File

@ -41,6 +41,8 @@ func Command() *cobra.Command {
command.AddCommand(
upCommand(),
downCommand(),
psCommand(),
logsCommand(),
)
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
}
printSection(os.Stdout, len(serviceList), 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 nil
}
func printSection(out io.Writer, len int, printer func(io.Writer), headers ...string) {
w := tabwriter.NewWriter(out, 20, 1, 3, ' ', 0)
fmt.Fprintln(w, strings.Join(headers, "\t"))
printer(w)
w.Flush()
}

View File

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

View File

@ -26,10 +26,12 @@ import (
"github.com/compose-spec/compose-go/cli"
"github.com/docker/api/context/cloud"
"github.com/docker/api/errdefs"
"github.com/docker/api/backend"
"github.com/docker/api/compose"
"github.com/docker/api/containers"
ecstypes "github.com/docker/ecs-plugin/pkg/compose"
)
type apiService struct {
@ -129,3 +131,10 @@ func (cs *composeService) Down(ctx context.Context, opts cli.ProjectOptions) err
fmt.Printf("Down command on project %q", prj.Name)
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
}