update compose-go and adopt NewProjectOptions and functional parameters

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-07-16 15:25:15 +02:00
parent 1a3c75fa29
commit 8582cb3928
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
9 changed files with 66 additions and 31 deletions

View File

@ -18,7 +18,7 @@ func ComposeCommand(dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "compose",
}
opts := &cli.ProjectOptions{}
opts := &composeOptions{}
AddFlags(opts, cmd.Flags())
cmd.AddCommand(
@ -42,12 +42,15 @@ func (o upOptions) LoadBalancerArn() *string {
return &o.loadBalancerArn
}
func ConvertCommand(dockerCli command.Cli, options *cli.ProjectOptions) *cobra.Command {
func ConvertCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "convert",
RunE: WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error {
opts := options.WithOsEnv()
project, err := cli.ProjectFromOptions(&opts)
opts, err := options.toProjectOptions()
if err != nil {
return err
}
project, err := cli.ProjectFromOptions(opts)
if err != nil {
return err
}
@ -68,24 +71,32 @@ func ConvertCommand(dockerCli command.Cli, options *cli.ProjectOptions) *cobra.C
return cmd
}
func UpCommand(dockerCli command.Cli, options *cli.ProjectOptions) *cobra.Command {
func UpCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
opts := upOptions{}
cmd := &cobra.Command{
Use: "up",
RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
return backend.Up(context.Background(), *options)
opts, err := options.toProjectOptions()
if err != nil {
return err
}
return backend.Up(context.Background(), opts)
}),
}
cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")
return cmd
}
func PsCommand(dockerCli command.Cli, options *cli.ProjectOptions) *cobra.Command {
func PsCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
opts := upOptions{}
cmd := &cobra.Command{
Use: "ps",
RunE: WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error {
status, err := backend.Ps(context.Background(), *options)
RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
opts, err := options.toProjectOptions()
if err != nil {
return err
}
status, err := backend.Ps(context.Background(), opts)
if err != nil {
return err
}
@ -105,23 +116,31 @@ type downOptions struct {
DeleteCluster bool
}
func DownCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.Command {
func DownCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
opts := downOptions{}
cmd := &cobra.Command{
Use: "down",
RunE: WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error {
return backend.Down(context.Background(), *projectOpts)
RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
opts, err := options.toProjectOptions()
if err != nil {
return err
}
return backend.Down(context.Background(), opts)
}),
}
cmd.Flags().BoolVar(&opts.DeleteCluster, "delete-cluster", false, "Delete cluster")
return cmd
}
func LogsCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.Command {
func LogsCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "logs [PROJECT NAME]",
RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
return backend.Logs(context.Background(), *projectOpts)
opts, err := options.toProjectOptions()
if err != nil {
return err
}
return backend.Logs(context.Background(), opts)
}),
}
return cmd

View File

