-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
114 lines (93 loc) · 2.69 KB
/
cli.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
package main
import (
"errors"
"fmt"
"github.com/rfizzle/collector-helpers/outputs"
"github.com/rfizzle/collector-helpers/state"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"log"
"os"
"path/filepath"
"strings"
)
func setupCliFlags() error {
viper.SetEnvPrefix("GSC")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
flag.Int("schedule", 30, "time in seconds to collect")
flag.String("gsuite-credentials", "", "google service account credential file path")
flag.String("impersonated-user", "", "user to impersonate for API access")
flag.BoolP("verbose", "v", false, "verbose logging")
flag.BoolP("config", "c", false, "enable config file")
flag.String("config-path", "", "config file path")
state.InitCLIParams()
outputs.InitCLIParams()
flag.Parse()
err := viper.BindPFlags(flag.CommandLine)
if err != nil {
log.Fatalf("Failed parsing flags: %v", err.Error())
}
// Check config
if err := checkConfigParams(); err != nil {
return err
}
// Check parameters
if err := checkRequiredParams(); err != nil {
return err
}
return nil
}
func checkConfigParams() error {
if viper.GetBool("config") {
if !fileExists(viper.GetString("config-path")) {
return errors.New("missing config file path param (--config-path)")
}
dir, file := filepath.Split(viper.GetString("config-path"))
ext := strings.ToLower(filepath.Ext(viper.GetString("config-path")))
supportedTypes := []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv"}
if !contains(supportedTypes, ext) {
e := fmt.Sprintf("invalid config file type (supported: %s )", strings.Join(supportedTypes[:], ", "))
return errors.New(e)
}
fileName := strings.TrimSuffix(file, ext)
viper.SetConfigName(fileName)
viper.SetConfigType(ext)
viper.AddConfigPath(dir)
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return fmt.Errorf("Fatal error config file: %s \n", err)
}
}
return nil
}
func checkRequiredParams() error {
if viper.GetString("gsuite-credentials") == "" {
return errors.New("missing google credentials param (--gsuite-credentials)")
}
if viper.GetString("impersonated-user") == "" {
return errors.New("missing Impersonate User param (--impersonated-user)")
}
if err := state.ValidateCLIParams(); err != nil {
return err
}
if err := outputs.ValidateCLIParams(); err != nil {
return err
}
return nil
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}