MCPcopy Index your code
hub / github.com/ddev/ddev / ComposeWithStreams

Function ComposeWithStreams

pkg/dockerutil/docker_compose.go:40–86  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

38// ComposeWithStreams executes a docker-compose command but allows the caller to specify
39// stdin/stdout/stderr
40func 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

Callers 4

ExecMethod · 0.92
ExecWithTtyMethod · 0.92
TestComposeWithStreamsFunction · 0.92
PullImagesFunction · 0.85

Calls 5

TimeTrackFunction · 0.92
GetDockerComposePathFunction · 0.92
EscapeDollarSignFunction · 0.92
CommandMethod · 0.80

Tested by 1

TestComposeWithStreamsFunction · 0.74