-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
45 lines (36 loc) · 1.52 KB
/
cmd.go
File metadata and controls
45 lines (36 loc) · 1.52 KB
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
package main
import (
"os"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "jsonschema-transform",
Short: "Welcome to the JSON Schema Transformer CLI",
Long: `Welcome to the JSON Schema Transformer CLI
The goal of this transformer is to generate diagrams from JSON schema files. This helps to visualize the overall schema`,
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
verboseFlag, _ := cmd.Flags().GetCount("verbose")
quietFlag, _ := cmd.Flags().GetCount("quiet")
// We default to only showing warnings and errors, but it can be increased to info and debug, and
// decreased to error and fatal
logLevel := logrus.WarnLevel + logrus.Level(verboseFlag) - logrus.Level(quietFlag)
logrus.SetLevel(logLevel)
logrus.SetFormatter(&MsgFormatter{})
},
}
// MsgFormatter is a logrus.Formatter that only prints the logrus.Entry.Message
type MsgFormatter struct{}
// Format the logrus.Entry to only use the logrus.Entry.Message and a newline
func (f MsgFormatter) Format(entry *logrus.Entry) ([]byte, error) {
return []byte(entry.Message + "\n"), nil
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.PersistentFlags().CountP("verbose", "v", "Increase the verbosity of the output by one level, -v shows informational logs and -vv will output debug information.")
rootCmd.PersistentFlags().CountP("quiet", "q", "Decrease the verbosity of the output by one level, -v hides warning logs and -vv will suppress non-fatal errors")
}