introduce options struct in the API

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-12-16 16:16:39 +01:00
parent 855a879a6a
commit eda6a59379
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
14 changed files with 77 additions and 38 deletions

View File

@ -64,7 +64,7 @@ func (cs *aciComposeService) Start(ctx context.Context, project *types.Project,
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }
func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, detach bool) error { func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
logrus.Debugf("Up on project with name %q", project.Name) logrus.Debugf("Up on project with name %q", project.Name)
if err := autocreateFileshares(ctx, project); err != nil { if err := autocreateFileshares(ctx, project); err != nil {
@ -102,7 +102,7 @@ func (cs aciComposeService) warnKeepVolumeOnDown(ctx context.Context, projectNam
return nil return nil
} }
func (cs *aciComposeService) Down(ctx context.Context, projectName string, removeOrphans bool) error { func (cs *aciComposeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
logrus.Debugf("Down on projectName with name %q", projectName) logrus.Debugf("Down on projectName with name %q", projectName)
if err := cs.warnKeepVolumeOnDown(ctx, projectName); err != nil { if err := cs.warnKeepVolumeOnDown(ctx, projectName); err != nil {
@ -198,6 +198,6 @@ func (cs *aciComposeService) Logs(ctx context.Context, projectName string, consu
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }
func (cs *aciComposeService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) { func (cs *aciComposeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
return nil, errdefs.ErrNotImplemented return nil, errdefs.ErrNotImplemented
} }

View File

@ -48,11 +48,11 @@ func (c *composeService) Start(ctx context.Context, project *types.Project, cons
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }
func (c *composeService) Up(context.Context, *types.Project, bool) error { func (c *composeService) Up(context.Context, *types.Project, compose.UpOptions) error {
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }
func (c *composeService) Down(context.Context, string, bool) error { func (c *composeService) Down(context.Context, string, compose.DownOptions) error {
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }
@ -68,6 +68,6 @@ func (c *composeService) List(context.Context, string) ([]compose.Stack, error)
return nil, errdefs.ErrNotImplemented return nil, errdefs.ErrNotImplemented
} }
func (c *composeService) Convert(context.Context, *types.Project, string) ([]byte, error) { func (c *composeService) Convert(context.Context, *types.Project, compose.ConvertOptions) ([]byte, error) {
return nil, errdefs.ErrNotImplemented return nil, errdefs.ErrNotImplemented
} }

View File

@ -35,9 +35,9 @@ type Service interface {
// Start executes the equivalent to a `compose start` // Start executes the equivalent to a `compose start`
Start(ctx context.Context, project *types.Project, consumer LogConsumer) error Start(ctx context.Context, project *types.Project, consumer LogConsumer) error
// Up executes the equivalent to a `compose up` // Up executes the equivalent to a `compose up`
Up(ctx context.Context, project *types.Project, detach bool) error Up(ctx context.Context, project *types.Project, options UpOptions) error
// Down executes the equivalent to a `compose down` // Down executes the equivalent to a `compose down`
Down(ctx context.Context, projectName string, removeOrphans bool) error Down(ctx context.Context, projectName string, options DownOptions) error
// Logs executes the equivalent to a `compose logs` // Logs executes the equivalent to a `compose logs`
Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
// Ps executes the equivalent to a `compose ps` // Ps executes the equivalent to a `compose ps`
@ -45,7 +45,25 @@ type Service interface {
// List executes the equivalent to a `docker stack ls` // List executes the equivalent to a `docker stack ls`
List(ctx context.Context, projectName string) ([]Stack, error) List(ctx context.Context, projectName string) ([]Stack, error)
// Convert translate compose model into backend's native format // Convert translate compose model into backend's native format
Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
}
// UpOptions group options of the Up API
type UpOptions struct {
// Detach will create services and return immediately
Detach bool
}
// DownOptions group options of the Down API
type DownOptions struct {
// RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels
RemoveOrphans bool
}
// ConvertOptions group options of the Convert API
type ConvertOptions struct {
// Format define the output format used to dump converted application model (json|yaml)
Format string
} }
// PortPublisher hold status about published port // PortPublisher hold status about published port

View File

@ -20,6 +20,8 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/docker/compose-cli/api/compose"
"github.com/compose-spec/compose-go/cli" "github.com/compose-spec/compose-go/cli"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -61,7 +63,9 @@ func runConvert(ctx context.Context, opts composeOptions) error {
return err return err
} }
json, err = c.ComposeService().Convert(ctx, project, opts.Format) json, err = c.ComposeService().Convert(ctx, project, compose.ConvertOptions{
Format: opts.Format,
})
if err != nil { if err != nil {
return err return err
} }

View File

@ -19,6 +19,8 @@ package compose
import ( import (
"context" "context"
"github.com/docker/compose-cli/api/compose"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client" "github.com/docker/compose-cli/api/client"
@ -52,7 +54,9 @@ func runDown(ctx context.Context, opts composeOptions) error {
if err != nil { if err != nil {
return "", err return "", err
} }
return projectName, c.ComposeService().Down(ctx, projectName, false) return projectName, c.ComposeService().Down(ctx, projectName, compose.DownOptions{
RemoveOrphans: false,
})
}) })
return err return err
} }

View File

@ -68,7 +68,9 @@ func runUp(ctx context.Context, opts composeOptions, services []string) error {
} }
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
return "", c.ComposeService().Up(ctx, project, opts.Detach) return "", c.ComposeService().Up(ctx, project, compose.UpOptions{
Detach: opts.Detach,
})
}) })
return err return err
} }
@ -96,7 +98,7 @@ func runCreateStart(ctx context.Context, opts composeOptions, services []string)
fmt.Println("Gracefully stopping...") fmt.Println("Gracefully stopping...")
ctx = context.Background() ctx = context.Background()
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
return "", c.ComposeService().Down(ctx, project.Name, false) return "", c.ComposeService().Down(ctx, project.Name, compose.DownOptions{})
}) })
} }
return err return err

