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

Commit b1f864a

Browse files
committed
Introduce --profile option
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 6215445 commit b1f864a

File tree

13 files changed

+55
-100
lines changed

13 files changed

+55
-100
lines changed

cli/cmd/compose/build.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,12 @@ func runBuild(ctx context.Context, opts buildOptions, services []string) error {
5050
return err
5151
}
5252

53-
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
54-
project, err := opts.toProject()
55-
if err != nil {
56-
return "", err
57-
}
53+
project, err := opts.toProject(services)
54+
if err != nil {
55+
return err
56+
}
5857

59-
err = filter(project, services)
60-
if err != nil {
61-
return "", err
62-
}
58+
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
6359
return "", c.ComposeService().Build(ctx, project)
6460
})
6561
return err

cli/cmd/compose/compose.go

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ import (
2929

3030
type projectOptions struct {
3131
ProjectName string
32+
Profiles []string
3233
ConfigPaths []string
3334
WorkingDir string
3435
EnvFile string
3536
}
3637

3738
func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
39+
f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable")
3840
f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
3941
f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
4042
f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.")
@@ -47,14 +49,14 @@ func (o *projectOptions) toProjectName() (string, error) {
4749
return o.ProjectName, nil
4850
}
4951

50-
project, err := o.toProject()
52+
project, err := o.toProject(nil)
5153
if err != nil {
5254
return "", err
5355
}
5456
return project.Name, nil
5557
}
5658

