-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
root.go
147 lines (129 loc) · 4.35 KB
/
root.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package cmd
import (
"fmt"
"io"
golog "log"
"os"
"path/filepath"
"sync"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/shibukawa/configdir"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var Version = "0.23.1"
var Banner = `
/\ |‾‾| /‾‾/ /‾/
/\ / \ | |_/ / / /
/ \/ \ | | / ‾‾\
/ \ | |‾\ \ | (_) |
/ __________ \ |__| \__\ \___/ .io`
var BannerColor = color.New(color.FgCyan)
var (
outMutex = &sync.Mutex{}
stdoutTTY = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
stderrTTY = isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())
stdout = consoleWriter{colorable.NewColorableStdout(), stdoutTTY, outMutex}
stderr = consoleWriter{colorable.NewColorableStderr(), stderrTTY, outMutex}
)
var (
cfgFile string
verbose bool
quiet bool
noColor bool
logFmt string
address string
)
// RootCmd represents the base command when called without any subcommands.
var RootCmd = &cobra.Command{
Use: "k6",
Short: "a next-generation load generator",
Long: BannerColor.Sprint(Banner),
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
setupLoggers(logFmt)
if noColor {
stdout.Writer = colorable.NewNonColorable(os.Stdout)
stderr.Writer = colorable.NewNonColorable(os.Stderr)
}
golog.SetOutput(log.StandardLogger().Writer())
},
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
log.Error(err.Error())
if e, ok := err.(ExitCode); ok {
os.Exit(e.Code)
}
os.Exit(-1)
}
}
func init() {
defaultConfigPathMsg := ""
configFolders := configDirs.QueryFolders(configdir.Global)
if len(configFolders) > 0 {
defaultConfigPathMsg = fmt.Sprintf(" (default %s)", filepath.Join(configFolders[0].Path, configFilename))
}
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable debug logging")
RootCmd.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "disable progress updates")
RootCmd.PersistentFlags().BoolVar(&noColor, "no-color", false, "disable colored output")
RootCmd.PersistentFlags().StringVar(&logFmt, "logformat", "", "log output format")
RootCmd.PersistentFlags().StringVarP(&address, "address", "a", "localhost:6565", "address for the api server")
RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file"+defaultConfigPathMsg)
must(cobra.MarkFlagFilename(RootCmd.PersistentFlags(), "config"))
}
// fprintf panics when where's an error writing to the supplied io.Writer
func fprintf(w io.Writer, format string, a ...interface{}) (n int) {
n, err := fmt.Fprintf(w, format, a...)
if err != nil {
panic(err.Error())
}
return n
}
// RawFormatter it does nothing with the message just prints it
type RawFormater struct{}
// Format renders a single log entry
func (f RawFormater) Format(entry *log.Entry) ([]byte, error) {
return append([]byte(entry.Message), '\n'), nil
}
func setupLoggers(logFmt string) {
if verbose {
log.SetLevel(log.DebugLevel)
}
log.SetOutput(stderr)
switch logFmt {
case "raw":
log.SetFormatter(&RawFormater{})
log.Debug("Logger format: RAW")
case "json":
log.SetFormatter(&log.JSONFormatter{})
log.Debug("Logger format: JSON")
default:
log.SetFormatter(&log.TextFormatter{ForceColors: stderrTTY, DisableColors: noColor})
log.Debug("Logger format: TEXT")
}
}