Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 9e1ec76

Browse files
authored
Merge pull request #1165 from docker/global-opts
Make `--file` and `--project-name` global compose options
2 parents fea9697 + effc126 commit 9e1ec76

File tree

18 files changed

+142
-120
lines changed

18 files changed

+142
-120
lines changed

cli/cmd/compose/build.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ import (
2626
)
2727

2828
type buildOptions struct {
29+
*projectOptions
2930
composeOptions
3031
}
3132

32-
func buildCommand() *cobra.Command {
33-
opts := buildOptions{}
33+
func buildCommand(p *projectOptions) *cobra.Command {
34+
opts := buildOptions{
35+
projectOptions: p,
36+
}
3437
buildCmd := &cobra.Command{
3538
Use: "build [SERVICE...]",
3639
Short: "Build or rebuild services",
3740
RunE: func(cmd *cobra.Command, args []string) error {
3841
return runBuild(cmd.Context(), opts, args)
3942
},
4043
}
41-
buildCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
42-
buildCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
43-
4444
return buildCmd
4545
}
4646

cli/cmd/compose/compose.go

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,22 @@ import (
2727
"github.com/docker/compose-cli/api/context/store"
2828
)
2929

30-
type composeOptions struct {
30+
type projectOptions struct {
3131
ProjectName string
32-
DomainName string
33-
WorkingDir string
3432
ConfigPaths []string
35-
Environment []string
33+
WorkingDir string
3634
EnvFile string
37-
Format string
38-
Detach bool
39-
Build bool
40-
Quiet bool
4135
}
4236

43-
func addComposeCommonFlags(f *pflag.FlagSet, opts *composeOptions) {
44-
f.StringVarP(&opts.ProjectName, "project-name", "p", "", "Project name")
45-
f.StringVar(&opts.Format, "format", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
46-
f.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
37+
func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
38+
f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
39+
f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
40+
f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.")
41+
f.StringVar(&o.WorkingDir, "workdir", "", "Specify an alternate working directory")
42+
// TODO make --project-directory an alias
4743
}
4844

49-
func (o *composeOptions) toProjectName() (string, error) {
45+
func (o *projectOptions) toProjectName() (string, error) {
5046
if o.ProjectName != "" {
5147
return o.ProjectName, nil
5248
}
@@ -58,7 +54,7 @@ func (o *composeOptions) toProjectName() (string, error) {
5854
return project.Name, nil
5955
}
6056

61-
func (o *composeOptions) toProject() (*types.Project, error) {
57+
func (o *projectOptions) toProject() (*types.Project, error) {
6258
options, err := o.toProjectOptions()
6359
if err != nil {
6460
return nil, err
@@ -71,18 +67,18 @@ func (o *composeOptions) toProject() (*types.Project, error) {
7167
return project, nil
7268
}
7369

74-
func (o *composeOptions) toProjectOptions() (*cli.ProjectOptions, error) {
70+
func (o *projectOptions) toProjectOptions() (*cli.ProjectOptions, error) {
7571
return cli.NewProjectOptions(o.ConfigPaths,
7672
cli.WithOsEnv,
7773
cli.WithEnvFile(o.EnvFile),
7874
cli.WithDotEnv,
79-
cli.WithEnv(o.Environment),
8075
cli.WithWorkingDirectory(o.WorkingDir),
8176
cli.WithName(o.ProjectName))
8277
}
8378

8479
// Command returns the compose command with its child commands
8580
func Command(contextType string) *cobra.Command {
81+
opts := projectOptions{}
8682
command := &cobra.Command{
8783
Short: "Docker Compose",
8884
Use: "compose",
@@ -95,23 +91,24 @@ func Command(contextType string) *cobra.Command {
9591
}
9692

9793
command.AddCommand(
98-
upCommand(contextType),
99-
downCommand(),
100-
psCommand(),
94+
upCommand(&opts, contextType),
95+
downCommand(&opts),
96+
psCommand(&opts),
10197
listCommand(),
102-
logsCommand(),
103-
convertCommand(),
104-
runCommand(),
98+
logsCommand(&opts),
99+
convertCommand(&opts),
100+
runCommand(&opts),
105101
)
106102

107103
if contextType == store.LocalContextType || contextType == store.DefaultContextType {
108104
command.AddCommand(
109-
buildCommand(),
110-
pushCommand(),
111-
pullCommand(),
105+
buildCommand(&opts),
106+
pushCommand(&opts),
107+
pullCommand(&opts),
112108
)
113109
}
114110
command.Flags().SetInterspersed(false)
111+
opts.addProjectFlags(command.PersistentFlags())
115112
return command
116113
}
117114

cli/cmd/compose/convert.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,29 @@ import (
2727
"github.com/docker/compose-cli/api/client"
2828
)
2929

30-
func convertCommand() *cobra.Command {
31-
opts := composeOptions{}
30+
type convertOptions struct {
31+
*projectOptions
32+
Format string
33+
}
34+
35+
func convertCommand(p *projectOptions) *cobra.Command {
36+
opts := convertOptions{
37+
projectOptions: p,
38+
}
3239
convertCmd := &cobra.Command{
3340
Use: "convert",
3441
Short: "Converts the compose file to a cloud format (default: cloudformation)",
3542
RunE: func(cmd *cobra.Command, args []string) error {
3643
return runConvert(cmd.Context(), opts)
3744
},
3845
}
39-
convertCmd.Flags().StringVarP(&opts.ProjectName, "project-name", "p", "", "Project name")
40-
convertCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
41-
convertCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
42-
convertCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
43-
convertCmd.Flags().StringVar(&opts.EnvFile, "env-file", "", "Specify an alternate environment file.")
44-
convertCmd.Flags().StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]")
46+
flags := convertCmd.Flags()
47+
flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]")
4548

4649
return convertCmd
4750
}
4851

49-
func runConvert(ctx context.Context, opts composeOptions) error {
52+
func runConvert(ctx context.Context, opts convertOptions) error {
5053
var json []byte
5154
c, err := client.NewWithDefaultLocalBackend(ctx)
5255
if err != nil {

cli/cmd/compose/down.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,25 @@ import (
2929
"github.com/docker/compose-cli/api/progress"
3030
)
3131

32-
func downCommand() *cobra.Command {
33-
opts := composeOptions{}
32+
type downOptions struct {
33+
*projectOptions
34+
}
35+
36+
func downCommand(p *projectOptions) *cobra.Command {
37+
opts := downOptions{
38+
projectOptions: p,
39+
}
3440
downCmd := &cobra.Command{
3541
Use: "down",
3642
Short: "Stop and remove containers, networks",
3743
RunE: func(cmd *cobra.Command, args []string) error {
3844
return runDown(cmd.Context(), opts)
3945
},
4046
}
41-
downCmd.Flags().StringVarP(&opts.ProjectName, "project-name", "p", "", "Project name")
42-
downCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
43-
downCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
44-
4547
return downCmd
4648
}
4749

48-
func runDown(ctx context.Context, opts composeOptions) error {
50+
func runDown(ctx context.Context, opts downOptions) error {
4951
c, err := client.NewWithDefaultLocalBackend(ctx)
5052
if err != nil {
5153
return err

cli/cmd/compose/list.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,31 @@ import (
3030
"github.com/docker/compose-cli/cli/formatter"
3131
)
3232

33+
type lsOptions struct {
34+
Format string
35+
Quiet bool
36+
}
37+
3338
func listCommand() *cobra.Command {
34-
opts := composeOptions{}
39+
opts := lsOptions{}
3540
lsCmd := &cobra.Command{
3641
Use: "ls",
3742
Short: "List running compose projects",
3843
RunE: func(cmd *cobra.Command, args []string) error {
3944
return runList(cmd.Context(), opts)
4045
},
4146
}
42-
addComposeCommonFlags(lsCmd.Flags(), &opts)
47+
lsCmd.Flags().StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json].")
48+
lsCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
4349
return lsCmd
4450
}
4551

46-
func runList(ctx context.Context, opts composeOptions) error {
52+
func runList(ctx context.Context, opts lsOptions) error {
4753
c, err := client.NewWithDefaultLocalBackend(ctx)
4854
if err != nil {
4955
return err
5056
}
51-
stackList, err := c.ComposeService().List(ctx, opts.ProjectName)
57+
stackList, err := c.ComposeService().List(ctx, "")
5258
if err != nil {
5359
return err
5460
}

cli/cmd/compose/logs.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,26 @@ import (
2727
"github.com/docker/compose-cli/cli/formatter"
2828
)
2929

30-
func logsCommand() *cobra.Command {
31-
opts := composeOptions{}
30+
type logsOptions struct {
31+
*projectOptions
32+
composeOptions
33+
}
34+
35+
func logsCommand(p *projectOptions) *cobra.Command {
36+
opts := logsOptions{
37+
projectOptions: p,
38+
}
3239
logsCmd := &cobra.Command{
3340
Use: "logs [service...]",
3441
Short: "View output from containers",
3542
RunE: func(cmd *cobra.Command, args []string) error {
3643
return runLogs(cmd.Context(), opts, args)
3744
},
3845
}
39-
logsCmd.Flags().StringVarP(&opts.ProjectName, "project-name", "p", "", "Project name")
40-
logsCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
41-
logsCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
42-
4346
return logsCmd
4447
}
4548

46-
func runLogs(ctx context.Context, opts composeOptions, services []string) error {
49+
func runLogs(ctx context.Context, opts logsOptions, services []string) error {
4750
c, err := client.NewWithDefaultLocalBackend(ctx)
4851
if err != nil {
4952
return err

cli/cmd/compose/ps.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,29 @@ import (
3030
"github.com/docker/compose-cli/cli/formatter"
3131
)
3232

33-
func psCommand() *cobra.Command {
34-
opts := composeOptions{}
33+
type psOptions struct {
34+
*projectOptions
35+
Format string
36+
Quiet bool
37+
}
38+
39+
func psCommand(p *projectOptions) *cobra.Command {
40+
opts := psOptions{
41+
projectOptions: p,
42+
}
3543
psCmd := &cobra.Command{
3644
Use: "ps",
3745
Short: "List containers",
3846
RunE: func(cmd *cobra.Command, args []string) error {
3947
return runPs(cmd.Context(), opts)
4048
},
4149
}
42-
psCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
43-
psCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
44-
addComposeCommonFlags(psCmd.Flags(), &opts)
50+
psCmd.Flags().StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json].")
51+
psCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
4552
return psCmd
4653
}
4754

48-
func runPs(ctx context.Context, opts composeOptions) error {
55+
func runPs(ctx context.Context, opts psOptions) error {
4956
c, err := client.NewWithDefaultLocalBackend(ctx)
5057
if err != nil {
5158
return err

cli/cmd/compose/pull.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,21 @@ import (
2626
)
2727

2828
type pullOptions struct {
29+
*projectOptions
2930
composeOptions
3031
}
3132

32-
func pullCommand() *cobra.Command {
33-
opts := pullOptions{}
33+
func pullCommand(p *projectOptions) *cobra.Command {
34+
opts := pullOptions{
35+
projectOptions: p,
36+
}
3437
pullCmd := &cobra.Command{
3538
Use: "pull [SERVICE...]",
3639
Short: "Pull service images",
3740
RunE: func(cmd *cobra.Command, args []string) error {
3841
return runPull(cmd.Context(), opts, args)
3942
},
4043
}
41-
42-
pullCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
43-
pullCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
44-
4544
return pullCmd
4645
}
4746

cli/cmd/compose/push.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,21 @@ import (
2626
)
2727

2828
type pushOptions struct {
29+
*projectOptions
2930
composeOptions
3031
}
3132

32-
func pushCommand() *cobra.Command {
33-
opts := pushOptions{}
33+
func pushCommand(p *projectOptions) *cobra.Command {
34+
opts := pushOptions{
35+
projectOptions: p,
36+
}
3437
pushCmd := &cobra.Command{
3538
Use: "push [SERVICE...]",
3639
Short: "Push service images",
3740
RunE: func(cmd *cobra.Command, args []string) error {
3841
return runPush(cmd.Context(), opts, args)
3942
},
4043
}
41-
42-
pushCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
43-
pushCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
44-
4544
return pushCmd
4645
}
4746

0 commit comments

Comments
 (0)