generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
printer.go
94 lines (80 loc) · 2.38 KB
/
printer.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package printing
import (
"fmt"
"github.com/broadinstitute/thelma/internal/thelma/cli/printing/format"
"github.com/broadinstitute/thelma/internal/thelma/utils"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/spf13/pflag"
"io"
"os"
)
const stdoutFlagVal = "STDOUT"
var flagNames = struct {
outputFile string
outputFormat string
}{
outputFile: "output-file",
outputFormat: "output-format",
}
type options struct {
outputFile string
outputFormat string
}
// Printer is a tool for printing formatted output from thelma cli commands.
type Printer interface {
// AddFlags adds --output-format and --output-file flags to a cobra FlagSet
AddFlags(flags *pflag.FlagSet)
// VerifyFlags verifies flags have correct values
VerifyFlags() error
// PrintOutput prints output in the user-supplied format
PrintOutput(output interface{}, stdout io.Writer) error
}
func NewPrinter() Printer {
return &printer{}
}
// Implements printer interface
type printer struct {
options options
}
func (p *printer) AddFlags(flags *pflag.FlagSet) {
defaultFormat := format.PrettyYaml
if !utils.Interactive() {
// use plain YAML if this is not an interactive terminal
defaultFormat = format.Yaml
}
flags.StringVar(&p.options.outputFormat, flagNames.outputFormat, defaultFormat.String(), fmt.Sprintf("One of: %s", utils.QuoteJoin(format.SupportedFormats())))
flags.StringVar(&p.options.outputFile, flagNames.outputFile, stdoutFlagVal, "Optionally write output to file instead of stdout")
}
func (p *printer) VerifyFlags() error {
if !format.IsSupported(p.options.outputFormat) {
return errors.Errorf("--%s must be one of %s; got %q", flagNames.outputFormat, utils.QuoteJoin(format.SupportedFormats()), p.options.outputFormat)
}
return nil
}
func (p *printer) PrintOutput(output interface{}, stdout io.Writer) error {
var ft format.Format
if err := (&ft).FromString(p.options.outputFormat); err != nil {
return err
}
if p.options.outputFile == stdoutFlagVal {
// write to stdout
return ft.Format(output, stdout)
} else {
// write to file
return printToFile(output, ft, p.options.outputFile)
}
}
func printToFile(output interface{}, ft format.Format, filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
if err = ft.Format(output, f); err != nil {
if err2 := f.Close(); err2 != nil {
log.Err(err2)
}
return err
}
return f.Close()
}