-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
150 lines (116 loc) · 3.62 KB
/
main.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
148
149
150
package main
import (
"os"
"time"
gconfig "github.com/flant/glaball/pkg/config"
"github.com/flant/glaball/pkg/limiter"
"github.com/flant/glaball/cmd/cache"
"github.com/flant/glaball/cmd/common"
"github.com/flant/glaball/cmd/config"
"github.com/flant/glaball/cmd/info"
"github.com/flant/glaball/cmd/projects"
"github.com/flant/glaball/cmd/users"
"github.com/flant/glaball/cmd/versions"
"github.com/hashicorp/go-hclog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
cfgFile string
logLevel string // "debug", "info", "warn", "error", "off"
update bool
verbose bool
rootCmd = &cobra.Command{
Use: gconfig.ApplicationName,
Short: "Gitlab bulk administration tool",
Long: ``,
SilenceErrors: false,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if verbose {
logLevel = "debug"
}
if err := setLogLevel(logLevel); err != nil {
return err
}
if update {
viper.Set("cache.ttl", time.Duration(0))
}
if err := common.Init(); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
)
func Execute() {
if err := rootCmd.Execute(); err != nil {
hclog.L().Error(err.Error())
os.Exit(1)
}
}
func main() {
rootCmd.Execute()
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "",
"Path to the configuration file. (default \"$HOME/.config/glaball/config.yaml\")")
rootCmd.PersistentFlags().Int("threads", limiter.DefaultLimit,
"Number of concurrent processes. (default: one process for each Gitlab instances in config file)")
rootCmd.PersistentFlags().Duration("ttl", time.Duration(time.Hour*24),
"Override cache TTL set in config file")
rootCmd.PersistentFlags().StringP("filter", "f", ".*", "Select Gitlab(s) by regexp filter")
rootCmd.PersistentFlags().BoolP("all", "a", false, "Show all hosts in grouped output")
rootCmd.PersistentFlags().StringVar(&logLevel, "log_level", "info",
"Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, off]")
rootCmd.PersistentFlags().BoolVarP(&update, "update", "u", false, "Refresh cache")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")
rootCmd.AddCommand(
cache.NewCmd(),
config.NewCmd(),
info.NewCmd(),
projects.NewCmd(),
users.NewCmd(),
users.NewWhoamiCmd(),
versions.NewCmd(),
)
}
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Search config in default directory
configDir, _ := gconfig.DefaultConfigDir()
viper.AddConfigPath(configDir)
viper.SetConfigType("yaml")
viper.SetConfigName("config.yaml")
}
viper.SetDefault("cache.enabled", true)
viper.SetDefault("cache.size", gconfig.DefaultCacheSize)
viper.SetDefault("cache.compression", true)
viper.BindPFlag("cache.ttl", rootCmd.Flags().Lookup("ttl"))
viper.BindPFlag("filter", rootCmd.Flags().Lookup("filter"))
viper.BindPFlag("all", rootCmd.Flags().Lookup("all"))
viper.BindPFlag("threads", rootCmd.Flags().Lookup("threads"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err == nil {
hclog.L().Debug("Using config file", "config", viper.ConfigFileUsed())
}
}
func setLogLevel(logLevel string) error {
options := hclog.LoggerOptions{
Level: hclog.LevelFromString(logLevel),
JSONFormat: false,
IncludeLocation: false,
DisableTime: true,
Color: hclog.AutoColor,
IndependentLevels: false,
}
hclog.SetDefault(hclog.New(&options))
return nil
}