MCPcopy
hub / github.com/cloudflare/cloudflared / PipeCommandOutputToFile

Function PipeCommandOutputToFile

diagnostic/log_collector_utils.go:11–67  ·  view source on GitHub ↗
(command *exec.Cmd, outputHandle *os.File)

Source from the content-addressed store, hash-verified

9)
10
11func PipeCommandOutputToFile(command *exec.Cmd, outputHandle *os.File) (*LogInformation, error) {
12 stdoutReader, err := command.StdoutPipe()
13 if err != nil {
14 return nil, fmt.Errorf(
15 "error retrieving stdout from command '%s': %w",
16 command.String(),
17 err,
18 )
19 }
20
21 stderrReader, err := command.StderrPipe()
22 if err != nil {
23 return nil, fmt.Errorf(
24 "error retrieving stderr from command '%s': %w",
25 command.String(),
26 err,
27 )
28 }
29
30 if err := command.Start(); err != nil {
31 return nil, fmt.Errorf(
32 "error running command '%s': %w",
33 command.String(),
34 err,
35 )
36 }
37
38 _, err = io.Copy(outputHandle, stdoutReader)
39 if err != nil {
40 return nil, fmt.Errorf(
41 "error copying stdout from %s to file %s: %w",
42 command.String(),
43 outputHandle.Name(),
44 err,
45 )
46 }
47
48 _, err = io.Copy(outputHandle, stderrReader)
49 if err != nil {
50 return nil, fmt.Errorf(
51 "error copying stderr from %s to file %s: %w",
52 command.String(),
53 outputHandle.Name(),
54 err,
55 )
56 }
57
58 if err := command.Wait(); err != nil {
59 return nil, fmt.Errorf(
60 "error waiting from command '%s': %w",
61 command.String(),
62 err,
63 )
64 }
65
66 return NewLogInformation(outputHandle.Name(), true, false), nil
67}
68

Callers 3

CollectMethod · 0.85
CollectMethod · 0.85

Calls 6

NewLogInformationFunction · 0.85
ErrorfMethod · 0.80
WaitMethod · 0.80
StringMethod · 0.65
StartMethod · 0.65
NameMethod · 0.65

Tested by

no test coverage detected