View File

@ -23,6 +23,8 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/docker/compose-cli/api/compose"
ecsapi "github.com/aws/aws-sdk-go/service/ecs" ecsapi "github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/elbv2" "github.com/aws/aws-sdk-go/service/elbv2"
cloudmapapi "github.com/aws/aws-sdk-go/service/servicediscovery" cloudmapapi "github.com/aws/aws-sdk-go/service/servicediscovery"
@ -37,13 +39,13 @@ import (
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
) )
func (b *ecsAPIService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) { func (b *ecsAPIService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
template, err := b.convert(ctx, project) template, err := b.convert(ctx, project)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return marshall(template, format) return marshall(template, options.Format)
} }
func (b *ecsAPIService) convert(ctx context.Context, project *types.Project) (*cloudformation.Template, error) { func (b *ecsAPIService) convert(ctx context.Context, project *types.Project) (*cloudformation.Template, error) {

View File

@ -19,10 +19,12 @@ package ecs
import ( import (
"context" "context"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/progress" "github.com/docker/compose-cli/progress"
) )
func (b *ecsAPIService) Down(ctx context.Context, projectName string, removeOrphans bool) error { func (b *ecsAPIService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
resources, err := b.aws.ListStackResources(ctx, projectName) resources, err := b.aws.ListStackResources(ctx, projectName)
if err != nil { if err != nil {
return err return err

View File

@ -55,11 +55,11 @@ func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, c
return e.compose.Start(ctx, project, consumer) return e.compose.Start(ctx, project, consumer)
} }
func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, detach bool) error { func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }
func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) { func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
enhanced, err := e.enhanceForLocalSimulation(project) enhanced, err := e.enhanceForLocalSimulation(project)
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,13 +73,13 @@ func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project,
"secrets": enhanced.Secrets, "secrets": enhanced.Secrets,
"configs": enhanced.Configs, "configs": enhanced.Configs,
} }
switch format { switch options.Format {
case "json": case "json":
return json.MarshalIndent(config, "", " ") return json.MarshalIndent(config, "", " ")
case "yaml": case "yaml":
return yaml.Marshal(config) return yaml.Marshal(config)
default: default:
return nil, fmt.Errorf("unsupported format %q", format) return nil, fmt.Errorf("unsupported format %q", options)
} }
} }
@ -147,11 +147,12 @@ func (e ecsLocalSimulation) enhanceForLocalSimulation(project *types.Project) (*
return project, nil return project, nil
} }
func (e ecsLocalSimulation) Down(ctx context.Context, projectName string, removeOrphans bool) error { func (e ecsLocalSimulation) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
return e.compose.Down(ctx, projectName, true) options.RemoveOrphans = true
return e.compose.Down(ctx, projectName, options)
} }
func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options componse.LogOptions) error { func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
return e.compose.Logs(ctx, projectName, consumer, options) return e.compose.Logs(ctx, projectName, consumer, options)
} }

View File

@ -49,14 +49,14 @@ func (b *ecsAPIService) Start(ctx context.Context, project *types.Project, consu
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }
func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach bool) error { func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
err := b.aws.CheckRequirements(ctx, b.Region) err := b.aws.CheckRequirements(ctx, b.Region)
if err != nil { if err != nil {
return err return err
} }
template, err := b.Convert(ctx, project, "yaml") template, err := b.Convert(ctx, project, compose.ConvertOptions{Format: "yaml"})
if err != nil { if err != nil {
return err return err
} }
@ -82,7 +82,7 @@ func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach b
return err return err
} }
} }
if detach { if options.Detach {
return nil return nil
} }
signalChan := make(chan os.Signal, 1) signalChan := make(chan os.Signal, 1)
@ -90,7 +90,7 @@ func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach b
go func() { go func() {
<-signalChan <-signalChan
fmt.Println("user interrupted deployment. Deleting stack...") fmt.Println("user interrupted deployment. Deleting stack...")
b.Down(ctx, project.Name, false) // nolint:errcheck b.Down(ctx, project.Name, compose.DownOptions{}) // nolint:errcheck
}() }()
err = b.WaitStackCompletion(ctx, project.Name, operation) err = b.WaitStackCompletion(ctx, project.Name, operation)

