ComposeWithStreams executes a docker-compose command but allows the caller to specify stdin/stdout/stderr
(cmd *ComposeCmdOpts, stdin io.Reader, stdout io.Writer, stderr io.Writer)
| 38 | // ComposeWithStreams executes a docker-compose command but allows the caller to specify |
| 39 | // stdin/stdout/stderr |
| 40 | func ComposeWithStreams(cmd *ComposeCmdOpts, stdin io.Reader, stdout io.Writer, stderr io.Writer) error { |
| 41 | defer util.TimeTrack()() |
| 42 | |
| 43 | var arg []string |
| 44 | |
| 45 | _, err := DownloadDockerComposeIfNeeded() |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | |
| 50 | if cmd.ProjectName != "" { |
| 51 | arg = append(arg, "-p", cmd.ProjectName) |
| 52 | } |
| 53 | |
| 54 | if cmd.ComposeYaml != nil { |
| 55 | // Read from stdin |
| 56 | arg = append(arg, "-f", "-") |
| 57 | } else { |
| 58 | for _, file := range cmd.ComposeFiles { |
| 59 | arg = append(arg, "-f", file) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | arg = append(arg, cmd.Action...) |
| 64 | |
| 65 | path, err := globalconfig.GetDockerComposePath() |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | proc := exec.Command(path, arg...) |
| 70 | proc.Stdout = stdout |
| 71 | proc.Stderr = stderr |
| 72 | if cmd.ComposeYaml != nil { |
| 73 | yamlBytes, err := cmd.ComposeYaml.MarshalYAML() |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | yamlBytes = util.EscapeDollarSign(yamlBytes) |
| 78 | proc.Stdin = strings.NewReader(string(yamlBytes)) |
| 79 | } else { |
| 80 | proc.Stdin = stdin |
| 81 | } |
| 82 | proc.Env = append(os.Environ(), cmd.Env...) |
| 83 | |
| 84 | err = proc.Run() |
| 85 | return err |
| 86 | } |
| 87 | |
| 88 | // ComposeCmd executes docker-compose commands via shell. |
| 89 | // returns stdout, stderr, error/nil |