Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 91fd5c9

Browse files
author
Massimiliano Pippi
committedAug 29, 2019
clean up format options
1 parent 363fbd5 commit 91fd5c9

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed
 

‎cli/cli.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ func toLogLevel(s string) (t logrus.Level, found bool) {
105105
}
106106

107107
func parseFormatString(arg string) (feedback.OutputFormat, bool) {
108-
arg = strings.ToLower(arg)
109108
f, found := map[string]feedback.OutputFormat{
110109
"json": feedback.JSON,
111110
"text": feedback.Text,
@@ -115,6 +114,10 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
115114
}
116115

117116
func preRun(cmd *cobra.Command, args []string) {
117+
// normalize the format strings
118+
globals.OutputFormat = strings.ToLower(globals.OutputFormat)
119+
logFormat = strings.ToLower(logFormat)
120+
118121
// should we log to file?
119122
if logFile != "" {
120123
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
@@ -124,7 +127,11 @@ func preRun(cmd *cobra.Command, args []string) {
124127
}
125128

126129
// we use a hook so we don't get color codes in the log file
127-
logrus.AddHook(lfshook.NewHook(file, &logrus.TextFormatter{}))
130+
if logFormat == "json" {
131+
logrus.AddHook(lfshook.NewHook(file, &logrus.JSONFormatter{}))
132+
} else {
133+
logrus.AddHook(lfshook.NewHook(file, &logrus.TextFormatter{}))
134+
}
128135
}
129136

130137
// should we log to stdout?
@@ -146,19 +153,20 @@ func preRun(cmd *cobra.Command, args []string) {
146153
logrus.SetLevel(lvl)
147154
}
148155

149-
// check the right format was passed
150-
if f, found := parseFormatString(globals.OutputFormat); !found {
156+
// set the Logger format
157+
if logFormat == "json" {
158+
logrus.SetFormatter(&logrus.JSONFormatter{})
159+
}
160+
161+
// check the right output format was passed
162+
format, found := parseFormatString(globals.OutputFormat)
163+
if !found {
151164
feedback.Error("Invalid output format: " + globals.OutputFormat)
152165
os.Exit(errorcodes.ErrBadCall)
153-
} else {
154-
// use the format to configure the Feedback
155-
feedback.SetFormat(f)
156166
}
157167

158-
// set the Logger format
159-
if strings.ToLower(logFormat) == "json" {
160-
logrus.SetFormatter(&logrus.JSONFormatter{})
161-
}
168+
// use the output format to configure the Feedback
169+
feedback.SetFormat(format)
162170

163171
globals.InitConfigs()
164172

0 commit comments

Comments
 (0)
Please sign in to comment.