Skip to content

Commit ae12899

Browse files
authored
Merge pull request #21 from Esonhugh/feat/verbose-counting
Feat/verbose counting
2 parents bb07d38 + 90f8537 commit ae12899

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

cmd/root.go

+19-22
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var Opts = struct {
1515
SvcDomains []string
1616
Zone string
1717
OutputFile string
18-
Verbose string
18+
Verbose int
1919

2020
MultiThreadingMode bool
2121
ThreadingNum int
@@ -30,7 +30,7 @@ func init() {
3030
RootCmd.PersistentFlags().StringSliceVarP(&Opts.SvcDomains, "svc-domains", "s", []string{}, "service domains, like: kubernetes.default,etcd.default don't add zone like svc.cluster.local")
3131
RootCmd.PersistentFlags().StringVarP(&Opts.Zone, "zone", "z", "cluster.local", "zone")
3232
RootCmd.PersistentFlags().StringVarP(&Opts.OutputFile, "output-file", "o", "", "output file")
33-
RootCmd.PersistentFlags().StringVarP(&Opts.Verbose, "verbose", "v", "info", "log level (debug,info,trace,warn,error,fatal,panic)")
33+
RootCmd.PersistentFlags().CountVarP(&Opts.Verbose, "verbose", "v", "log level (-v debug,-vv trace, info")
3434
RootCmd.PersistentFlags().BoolVarP(&Opts.MultiThreadingMode, "thread", "t", false, "multi threading mode, work pair with -n")
3535
RootCmd.PersistentFlags().IntVarP(&Opts.ThreadingNum, "thread-num", "n", 16, "threading num, default 16")
3636

@@ -42,42 +42,39 @@ var RootCmd = &cobra.Command{
4242
Short: "k8spider is a tool to discover k8s services",
4343
Long: "k8spider is a tool to discover k8s services",
4444
PersistentPreRun: func(cmd *cobra.Command, args []string) {
45+
// Set Log Levels
4546
SetLogLevel(Opts.Verbose)
47+
// Set pkg global config
48+
pkg.Zone = Opts.Zone
4649
if Opts.DnsServer != "" {
4750
pkg.NetResolver = pkg.WarpDnsServer(Opts.DnsServer)
4851
}
49-
if Opts.SkipKubeDNSCheck == false { // Not Skip
52+
// Check if current environment is a kubernetes cluster
53+
// If the command is whereisdns, that DNS is not sure , so skip this check
54+
// If SkipKubeDNSCheck is true, skip this check!
55+
if Opts.SkipKubeDNSCheck == false || cmd.Use != "whereisdns" {
5056
if pkg.CheckKubeDNS() {
5157
log.Warn("current environment is not a kubernetes cluster")
5258
os.Exit(1)
5359
}
5460
}
55-
pkg.Zone = Opts.Zone
5661
},
5762
Run: func(cmd *cobra.Command, args []string) {
5863
_ = cmd.Help()
5964
},
6065
}
6166

62-
func SetLogLevel(level string) {
63-
switch level {
64-
case "trace":
65-
log.SetLevel(log.TraceLevel)
66-
case "debug":
67-
log.SetLevel(log.DebugLevel)
68-
case "info":
69-
log.SetLevel(log.InfoLevel)
70-
case "warn":
71-
log.SetLevel(log.WarnLevel)
72-
case "error":
73-
log.SetLevel(log.ErrorLevel)
74-
case "fatal":
75-
log.SetLevel(log.FatalLevel)
76-
case "panic":
77-
log.SetLevel(log.PanicLevel)
78-
default:
79-
log.SetLevel(log.InfoLevel)
67+
var LevelMap = []log.Level{
68+
log.InfoLevel, // 0
69+
log.DebugLevel, // 1
70+
log.TraceLevel, // 2
71+
}
72+
73+
func SetLogLevel(level int) {
74+
if level > 2 {
75+
level = 2
8076
}
77+
log.SetLevel(LevelMap[level])
8178
}
8279

8380
func Execute() {

0 commit comments

Comments
 (0)