-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroot.go
52 lines (42 loc) · 1.08 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
package cmd
import (
"fmt"
"github.com/fallais/gocoop/internal"
"os"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "gocoop",
Short: "GoCoop helps you manage your chicken coop",
PersistentPreRunE: persistentPreRunE,
Run: internal.Run,
}
func persistentPreRunE(cmd *cobra.Command, args []string) error {
logging, err := cmd.Flags().GetString("logging")
if err != nil {
return fmt.Errorf("error with the logging flag: %s", err)
}
// Parse the logging level
level, err := logrus.ParseLevel(logging)
if err != nil {
return fmt.Errorf("error while parsing the logging level: %s", err)
}
// Set
logrus.SetLevel(level)
// Set the TextFormatter
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
})
return nil
}
func init() {
rootCmd.PersistentFlags().StringP("logging", "l", "info", "Logging level")
rootCmd.Flags().StringP("config", "c", "config.yml", "Configuration file")
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}