|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/compose-spec/compose-go/types" |
| 24 | + "github.com/spf13/cobra" |
| 25 | + |
| 26 | + "github.com/docker/compose-cli/api/client" |
| 27 | + "github.com/docker/compose-cli/api/compose" |
| 28 | + "github.com/docker/compose-cli/progress" |
| 29 | +) |
| 30 | + |
| 31 | +type runOptions struct { |
| 32 | + Name string |
| 33 | + Command []string |
| 34 | + WorkingDir string |
| 35 | + ConfigPaths []string |
| 36 | + Environment []string |
| 37 | + Detach bool |
| 38 | + Remove bool |
| 39 | +} |
| 40 | + |
| 41 | +func runCommand() *cobra.Command { |
| 42 | + opts := runOptions{} |
| 43 | + runCmd := &cobra.Command{ |
| 44 | + Use: "run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...] SERVICE [COMMAND] [ARGS...]", |
| 45 | + Short: "Run a one-off command on a service.", |
| 46 | + Args: cobra.MinimumNArgs(1), |
| 47 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 48 | + if len(args) > 1 { |
| 49 | + opts.Command = args[1:] |
| 50 | + } |
| 51 | + opts.Name = args[0] |
| 52 | + return runRun(cmd.Context(), opts) |
| 53 | + }, |
| 54 | + } |
| 55 | + runCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir") |
| 56 | + runCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files") |
| 57 | + runCmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID") |
| 58 | + runCmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables") |
| 59 | + runCmd.Flags().BoolVar(&opts.Remove, "rm", false, "Automatically remove the container when it exits") |
| 60 | + |
| 61 | + runCmd.Flags().SetInterspersed(false) |
| 62 | + return runCmd |
| 63 | +} |
| 64 | + |
| 65 | +func runRun(ctx context.Context, opts runOptions) error { |
| 66 | + projectOpts := composeOptions{ |
| 67 | + ConfigPaths: opts.ConfigPaths, |
| 68 | + WorkingDir: opts.WorkingDir, |
| 69 | + Environment: opts.Environment, |
| 70 | + } |
| 71 | + c, project, err := setup(ctx, projectOpts, []string{opts.Name}) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + originalServices := project.Services |
| 77 | + _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { |
| 78 | + return "", startDependencies(ctx, c, project, opts.Name) |
| 79 | + }) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + project.Services = originalServices |
| 85 | + // start container and attach to container streams |
| 86 | + runOpts := compose.RunOptions{ |
| 87 | + Name: opts.Name, |
| 88 | + Command: opts.Command, |
| 89 | + Detach: opts.Detach, |
| 90 | + AutoRemove: opts.Remove, |
| 91 | + Writer: os.Stdout, |
| 92 | + Reader: os.Stdin, |
| 93 | + } |
| 94 | + return c.ComposeService().RunOneOffContainer(ctx, project, runOpts) |
| 95 | +} |
| 96 | + |
| 97 | +func startDependencies(ctx context.Context, c *client.Client, project *types.Project, requestedService string) error { |
| 98 | + originalServices := project.Services |
| 99 | + dependencies := types.Services{} |
| 100 | + for _, service := range originalServices { |
| 101 | + if service.Name != requestedService { |
| 102 | + dependencies = append(dependencies, service) |
| 103 | + } |
| 104 | + } |
| 105 | + project.Services = dependencies |
| 106 | + if err := c.ComposeService().Create(ctx, project); err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + if err := c.ComposeService().Start(ctx, project, nil); err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + return nil |
| 113 | + |
| 114 | +} |
0 commit comments