@ -5,7 +5,24 @@ import (
"github.com/spf13/pflag"
)
func AddFlags(o *cli.ProjectOptions, flags *pflag.FlagSet) {
type composeOptions struct {
Name string
WorkingDir string
ConfigPaths []string
Environment []string
}
func AddFlags(o *composeOptions, flags *pflag.FlagSet) {
flags.StringArrayVarP(&o.ConfigPaths, "file", "f", nil, "Specify an alternate compose file")
flags.StringVarP(&o.Name, "project-name", "n", "", "Specify an alternate project name (default: directory name)")
flags.StringVarP(&o.WorkingDir, "workdir", "w", "", "Working directory")
flags.StringSliceVarP(&o.Environment, "environment", "e", []string{}, "Environment variables")
}
func (o *composeOptions) toProjectOptions() (*cli.ProjectOptions, error) {
return cli.NewProjectOptions(o.ConfigPaths,
cli.WithOsEnv,
cli.WithEnv(o.Environment),
cli.WithWorkingDirectory(o.WorkingDir),
cli.WithName(o.Name))
}

View File

@ -14,7 +14,7 @@ require (
github.com/bugsnag/panicwrap v1.2.0 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cloudflare/cfssl v1.4.1 // indirect
github.com/compose-spec/compose-go v0.0.0-20200710075715-6fcc35384ee1
github.com/compose-spec/compose-go v0.0.0-20200716130117-e87e4f7839e3
github.com/containerd/containerd v1.3.2 // indirect
github.com/containerd/continuity v0.0.0-20200413184840-d3ef23f19fbb // indirect
github.com/docker/cli v0.0.0-20200130152716-5d0cf8839492

View File

@ -66,6 +66,8 @@ github.com/compose-spec/compose-go v0.0.0-20200709084333-492a50989a5a h1:pIiSz5j
github.com/compose-spec/compose-go v0.0.0-20200709084333-492a50989a5a/go.mod h1:ArodJ6gsEB7iWKrbV3fSHZ08LlBvSVB0Oqg04fX86t4=
github.com/compose-spec/compose-go v0.0.0-20200710075715-6fcc35384ee1 h1:F+YIkKDMHdgZBacawhFY1P9RAIgO+6uv2te6hjsjzF0=
github.com/compose-spec/compose-go v0.0.0-20200710075715-6fcc35384ee1/go.mod h1:ArodJ6gsEB7iWKrbV3fSHZ08LlBvSVB0Oqg04fX86t4=
github.com/compose-spec/compose-go v0.0.0-20200716130117-e87e4f7839e3 h1:+ntlMTrEcScJjlnEOP8P1IIrusJaR93Eazr66YgUueA=
github.com/compose-spec/compose-go v0.0.0-20200716130117-e87e4f7839e3/go.mod h1:ArodJ6gsEB7iWKrbV3fSHZ08LlBvSVB0Oqg04fX86t4=
github.com/containerd/cgroups v0.0.0-20190919134610-bf292b21730f/go.mod h1:OApqhQ4XNSNC13gXIwDjhOQxjWa/NxkwZXJ1EvqT0ko=
github.com/containerd/console v0.0.0-20180822173158-c12b1e7919c1/go.mod h1:Tj/on1eG8kiEhd0+fhSDzsPAFESxzBBvdyEgyryXffw=
github.com/containerd/containerd v1.3.0-beta.2.0.20190828155532-0293cbd26c69/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA=

View File

@ -8,7 +8,7 @@ import (
"github.com/docker/ecs-plugin/pkg/console"
)
func (b *Backend) Down(ctx context.Context, options cli.ProjectOptions) error {
func (b *Backend) Down(ctx context.Context, options *cli.ProjectOptions) error {
name, err := b.projectName(options)
if err != nil {
return err
@ -27,10 +27,10 @@ func (b *Backend) Down(ctx context.Context, options cli.ProjectOptions) error {
return nil
}
func (b *Backend) projectName(options cli.ProjectOptions) (string, error) {
func (b *Backend) projectName(options *cli.ProjectOptions) (string, error) {
name := options.Name
if name == "" {
project, err := cli.ProjectFromOptions(&options)
project, err := cli.ProjectFromOptions(options)
if err != nil {
return "", err
}

View File

@ -3,16 +3,13 @@ package backend
import (
"context"
"fmt"
"regexp"
"strings"
"github.com/compose-spec/compose-go/cli"
"github.com/docker/ecs-plugin/pkg/compose"
)
var targetGroupLogicalName = regexp.MustCompile("(.*)(TCP|UDP)([0-9]+)TargetGroup")
func (b *Backend) Ps(ctx context.Context, options cli.ProjectOptions) ([]compose.ServiceStatus, error) {
func (b *Backend) Ps(ctx context.Context, options *cli.ProjectOptions) ([]compose.ServiceStatus, error) {
projectName, err := b.projectName(options)
if err != nil {
return nil, err

View File

@ -13,10 +13,10 @@ import (
"github.com/docker/ecs-plugin/pkg/console"
)
func (b *Backend) Logs(ctx context.Context, options cli.ProjectOptions) error {
func (b *Backend) Logs(ctx context.Context, options *cli.ProjectOptions) error {
name := options.Name
if name == "" {
project, err := cli.ProjectFromOptions(&options)
project, err := cli.ProjectFromOptions(options)
if err != nil {
return err
}

View File

@ -13,8 +13,8 @@ import (
"github.com/docker/ecs-plugin/pkg/console"
)
func (b *Backend) Up(ctx context.Context, options cli.ProjectOptions) error {
project, err := cli.ProjectFromOptions(&options)
func (b *Backend) Up(ctx context.Context, options *cli.ProjectOptions) error {
project, err := cli.ProjectFromOptions(options)
if err != nil {
return err
}

View File

@ -9,14 +9,14 @@ import (
)
type API interface {
Up(ctx context.Context, options cli.ProjectOptions) error
Down(ctx context.Context, options cli.ProjectOptions) error
Up(ctx context.Context, options *cli.ProjectOptions) error
Down(ctx context.Context, options *cli.ProjectOptions) error
CreateContextData(ctx context.Context, params map[string]string) (contextData interface{}, description string, err error)
Convert(project *types.Project) (*cloudformation.Template, error)
Logs(ctx context.Context, projectName cli.ProjectOptions) error
Ps(background context.Context, options cli.ProjectOptions) ([]ServiceStatus, error)
Logs(ctx context.Context, options *cli.ProjectOptions) error
Ps(background context.Context, options *cli.ProjectOptions) ([]ServiceStatus, error)
CreateSecret(ctx context.Context, secret Secret) (string, error)
InspectSecret(ctx context.Context, id string) (Secret, error)