forked from pete911/certinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.go
68 lines (59 loc) · 1.96 KB
/
flag.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
package main
import (
"flag"
"fmt"
"os"
"strconv"
)
type Flags struct {
Usage func()
Expiry bool
NoDuplicate bool
NoExpired bool
Insecure bool
Chains bool
Pem bool
PemOnly bool
Version bool
Args []string
}
func ParseFlags() (Flags, error) {
var flags Flags
flagSet := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
flagSet.BoolVar(&flags.Expiry, "expiry", getBoolEnv("CERTINFO_EXPIRY", false),
"print expiry of certificates")
flagSet.BoolVar(&flags.NoDuplicate, "no-duplicate", getBoolEnv("CERTINFO_NO_DUPLICATE", false),
"do not print duplicate certificates")
flagSet.BoolVar(&flags.NoExpired, "no-expired", getBoolEnv("CERTINFO_NO_EXPIRED", false),
"do not print expired certificates")
flagSet.BoolVar(&flags.Insecure, "insecure", getBoolEnv("CERTINFO_INSECURE", false),
"whether a client verifies the server's certificate chain and host name (only applicable for host)")
flagSet.BoolVar(&flags.Chains, "chains", getBoolEnv("CERTINFO_CHAINS", false),
"whether to print verified chains as well (only applicable for host)")
flagSet.BoolVar(&flags.Pem, "pem", getBoolEnv("CERTINFO_PEM", false),
"whether to print pem as well")
flagSet.BoolVar(&flags.PemOnly, "pem-only", getBoolEnv("CERTINFO_PEM_ONLY", false),
"whether to print only pem (useful for downloading certs from host)")
flagSet.BoolVar(&flags.Version, "version", getBoolEnv("CERTINFO_VERSION", false),
"certinfo version")
flagSet.Usage = func() {
fmt.Fprint(flagSet.Output(), "Usage: certinfo [flags] [<file>|<host:port> ...]\n")
flagSet.PrintDefaults()
}
flags.Usage = flagSet.Usage
if err := flagSet.Parse(os.Args[1:]); err != nil {
return Flags{}, err
}
flags.Args = flagSet.Args()
return flags, nil
}
func getBoolEnv(envName string, defaultValue bool) bool {
env, ok := os.LookupEnv(envName)
if !ok {
return defaultValue
}
if intValue, err := strconv.ParseBool(env); err == nil {
return intValue
}
return defaultValue
}