From cef7a2d4ce99d60777ec947da4a1bc89bb4601a0 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Tue, 29 Dec 2020 16:22:57 -0300 Subject: [PATCH] Refactor ProjectName This separate project name and service name for command `run` Signed-off-by: Ulysses Souza --- api/compose/api.go | 2 +- cli/cmd/compose/compose.go | 10 +++++----- cli/cmd/compose/convert.go | 2 +- cli/cmd/compose/down.go | 2 +- cli/cmd/compose/list.go | 2 +- cli/cmd/compose/logs.go | 5 +++-- cli/cmd/compose/run.go | 9 +++++---- cli/cmd/compose/up.go | 2 +- local/compose/run.go | 5 +++-- server/proxy/compose.go | 10 +++++----- 10 files changed, 26 insertions(+), 23 deletions(-) diff --git a/api/compose/api.go b/api/compose/api.go index c6faa5c3..6777594b 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -71,7 +71,7 @@ type ConvertOptions struct { // RunOptions options to execute compose run type RunOptions struct { - Name string + Service string Command []string Detach bool AutoRemove bool diff --git a/cli/cmd/compose/compose.go b/cli/cmd/compose/compose.go index 11705a46..ddd04385 100644 --- a/cli/cmd/compose/compose.go +++ b/cli/cmd/compose/compose.go @@ -28,7 +28,7 @@ import ( ) type composeOptions struct { - Name string + ProjectName string DomainName string WorkingDir string ConfigPaths []string @@ -40,14 +40,14 @@ type composeOptions struct { } func addComposeCommonFlags(f *pflag.FlagSet, opts *composeOptions) { - f.StringVarP(&opts.Name, "project-name", "p", "", "Project name") + f.StringVarP(&opts.ProjectName, "project-name", "p", "", "Project name") f.StringVar(&opts.Format, "format", "", "Format the output. Values: [pretty | json]. (Default: pretty)") f.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs") } func (o *composeOptions) toProjectName() (string, error) { - if o.Name != "" { - return o.Name, nil + if o.ProjectName != "" { + return o.ProjectName, nil } options, err := o.toProjectOptions() @@ -68,7 +68,7 @@ func (o *composeOptions) toProjectOptions() (*cli.ProjectOptions, error) { cli.WithDotEnv, cli.WithEnv(o.Environment), cli.WithWorkingDirectory(o.WorkingDir), - cli.WithName(o.Name)) + cli.WithName(o.ProjectName)) } // Command returns the compose command with its child commands diff --git a/cli/cmd/compose/convert.go b/cli/cmd/compose/convert.go index 5128155a..57a77bec 100644 --- a/cli/cmd/compose/convert.go +++ b/cli/cmd/compose/convert.go @@ -37,7 +37,7 @@ func convertCommand() *cobra.Command { return runConvert(cmd.Context(), opts) }, } - convertCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name") + convertCmd.Flags().StringVarP(&opts.ProjectName, "project-name", "p", "", "Project name") convertCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir") convertCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files") convertCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables") diff --git a/cli/cmd/compose/down.go b/cli/cmd/compose/down.go index 05715760..ac1b9355 100644 --- a/cli/cmd/compose/down.go +++ b/cli/cmd/compose/down.go @@ -36,7 +36,7 @@ func downCommand() *cobra.Command { return runDown(cmd.Context(), opts) }, } - downCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name") + downCmd.Flags().StringVarP(&opts.ProjectName, "project-name", "p", "", "Project name") downCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir") downCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files") diff --git a/cli/cmd/compose/list.go b/cli/cmd/compose/list.go index cb1c59ba..8b8d7c07 100644 --- a/cli/cmd/compose/list.go +++ b/cli/cmd/compose/list.go @@ -48,7 +48,7 @@ func runList(ctx context.Context, opts composeOptions) error { if err != nil { return err } - stackList, err := c.ComposeService().List(ctx, opts.Name) + stackList, err := c.ComposeService().List(ctx, opts.ProjectName) if err != nil { return err } diff --git a/cli/cmd/compose/logs.go b/cli/cmd/compose/logs.go index e75b0bdb..6993943b 100644 --- a/cli/cmd/compose/logs.go +++ b/cli/cmd/compose/logs.go @@ -20,10 +20,11 @@ import ( "context" "os" + "github.com/spf13/cobra" + "github.com/docker/compose-cli/api/client" "github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/formatter" - "github.com/spf13/cobra" ) func logsCommand() *cobra.Command { @@ -35,7 +36,7 @@ func logsCommand() *cobra.Command { return runLogs(cmd.Context(), opts, args) }, } - logsCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name") + logsCmd.Flags().StringVarP(&opts.ProjectName, "project-name", "p", "", "Project name") logsCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir") logsCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files") diff --git a/cli/cmd/compose/run.go b/cli/cmd/compose/run.go index 8b6f41c8..9f0140e7 100644 --- a/cli/cmd/compose/run.go +++ b/cli/cmd/compose/run.go @@ -30,6 +30,7 @@ import ( type runOptions struct { Name string + Service string Command []string WorkingDir string ConfigPaths []string @@ -48,7 +49,7 @@ func runCommand() *cobra.Command { if len(args) > 1 { opts.Command = args[1:] } - opts.Name = args[0] + opts.Service = args[0] return runRun(cmd.Context(), opts) }, } @@ -68,14 +69,14 @@ func runRun(ctx context.Context, opts runOptions) error { WorkingDir: opts.WorkingDir, Environment: opts.Environment, } - c, project, err := setup(ctx, projectOpts, []string{opts.Name}) + c, project, err := setup(ctx, projectOpts, []string{opts.Service}) if err != nil { return err } originalServices := project.Services _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", startDependencies(ctx, c, project, opts.Name) + return "", startDependencies(ctx, c, project, opts.Service) }) if err != nil { return err @@ -84,7 +85,7 @@ func runRun(ctx context.Context, opts runOptions) error { project.Services = originalServices // start container and attach to container streams runOpts := compose.RunOptions{ - Name: opts.Name, + Service: opts.Service, Command: opts.Command, Detach: opts.Detach, AutoRemove: opts.Remove, diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index a74e352b..b1b0b818 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -47,7 +47,7 @@ func upCommand(contextType string) *cobra.Command { } }, } - upCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name") + upCmd.Flags().StringVarP(&opts.ProjectName, "project-name", "p", "", "Project name") upCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir") upCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files") upCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables") diff --git a/local/compose/run.go b/local/compose/run.go index 0f5e2564..8c98bc3c 100644 --- a/local/compose/run.go +++ b/local/compose/run.go @@ -21,11 +21,12 @@ import ( "fmt" "github.com/compose-spec/compose-go/types" - "github.com/docker/compose-cli/api/compose" apitypes "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "golang.org/x/sync/errgroup" + "github.com/docker/compose-cli/api/compose" + moby "github.com/docker/docker/pkg/stringid" ) @@ -33,7 +34,7 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types. originalServices := project.Services var requestedService types.ServiceConfig for _, service := range originalServices { - if service.Name == opts.Name { + if service.Name == opts.Service { requestedService = service } } diff --git a/server/proxy/compose.go b/server/proxy/compose.go index b3b5f8ee..4fa76548 100644 --- a/server/proxy/compose.go +++ b/server/proxy/compose.go @@ -66,11 +66,11 @@ func (p *proxy) Services(ctx context.Context, request *composev1.ComposeServices /* FIXME need to create `docker service ls` command to re-introduce this feature for _, service := range services { response = append(response, &composev1.Service{ - Id: service.ID, - Name: service.Name, - Replicas: uint32(service.Replicas), - Desired: uint32(service.Desired), - Ports: service.Ports, + Id: service.ID, + ProjectName: service.ProjectName, + Replicas: uint32(service.Replicas), + Desired: uint32(service.Desired), + Ports: service.Ports, }) }*/ return &composev1.ComposeServicesResponse{Services: response}, nil