57-
func (o *projectOptions) toProject() (*types.Project, error) {
59+
func (o *projectOptions) toProject(services []string) (*types.Project, error) {
5860
options, err := o.toProjectOptions()
5961
if err != nil {
6062
return nil, err
@@ -64,7 +66,19 @@ func (o *projectOptions) toProject() (*types.Project, error) {
6466
if err != nil {
6567
return nil, err
6668
}
67-
return project, nil
69+
70+
if len(services) != 0 {
71+
s, err := project.GetServices(services)
72+
if err != nil {
73+
return nil, err
74+
}
75+
o.Profiles = append(o.Profiles, s.GetProfiles()...)
76+
}
77+
78+
project.ApplyProfiles(o.Profiles)
79+
80+
err = project.ForServices(services)
81+
return project, err
6882
}
6983

7084
func (o *projectOptions) toProjectOptions() (*cli.ProjectOptions, error) {
@@ -114,40 +128,3 @@ func Command(contextType string) *cobra.Command {
114128
opts.addProjectFlags(command.PersistentFlags())
115129
return command
116130
}
117-
118-
//
119-
func filter(project *types.Project, services []string) error {
120-
if len(services) == 0 {
121-
// All services
122-
return nil
123-
}
124-
125-
names := map[string]bool{}
126-
err := addServiceNames(project, services, names)
127-
if err != nil {
128-
return err
129-
}
130-
var filtered types.Services
131-
for _, s := range project.Services {
132-
if _, ok := names[s.Name]; ok {
133-
filtered = append(filtered, s)
134-
}
135-
}
136-
project.Services = filtered
137-
return nil
138-
}
139-
140-
func addServiceNames(project *types.Project, services []string, names map[string]bool) error {
141-
for _, name := range services {
142-
names[name] = true
143-
service, err := project.GetService(name)
144-
if err != nil {
145-
return err
146-
}
147-
err = addServiceNames(project, service.GetDependencies(), names)
148-
if err != nil {
149-
return err
150-
}
151-
}
152-
return nil
153-
}

cli/cmd/compose/compose_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
func TestFilterServices(t *testing.T) {
27-
p := types.Project{
27+
p := &types.Project{
2828
Services: []types.ServiceConfig{
2929
{
3030
Name: "foo",
@@ -42,7 +42,7 @@ func TestFilterServices(t *testing.T) {
4242
},
4343
},
4444
}
45-
err := filter(&p, []string{"bar"})
45+
err := p.ForServices([]string{"bar"})
4646
assert.NilError(t, err)
4747

4848
assert.Equal(t, len(p.Services), 2)

cli/cmd/compose/convert.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ func convertCommand(p *projectOptions) *cobra.Command {
3737
projectOptions: p,
3838
}
3939
convertCmd := &cobra.Command{
40-
Use: "convert",
41-
Short: "Converts the compose file to a cloud format (default: cloudformation)",
40+
Aliases: []string{"config"},
41+
Use: "convert SERVICES",
42+
Short: "Converts the compose file to platform's canonical format",
4243
RunE: func(cmd *cobra.Command, args []string) error {
43-
return runConvert(cmd.Context(), opts)
44+
return runConvert(cmd.Context(), opts, args)
4445
},
4546
}
4647
flags := convertCmd.Flags()
@@ -49,14 +50,14 @@ func convertCommand(p *projectOptions) *cobra.Command {
4950
return convertCmd
5051
}
5152

52-
func runConvert(ctx context.Context, opts convertOptions) error {
53+
func runConvert(ctx context.Context, opts convertOptions, services []string) error {
5354
var json []byte
5455
c, err := client.NewWithDefaultLocalBackend(ctx)
5556
if err != nil {
5657
return err
5758
}
5859

59-
project, err := opts.toProject()
60+
project, err := opts.toProject(services)
6061
if err != nil {
6162
return err
6263
}

cli/cmd/compose/down.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func runDown(ctx context.Context, opts downOptions) error {
5757
name := opts.ProjectName
5858
var project *types.Project
5959
if opts.ProjectName == "" {
60-
p, err := opts.toProject()
60+
p, err := opts.toProject(nil)
6161
if err != nil {
6262
return "", err
6363
}

cli/cmd/compose/pull.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,12 @@ func runPull(ctx context.Context, opts pullOptions, services []string) error {
5050
return err
5151
}
5252

53-
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
54-
project, err := opts.toProject()
55-
if err != nil {
56-
return "", err
57-
}
53+
project, err := opts.toProject(services)
54+
if err != nil {
55+
return err
56+
}
5857

59-
err = filter(project, services)
60-
if err != nil {
61-
return "", err
62-
}
58+
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
6359
return "", c.ComposeService().Pull(ctx, project)
6460
})
6561
return err

cli/cmd/compose/push.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,12 @@ func runPush(ctx context.Context, opts pushOptions, services []string) error {
5050
return err
5151
}
5252

53-
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
54-
project, err := opts.toProject()
55-
if err != nil {
56-
return "", err
57-
}
53+
project, err := opts.toProject(services)
54+
if err != nil {
55+
return err
56+
}
5857

59-
err = filter(project, services)
60-
if err != nil {
61-
return "", err
62-
}
58+
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
6359
return "", c.ComposeService().Push(ctx, project)
6460
})
6561
return err

cli/cmd/compose/start.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ func runStart(ctx context.Context, opts startOptions, services []string) error {
5454
return err
5555
}
5656

57-
project, err := opts.toProject()
58-
if err != nil {
59-
return err
60-
}
61-
62-
err = filter(project, services)
57+
project, err := opts.toProject(services)
6358
if err != nil {
6459
return err
6560
}

cli/cmd/compose/stop.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,12 @@ func runStop(ctx context.Context, opts stopOptions, services []string) error {
4949
return err
5050
}
5151

52-
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
53-
project, err := opts.toProject()
54-
if err != nil {
55-
return "", err
56-
}
52+
project, err := opts.toProject(services)
53+
if err != nil {
54+
return err
55+
}
5756

58-
err = filter(project, services)
59-
if err != nil {
60-
return "", err
61-
}
57+
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
6258
return "", c.ComposeService().Stop(ctx, project)
6359
})
6460
return err

cli/cmd/compose/up.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,11 @@ func setup(ctx context.Context, opts composeOptions, services []string) (*client
161161
return nil, nil, err
162162
}
163163

164-
project, err := opts.toProject()
164+
project, err := opts.toProject(services)
165165
if err != nil {
166166
return nil, nil, err
167167
}
168+
168169
if opts.DomainName != "" {
169170
// arbitrarily set the domain name on the first service ; ACI backend will expose the entire project
170171
project.Services[0].DomainName = opts.DomainName
@@ -175,9 +176,5 @@ func setup(ctx context.Context, opts composeOptions, services []string) (*client
175176
}
176177
}
177178

178-
err = filter(project, services)
179-
if err != nil {
180-
return nil, nil, err
181-
}
182179
return c, project, nil
183180
}

0 commit comments

Comments
 (0)