Browse Source

introduce options struct in the API

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 5 years ago
parent
commit
eda6a59379

+ 3 - 3
aci/compose.go

@@ -64,7 +64,7 @@ func (cs *aciComposeService) Start(ctx context.Context, project *types.Project,
 	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)
 
 	if err := autocreateFileshares(ctx, project); err != nil {
@@ -102,7 +102,7 @@ func (cs aciComposeService) warnKeepVolumeOnDown(ctx context.Context, projectNam
 	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)
 
 	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
 }
 
-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
 }

+ 3 - 3
api/client/compose.go

@@ -48,11 +48,11 @@ func (c *composeService) Start(ctx context.Context, project *types.Project, cons
 	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
 }
 
-func (c *composeService) Down(context.Context, string, bool) error {
+func (c *composeService) Down(context.Context, string, compose.DownOptions) error {
 	return errdefs.ErrNotImplemented
 }
 
@@ -68,6 +68,6 @@ func (c *composeService) List(context.Context, string) ([]compose.Stack, error)
 	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
 }

+ 21 - 3
api/compose/api.go

@@ -35,9 +35,9 @@ type Service interface {
 	// Start executes the equivalent to a `compose start`
 	Start(ctx context.Context, project *types.Project, consumer LogConsumer) error
 	// 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(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(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
 	// 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(ctx context.Context, projectName string) ([]Stack, error)
 	// 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

+ 5 - 1
cli/cmd/compose/convert.go

@@ -20,6 +20,8 @@ import (
 	"context"
 	"fmt"
 
+	"github.com/docker/compose-cli/api/compose"
+
 	"github.com/compose-spec/compose-go/cli"
 	"github.com/spf13/cobra"
 
@@ -61,7 +63,9 @@ func runConvert(ctx context.Context, opts composeOptions) error {
 		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 {
 		return err
 	}

+ 5 - 1
cli/cmd/compose/down.go

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

+ 4 - 2
cli/cmd/compose/up.go

@@ -68,7 +68,9 @@ func runUp(ctx context.Context, opts composeOptions, services []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
 }
@@ -96,7 +98,7 @@ func runCreateStart(ctx context.Context, opts composeOptions, services []string)
 		fmt.Println("Gracefully stopping...")
 		ctx = context.Background()
 		_, 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

+ 4 - 2
ecs/cloudformation.go

@@ -23,6 +23,8 @@ import (
 	"regexp"
 	"strings"
 
+	"github.com/docker/compose-cli/api/compose"
+
 	ecsapi "github.com/aws/aws-sdk-go/service/ecs"
 	"github.com/aws/aws-sdk-go/service/elbv2"
 	cloudmapapi "github.com/aws/aws-sdk-go/service/servicediscovery"
@@ -37,13 +39,13 @@ import (
 	"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)
 	if err != nil {
 		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) {

+ 3 - 1
ecs/down.go

@@ -19,10 +19,12 @@ package ecs
 import (
 	"context"
 
+	"github.com/docker/compose-cli/api/compose"
+
 	"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)
 	if err != nil {
 		return err

+ 8 - 7
ecs/local/compose.go

@@ -55,11 +55,11 @@ func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, c
 	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
 }
 
-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)
 	if err != nil {
 		return nil, err
@@ -73,13 +73,13 @@ func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project,
 		"secrets":  enhanced.Secrets,
 		"configs":  enhanced.Configs,
 	}
-	switch format {
+	switch options.Format {
 	case "json":
 		return json.MarshalIndent(config, "", "  ")
 	case "yaml":
 		return yaml.Marshal(config)
 	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
 }
 
-func (e ecsLocalSimulation) Down(ctx context.Context, projectName string, removeOrphans bool) error {
-	return e.compose.Down(ctx, projectName, true)
+func (e ecsLocalSimulation) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
+	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)
 }
 

+ 4 - 4
ecs/up.go

@@ -49,14 +49,14 @@ func (b *ecsAPIService) Start(ctx context.Context, project *types.Project, consu
 	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)
 	if err != nil {
 		return err
 	}
 
-	template, err := b.Convert(ctx, project, "yaml")
+	template, err := b.Convert(ctx, project, compose.ConvertOptions{Format: "yaml"})
 	if err != nil {
 		return err
 	}
@@ -82,7 +82,7 @@ func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach b
 			return err
 		}
 	}
-	if detach {
+	if options.Detach {
 		return nil
 	}
 	signalChan := make(chan os.Signal, 1)
@@ -90,7 +90,7 @@ func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach b
 	go func() {
 		<-signalChan
 		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)

+ 3 - 3
example/backend.go

@@ -158,13 +158,13 @@ func (cs *composeService) Start(ctx context.Context, project *types.Project, con
 	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)
 	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)
 	return nil
 }
@@ -179,6 +179,6 @@ func (cs *composeService) Logs(ctx context.Context, projectName string, consumer
 	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
 }

+ 4 - 4
local/compose/compose.go

@@ -40,7 +40,7 @@ type composeService struct {
 	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
 }
 
@@ -54,13 +54,13 @@ func getContainerName(c moby.Container) string {
 	return c.Names[0][1:]
 }
 
-func (s *composeService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) {
-	switch format {
+func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
+	switch options.Format {
 	case "json":
 		return json.MarshalIndent(project, "", "  ")
 	case "yaml":
 		return yaml.Marshal(project)
 	default:
-		return nil, fmt.Errorf("unsupported format %q", format)
+		return nil, fmt.Errorf("unsupported format %q", options)
 	}
 }

+ 4 - 2
local/compose/down.go

@@ -21,6 +21,8 @@ import (
 	"path/filepath"
 	"strings"
 
+	"github.com/docker/compose-cli/api/compose"
+
 	"github.com/docker/compose-cli/progress"
 
 	"github.com/compose-spec/compose-go/cli"
@@ -30,7 +32,7 @@ import (
 	"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)
 	w := progress.ContextWriter(ctx)
 
@@ -54,7 +56,7 @@ func (s *composeService) Down(ctx context.Context, projectName string, removeOrp
 		return err
 	})
 
-	if removeOrphans {
+	if options.RemoveOrphans {
 		err := s.removeContainers(ctx, w, eg, containers)
 		if err != nil {
 			return err

+ 6 - 2
server/proxy/compose.go

@@ -19,6 +19,8 @@ package proxy
 import (
 	"context"
 
+	"github.com/docker/compose-cli/api/compose"
+
 	"github.com/compose-spec/compose-go/cli"
 	"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 {
 		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) {
@@ -42,7 +45,8 @@ func (p *proxy) Down(ctx context.Context, request *composev1.ComposeDownRequest)
 		}
 		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) {