-
Notifications
You must be signed in to change notification settings - Fork 432
/
file.go
34 lines (31 loc) · 1.07 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package cmdutils
import (
"os"
"path/filepath"
)
// RunCommandOutputToFileFunc returns a func that runs the given Cmd pipes its stdout to the file specified.
// If the file does not exist on the host, it will be created
func RunCommandOutputToFileFunc(cmd Cmd, path string) func() error {
return func() error {
f, err := getOrCreateFileOnHost(path)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
// We intentionally do not output stderr to the output file
// Otherwise, the output file will contain errors that may be misleading to users
// For example, if a curl request failed first, and then succeeded, we do not want to
// populate the failures in the output file
return cmd.WithStdout(f).Run().Cause()
}
}
// getOrCreateFileOnHost is a helper to create a file at path even if the parent directory doesn't exist
// in which case it will be created with ModePerm
func getOrCreateFileOnHost(path string) (*os.File, error) {
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
return nil, err
}
return os.Create(path)
}