Skip to content

Commit aa56ab3

Browse files
authored
Merge pull request #11492 from jhrotko/use-listeners-for-file-metadata
Use listener for file metadata
2 parents 07bda59 + 16c8099 commit aa56ab3

File tree

17 files changed

+54
-37
lines changed

17 files changed

+54
-37
lines changed

cmd/compose/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func buildCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service)
136136
}
137137

138138
func runBuild(ctx context.Context, dockerCli command.Cli, backend api.Service, opts buildOptions, services []string) error {
139-
project, err := opts.ToProject(ctx, dockerCli, services, cli.WithResolvedPaths(true), cli.WithoutEnvironmentResolution)
139+
project, _, err := opts.ToProject(ctx, dockerCli, services, cli.WithResolvedPaths(true), cli.WithoutEnvironmentResolution)
140140
if err != nil {
141141
return err
142142
}

cmd/compose/completion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func noCompletion() validArgsFn {
3737
func completeServiceNames(dockerCli command.Cli, p *ProjectOptions) validArgsFn {
3838
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
3939
p.Offline = true
40-
project, err := p.ToProject(cmd.Context(), dockerCli, nil)
40+
project, _, err := p.ToProject(cmd.Context(), dockerCli, nil)
4141
if err != nil {
4242
return nil, cobra.ShellCompDirectiveNoFileComp
4343
}
@@ -72,7 +72,7 @@ func completeProjectNames(backend api.Service) func(cmd *cobra.Command, args []s
7272
func completeProfileNames(dockerCli command.Cli, p *ProjectOptions) validArgsFn {
7373
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
7474
p.Offline = true
75-
project, err := p.ToProject(cmd.Context(), dockerCli, nil)
75+
project, _, err := p.ToProject(cmd.Context(), dockerCli, nil)
7676
if err != nil {
7777
return nil, cobra.ShellCompDirectiveNoFileComp
7878
}

cmd/compose/compose.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/docker/cli/cli-plugins/manager"
3737
"github.com/docker/cli/cli/command"
3838
"github.com/docker/compose/v2/cmd/formatter"
39+
"github.com/docker/compose/v2/internal/tracing"
3940
"github.com/docker/compose/v2/pkg/api"
4041
"github.com/docker/compose/v2/pkg/compose"
4142
ui "github.com/docker/compose/v2/pkg/progress"
@@ -141,11 +142,13 @@ func (o *ProjectOptions) WithServices(dockerCli command.Cli, fn ProjectServicesF
141142
cli.WithDiscardEnvFile,
142143
}
143144

144-
project, err := o.ToProject(ctx, dockerCli, args, options...)
145+
project, metrics, err := o.ToProject(ctx, dockerCli, args, options...)
145146
if err != nil {
146147
return err
147148
}
148149

150+
ctx = context.WithValue(ctx, tracing.Metrics{}, metrics)
151+
149152
return fn(ctx, project, args)
150153
})
151154
}
@@ -166,7 +169,7 @@ func (o *ProjectOptions) projectOrName(ctx context.Context, dockerCli command.Cl
166169
name := o.ProjectName
167170
var project *types.Project
168171
if len(o.ConfigPaths) > 0 || o.ProjectName == "" {
169-
p, err := o.ToProject(ctx, dockerCli, services, cli.WithDiscardEnvFile)
172+
p, _, err := o.ToProject(ctx, dockerCli, services, cli.WithDiscardEnvFile)
170173
if err != nil {
171174
envProjectName := os.Getenv(ComposeProjectName)
172175
if envProjectName != "" {
@@ -190,14 +193,15 @@ func (o *ProjectOptions) toProjectName(ctx context.Context, dockerCli command.Cl
190193
return envProjectName, nil
191194
}
192195

193-
project, err := o.ToProject(ctx, dockerCli, nil)
196+
project, _, err := o.ToProject(ctx, dockerCli, nil)
194197
if err != nil {
195198
return "", err
196199
}
197200
return project.Name, nil
198201
}
199202

200-
func (o *ProjectOptions) ToProject(ctx context.Context, dockerCli command.Cli, services []string, po ...cli.ProjectOptionsFn) (*types.Project, error) {
203+
func (o *ProjectOptions) ToProject(ctx context.Context, dockerCli command.Cli, services []string, po ...cli.ProjectOptionsFn) (*types.Project, tracing.Metrics, error) {
204+
var metrics tracing.Metrics
201205
if !o.Offline {
202206
po = append(po, o.remoteLoaders(dockerCli)...)
203207
}
@@ -206,25 +210,30 @@ func (o *ProjectOptions) ToProject(ctx context.Context, dockerCli command.Cli, s
206210

207211
options, err := o.toProjectOptions(po...)
208212
if err != nil {
209-
return nil, compose.WrapComposeError(err)
213+
return nil, metrics, compose.WrapComposeError(err)
210214
}
211215

216+
options.WithListeners(func(event string, metadata map[string]any) {
217+
if event == "extends" {
218+
metrics.CountExtends++
219+
}
220+
})
212221
if o.Compatibility || utils.StringToBool(options.Environment[ComposeCompatibility]) {
213222
api.Separator = "_"
214223
}
215224

216225
project, err := cli.ProjectFromOptions(options)
217226
if err != nil {
218-
return nil, compose.WrapComposeError(err)
227+
return nil, metrics, compose.WrapComposeError(err)
219228
}
220229

221230
if project.Name == "" {
222-
return nil, errors.New("project name can't be empty. Use `--project-name` to set a valid name")
231+
return nil, metrics, errors.New("project name can't be empty. Use `--project-name` to set a valid name")
223232
}
224233

225234
project, err = project.WithServicesEnabled(services...)
226235
if err != nil {
227-
return nil, err
236+
return nil, metrics, err
228237
}
229238

230239
for name, s := range project.Services {
@@ -245,7 +254,7 @@ func (o *ProjectOptions) ToProject(ctx context.Context, dockerCli command.Cli, s
245254
project = project.WithoutUnnecessaryResources()
246255

247256
project, err = project.WithSelectedServices(services)
248-
return project, err
257+
return project, metrics, err
249258
}
250259

251260
func (o *ProjectOptions) remoteLoaders(dockerCli command.Cli) []cli.ProjectOptionsFn {

cmd/compose/config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/docker/cli/cli/command"
3030
"github.com/spf13/cobra"
3131

32+
"github.com/docker/compose/v2/internal/tracing"
3233
"github.com/docker/compose/v2/pkg/api"
3334
"github.com/docker/compose/v2/pkg/compose"
3435
)
@@ -50,7 +51,7 @@ type configOptions struct {
5051
noConsistency bool
5152
}
5253

53-
func (o *configOptions) ToProject(ctx context.Context, dockerCli command.Cli, services []string, po ...cli.ProjectOptionsFn) (*types.Project, error) {
54+
func (o *configOptions) ToProject(ctx context.Context, dockerCli command.Cli, services []string, po ...cli.ProjectOptionsFn) (*types.Project, tracing.Metrics, error) {
5455
po = append(po,
5556
cli.WithInterpolation(!o.noInterpolate),
5657
cli.WithResolvedPaths(!o.noResolvePath),
@@ -124,7 +125,7 @@ func configCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service
124125

125126
func runConfig(ctx context.Context, dockerCli command.Cli, backend api.Service, opts configOptions, services []string) error {
126127
var content []byte
127-
project, err := opts.ToProject(ctx, dockerCli, services)
128+
project, _, err := opts.ToProject(ctx, dockerCli, services)
128129
if err != nil {
129130
return err
130131
}
@@ -154,7 +155,7 @@ func runConfig(ctx context.Context, dockerCli command.Cli, backend api.Service,
154155
}
155156

156157
func runServices(ctx context.Context, dockerCli command.Cli, opts configOptions) error {
157-
project, err := opts.ToProject(ctx, dockerCli, nil, cli.WithoutEnvironmentResolution)
158+
project, _, err := opts.ToProject(ctx, dockerCli, nil, cli.WithoutEnvironmentResolution)
158159
if err != nil {
159160
return err
160161
}
@@ -166,7 +167,7 @@ func runServices(ctx context.Context, dockerCli command.Cli, opts configOptions)
166167
}
167168

168169
func runVolumes(ctx context.Context, dockerCli command.Cli, opts configOptions) error {
169-
project, err := opts.ToProject(ctx, dockerCli, nil, cli.WithoutEnvironmentResolution)
170+
project, _, err := opts.ToProject(ctx, dockerCli, nil, cli.WithoutEnvironmentResolution)
170171
if err != nil {
171172
return err
172173
}
@@ -181,7 +182,7 @@ func runHash(ctx context.Context, dockerCli command.Cli, opts configOptions) err
181182
if opts.hash != "*" {
182183
services = append(services, strings.Split(opts.hash, ",")...)
183184
}
184-
project, err := opts.ToProject(ctx, dockerCli, nil, cli.WithoutEnvironmentResolution)
185+
project, _, err := opts.ToProject(ctx, dockerCli, nil, cli.WithoutEnvironmentResolution)
185186
if err != nil {
186187
return err
187188
}
@@ -217,7 +218,7 @@ func runHash(ctx context.Context, dockerCli command.Cli, opts configOptions) err
217218

218219
func runProfiles(ctx context.Context, dockerCli command.Cli, opts configOptions, services []string) error {
219220
set := map[string]struct{}{}
220-
project, err := opts.ToProject(ctx, dockerCli, services, cli.WithoutEnvironmentResolution)
221+
project, _, err := opts.ToProject(ctx, dockerCli, services, cli.WithoutEnvironmentResolution)
221222
if err != nil {
222223
return err
223224
}
@@ -238,7 +239,7 @@ func runProfiles(ctx context.Context, dockerCli command.Cli, opts configOptions,
238239
}
239240

240241
func runConfigImages(ctx context.Context, dockerCli command.Cli, opts configOptions, services []string) error {
241-
project, err := opts.ToProject(ctx, dockerCli, services, cli.WithoutEnvironmentResolution)
242+
project, _, err := opts.ToProject(ctx, dockerCli, services, cli.WithoutEnvironmentResolution)
242243
if err != nil {
243244
return err
244245
}

cmd/compose/publish.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func publishCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Servic
5050
}
5151

5252
func runPublish(ctx context.Context, dockerCli command.Cli, backend api.Service, opts publishOptions, repository string) error {
53-
project, err := opts.ToProject(ctx, dockerCli, nil)
53+
project, _, err := opts.ToProject(ctx, dockerCli, nil)
5454
if err != nil {
5555
return err
5656
}

cmd/compose/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (opts pullOptions) apply(project *types.Project, services []string) (*types
9494
}
9595

9696
func runPull(ctx context.Context, dockerCli command.Cli, backend api.Service, opts pullOptions, services []string) error {
97-
project, err := opts.ToProject(ctx, dockerCli, services)
97+
project, _, err := opts.ToProject(ctx, dockerCli, services)
9898
if err != nil {
9999
return err
100100
}

cmd/compose/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func pushCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service)
5454
}
5555

5656
func runPush(ctx context.Context, dockerCli command.Cli, backend api.Service, opts pushOptions, services []string) error {
57-
project, err := opts.ToProject(ctx, dockerCli, services)
57+
project, _, err := opts.ToProject(ctx, dockerCli, services)
5858
if err != nil {
5959
return err
6060
}

cmd/compose/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *
156156
return nil
157157
}),
158158
RunE: Adapt(func(ctx context.Context, args []string) error {
159-
project, err := p.ToProject(ctx, dockerCli, []string{options.Service}, cgo.WithResolvedPaths(true), cgo.WithDiscardEnvFile)
159+
project, _, err := p.ToProject(ctx, dockerCli, []string{options.Service}, cgo.WithResolvedPaths(true), cgo.WithDiscardEnvFile)
160160
if err != nil {
161161
return err
162162
}

cmd/compose/scale.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func scaleCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service)
6161

6262
func runScale(ctx context.Context, dockerCli command.Cli, backend api.Service, opts scaleOptions, serviceReplicaTuples map[string]int) error {
6363
services := maps.Keys(serviceReplicaTuples)
64-
project, err := opts.ToProject(ctx, dockerCli, services)
64+
project, _, err := opts.ToProject(ctx, dockerCli, services)
6565
if err != nil {
6666
return err
6767
}

cmd/compose/viz.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func vizCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *
6565

6666
func runViz(ctx context.Context, dockerCli command.Cli, backend api.Service, opts *vizOptions) error {
6767
_, _ = fmt.Fprintln(os.Stderr, "viz command is EXPERIMENTAL")
68-
project, err := opts.ToProject(ctx, dockerCli, nil)
68+
project, _, err := opts.ToProject(ctx, dockerCli, nil)
6969
if err != nil {
7070
return err
7171
}

0 commit comments

Comments
 (0)