/
root.go
95 lines (84 loc) · 2.66 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
package cmd
import (
"fmt"
"os"
"runtime/pprof"
"github.com/nextlinux/k8s-inventory/pkg/mode"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/nextlinux/k8s-inventory/pkg"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "nextlinux-k8s-inventory",
Short: "nextlinux-k8s-inventory tells Nextlinux which images are in use in your Kubernetes Cluster",
Long: `Nextlinux Kubernetes Inventory can poll
Kubernetes Cluster API(s) to tell Nextlinux which Images are currently in-use`,
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
log.Info("nextlinux-k8s-inventory is starting up...")
if appConfig.Dev.ProfileCPU {
f, err := os.Create("cpu.profile")
if err != nil {
log.Errorf("unable to create CPU profile: %+v", err)
} else {
err := pprof.StartCPUProfile(f)
if err != nil {
log.Errorf("unable to start CPU profile: %+v", err)
}
}
}
if len(args) > 0 {
err := cmd.Help()
if err != nil {
log.Errorf(err.Error())
os.Exit(1)
}
os.Exit(1)
}
switch appConfig.RunMode {
case mode.PeriodicPolling:
pkg.PeriodicallyGetInventoryReport(appConfig)
default:
report, err := pkg.GetInventoryReport(appConfig)
if appConfig.Dev.ProfileCPU {
pprof.StopCPUProfile()
}
if err != nil {
log.Errorf("Failed to get Image Results: %+v", err)
os.Exit(1)
}
err = pkg.HandleReport(report, appConfig)
if err != nil {
log.Errorf("Failed to handle Image Results: %+v", err)
os.Exit(1)
}
}
},
}
func init() {
opt := "kubeconfig"
rootCmd.Flags().StringP(opt, "k", "", "(optional) absolute path to the kubeconfig file")
if err := viper.BindPFlag(opt+".path", rootCmd.Flags().Lookup(opt)); err != nil {
fmt.Printf("unable to bind flag '%s': %+v", opt, err)
os.Exit(1)
}
opt = "mode"
rootCmd.Flags().StringP(opt, "m", mode.AdHoc.String(), fmt.Sprintf("execution mode, options=%v", mode.Modes))
if err := viper.BindPFlag(opt, rootCmd.Flags().Lookup(opt)); err != nil {
fmt.Printf("unable to bind flag '%s': %+v", opt, err)
os.Exit(1)
}
opt = "polling-interval-seconds"
rootCmd.Flags().StringP(opt, "p", "300", "If mode is 'periodic', this specifies the interval")
if err := viper.BindPFlag(opt, rootCmd.Flags().Lookup(opt)); err != nil {
fmt.Printf("unable to bind flag '%s': %+v", opt, err)
os.Exit(1)
}
opt = "verbose-inventory-reports"
rootCmd.Flags().BoolP(opt, "i", false, "If true, will print the full inventory report to stdout")
if err := viper.BindPFlag(opt, rootCmd.Flags().Lookup(opt)); err != nil {
fmt.Printf("unable to bind flag '%s': %+v", opt, err)
os.Exit(1)
}
}