2021-10-05 19:57:36 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func reportWriter() (io.Writer, func() error, error) {
|
|
|
|
nop := func() error { return nil }
|
|
|
|
path := strings.TrimSpace(appConfig.File)
|
2021-10-06 13:49:42 +00:00
|
|
|
|
2021-10-05 19:57:36 +00:00
|
|
|
switch len(path) {
|
|
|
|
case 0:
|
|
|
|
return os.Stdout, nop, nil
|
2021-10-06 13:49:42 +00:00
|
|
|
|
2021-10-05 19:57:36 +00:00
|
|
|
default:
|
|
|
|
reportFile, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
2021-10-06 13:49:42 +00:00
|
|
|
|
2021-10-05 19:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nop, fmt.Errorf("unable to create report file: %w", err)
|
|
|
|
}
|
2021-10-06 13:49:42 +00:00
|
|
|
|
2021-10-05 19:57:36 +00:00
|
|
|
return reportFile, func() error {
|
|
|
|
if !appConfig.Quiet {
|
|
|
|
fmt.Printf("Report written to %q\n", path)
|
|
|
|
}
|
2021-10-06 13:49:42 +00:00
|
|
|
|
2021-10-05 19:57:36 +00:00
|
|
|
return reportFile.Close()
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|