ComposeCmd executes docker-compose commands via shell. returns stdout, stderr, error/nil
(cmd *ComposeCmdOpts)
| 88 | // ComposeCmd executes docker-compose commands via shell. |
| 89 | // returns stdout, stderr, error/nil |
| 90 | func ComposeCmd(cmd *ComposeCmdOpts) (string, string, error) { |
| 91 | var arg []string |
| 92 | var stdout bytes.Buffer |
| 93 | var stderr string |
| 94 | |
| 95 | _, err := DownloadDockerComposeIfNeeded() |
| 96 | if err != nil { |
| 97 | return "", "", err |
| 98 | } |
| 99 | |
| 100 | if cmd.ProjectName != "" { |
| 101 | arg = append(arg, "-p", cmd.ProjectName) |
| 102 | } |
| 103 | |
| 104 | if cmd.ComposeYaml != nil { |
| 105 | // Read from stdin |
| 106 | arg = append(arg, "-f", "-") |
| 107 | } else { |
| 108 | for _, file := range cmd.ComposeFiles { |
| 109 | arg = append(arg, "-f", file) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | for _, profile := range cmd.Profiles { |
| 114 | arg = append(arg, "--profile", profile) |
| 115 | } |
| 116 | |
| 117 | arg = append(arg, cmd.Action...) |
| 118 | |
| 119 | path, err := globalconfig.GetDockerComposePath() |
| 120 | if err != nil { |
| 121 | return "", "", err |
| 122 | } |
| 123 | |
| 124 | ctx := context.Background() |
| 125 | if cmd.Timeout > 0 { |
| 126 | var cancel context.CancelFunc |
| 127 | ctx, cancel = context.WithTimeout(ctx, cmd.Timeout) |
| 128 | defer cancel() |
| 129 | } |
| 130 | proc := exec.CommandContext(ctx, path, arg...) |
| 131 | proc.Stdout = &stdout |
| 132 | if cmd.ComposeYaml != nil { |
| 133 | yamlBytes, err := cmd.ComposeYaml.MarshalYAML() |
| 134 | if err != nil { |
| 135 | return "", "", err |
| 136 | } |
| 137 | yamlBytes = util.EscapeDollarSign(yamlBytes) |
| 138 | proc.Stdin = strings.NewReader(string(yamlBytes)) |
| 139 | } else { |
| 140 | proc.Stdin = os.Stdin |
| 141 | } |
| 142 | proc.Env = append(os.Environ(), cmd.Env...) |
| 143 | |
| 144 | stderrPipe, err := proc.StderrPipe() |
| 145 | if err != nil { |
| 146 | return "", "", fmt.Errorf("failed to proc.StderrPipe(): %v", err) |
| 147 | } |