View File

@ -158,13 +158,13 @@ func (cs *composeService) Start(ctx context.Context, project *types.Project, con
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }
func (cs *composeService) Up(ctx context.Context, project *types.Project, detach bool) error { func (cs *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
fmt.Printf("Up command on project %q", project.Name) fmt.Printf("Up command on project %q", project.Name)
return nil return nil
} }
func (cs *composeService) Down(ctx context.Context, projectName string, removeOrphans bool) error { func (cs *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
fmt.Printf("Down command on project %q", projectName) fmt.Printf("Down command on project %q", projectName)
return nil return nil
} }
@ -179,6 +179,6 @@ func (cs *composeService) Logs(ctx context.Context, projectName string, consumer
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }
func (cs *composeService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) { func (cs *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
return nil, errdefs.ErrNotImplemented return nil, errdefs.ErrNotImplemented
} }

View File

@ -40,7 +40,7 @@ type composeService struct {
apiClient *client.Client apiClient *client.Client
} }
func (s *composeService) Up(ctx context.Context, project *types.Project, detach bool) error { func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
return errdefs2.ErrNotImplemented return errdefs2.ErrNotImplemented
} }
@ -54,13 +54,13 @@ func getContainerName(c moby.Container) string {
return c.Names[0][1:] return c.Names[0][1:]
} }
func (s *composeService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) { func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
switch format { switch options.Format {
case "json": case "json":
return json.MarshalIndent(project, "", " ") return json.MarshalIndent(project, "", " ")
case "yaml": case "yaml":
return yaml.Marshal(project) return yaml.Marshal(project)
default: default:
return nil, fmt.Errorf("unsupported format %q", format) return nil, fmt.Errorf("unsupported format %q", options)
} }
} }

View File

@ -21,6 +21,8 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/progress" "github.com/docker/compose-cli/progress"
"github.com/compose-spec/compose-go/cli" "github.com/compose-spec/compose-go/cli"
@ -30,7 +32,7 @@ import (
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
func (s *composeService) Down(ctx context.Context, projectName string, removeOrphans bool) error { func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
eg, _ := errgroup.WithContext(ctx) eg, _ := errgroup.WithContext(ctx)
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
@ -54,7 +56,7 @@ func (s *composeService) Down(ctx context.Context, projectName string, removeOrp
return err return err
}) })
if removeOrphans { if options.RemoveOrphans {
err := s.removeContainers(ctx, w, eg, containers) err := s.removeContainers(ctx, w, eg, containers)
if err != nil { if err != nil {
return err return err

View File

@ -19,6 +19,8 @@ package proxy
import ( import (
"context" "context"
"github.com/docker/compose-cli/api/compose"
"github.com/compose-spec/compose-go/cli" "github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
@ -30,7 +32,8 @@ func (p *proxy) Up(ctx context.Context, request *composev1.ComposeUpRequest) (*c
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &composev1.ComposeUpResponse{ProjectName: project.Name}, Client(ctx).ComposeService().Up(ctx, project, true) err = Client(ctx).ComposeService().Up(ctx, project, compose.UpOptions{Detach: true})
return &composev1.ComposeUpResponse{ProjectName: project.Name}, err
} }
func (p *proxy) Down(ctx context.Context, request *composev1.ComposeDownRequest) (*composev1.ComposeDownResponse, error) { func (p *proxy) Down(ctx context.Context, request *composev1.ComposeDownRequest) (*composev1.ComposeDownResponse, error) {
@ -42,7 +45,8 @@ func (p *proxy) Down(ctx context.Context, request *composev1.ComposeDownRequest)
} }
projectName = project.Name projectName = project.Name
} }
return &composev1.ComposeDownResponse{ProjectName: projectName}, Client(ctx).ComposeService().Down(ctx, projectName, false) err := Client(ctx).ComposeService().Down(ctx, projectName, compose.DownOptions{})
return &composev1.ComposeDownResponse{ProjectName: projectName}, err
} }
func (p *proxy) Services(ctx context.Context, request *composev1.ComposeServicesRequest) (*composev1.ComposeServicesResponse, error) { func (p *proxy) Services(ctx context.Context, request *composev1.ComposeServicesRequest) (*composev1.ComposeServicesResponse, error) {