-
Notifications
You must be signed in to change notification settings - Fork 121
/
commandline.go
63 lines (50 loc) · 2.13 KB
/
commandline.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
// commandline.go contains logic and data structures relevant to actually
// running peirates as a command line tool. Mainly this is just flag handling.
package peirates
import (
"flag" // Command line flag parsing
"log"
"os"
"strings"
)
type CommandLineOptions struct {
connectionConfig *ServerInfo
moduleToRun string
verbose bool
}
// parseOptions parses command-line options. We call it in main().
// func parseOptions(connectionString *ServerInfo, kubeData *Kube_Data) {
func parseOptions(opts *CommandLineOptions) {
// This is like the parser.add_option stuff except it works implicitly on a global parser instance.
// Notice the use of pointers (&connectionString.APIServer for example) to bind flags to variables
flagset := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagset.StringVar(&opts.connectionConfig.APIServer, "u", opts.connectionConfig.APIServer, "API Server URL: ex. https://10.96.0.1:6443")
flagset.StringVar(&opts.connectionConfig.Token, "t", opts.connectionConfig.Token, "Token (JWT)")
flagset.StringVar(&opts.moduleToRun, "m", "", "module to run from menu - items on main menu with an * support this.")
flagset.BoolVar(&opts.verbose, "v", false, "verbose mode - display debug messages")
// This is the function that actually runs the parser
// once you've defined all your options.
err := flagset.Parse(os.Args[1:])
if err != nil {
println("Problem with args: %v", err)
}
// If the API Server URL is passed in, normalize it.
if len(opts.connectionConfig.APIServer) > 0 {
// Trim any leading or trailing whitespace
APIServer := strings.TrimSpace(opts.connectionConfig.APIServer)
// Remove any trailing /
APIServer = strings.TrimSuffix(APIServer, "/")
// Check to see if APIServer begins with http or https, adding https if it does not.
if !(strings.HasPrefix(APIServer, "http://") || strings.HasPrefix(APIServer, "https://")) {
APIServer = "https://" + APIServer
}
opts.connectionConfig.APIServer = APIServer
}
if opts.connectionConfig.Token != "" {
log.Println("JWT provided on the command line.")
}
Verbose = opts.verbose
if Verbose {
println("DEBUG: verbose mode on")
}
}