-
Notifications
You must be signed in to change notification settings - Fork 26
/
output.go
63 lines (57 loc) · 1.14 KB
/
output.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
package output
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
)
const (
// FormatKey ...
FormatKey = "format"
// FormatRaw ...
FormatRaw = "raw"
// FormatJSON ...
FormatJSON = "json"
// FormatYML ...
FormatYML = "yml"
)
// Format ...
var Format = FormatRaw
// ConfigureOutputFormat ...
func ConfigureOutputFormat(c *cli.Context) error {
outFmt := c.String(FormatKey)
switch outFmt {
case FormatRaw, FormatJSON, FormatYML:
// valid
Format = outFmt
case "":
// default
Format = FormatRaw
default:
// invalid
return fmt.Errorf("Invalid Output Format: %s", outFmt)
}
return nil
}
// Print ...
func Print(outModel interface{}, format string) {
switch format {
case FormatJSON:
serBytes, err := json.Marshal(outModel)
if err != nil {
log.Errorf("[.print] ERROR: %s", err)
return
}
fmt.Printf("%s\n", serBytes)
case FormatYML:
serBytes, err := yaml.Marshal(outModel)
if err != nil {
log.Errorf("[output.print] ERROR: %s", err)
return
}
fmt.Printf("%s\n", serBytes)
default:
log.Errorf("[output.print] Invalid output format: %s", format)
}
}