From 916dc35de6c71ed2411a9c470f4adc8b3e6f89ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Fri, 19 Feb 2021 10:53:07 +0100 Subject: [PATCH] Use golf for command line parsing * Split command line flags data from the main program data structure * Use golf to support GNU-style flags * Added -h/--help flag --- .gitignore | 1 + cmd/check_ssl_certificate/main.go | 51 +++++++++++++++++++------------ go.mod | 2 ++ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index ba077a4..6a365c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ bin +go.sum \ No newline at end of file diff --git a/cmd/check_ssl_certificate/main.go b/cmd/check_ssl_certificate/main.go index df07714..4a47230 100644 --- a/cmd/check_ssl_certificate/main.go +++ b/cmd/check_ssl_certificate/main.go @@ -3,42 +3,55 @@ package main import ( "crypto/tls" "crypto/x509" - "flag" "fmt" + "os" "strings" "time" "nocternity.net/monitoring/perfdata" "nocternity.net/monitoring/plugin" + + "github.com/karrick/golf" ) -type checkProgram struct { +type programFlags struct { hostname string port int warn int crit int ignoreCnOnly bool - - certificate *x509.Certificate - plugin *plugin.Plugin } -func newProgram() (flags *checkProgram) { - flags = &checkProgram{ +type checkProgram struct { + programFlags + plugin *plugin.Plugin + certificate *x509.Certificate +} + +func (flags *programFlags) parseArguments() { + var help bool + golf.BoolVarP(&help, 'h', "help", false, "Display usage information") + golf.StringVarP(&flags.hostname, 'H', "hostname", "", "Host name to connect to.") + golf.IntVarP(&flags.port, 'P', "port", -1, "Port to connect to.") + golf.IntVarP(&flags.warn, 'W', "warning", -1, + "Validity threshold below which a warning state is issued, in days.") + golf.IntVarP(&flags.crit, 'C', "critical", -1, + "Validity threshold below which a critical state is issued, in days.") + golf.BoolVar(&flags.ignoreCnOnly, "ignore-cn-only", false, + "Do not issue warnings regarding certificates that do not use SANs at all.") + golf.Parse() + if help { + golf.Usage() + os.Exit(0) + } +} + +func newProgram() *checkProgram { + program := &checkProgram{ plugin: plugin.New("Certificate check"), } - flag.StringVar(&flags.hostname, "hostname", "", "Host name to connect to.") - flag.StringVar(&flags.hostname, "H", "", "Host name to connect to (shorthand).") - flag.IntVar(&flags.port, "port", -1, "Port to connect to.") - flag.IntVar(&flags.port, "P", -1, "Port to connect to (shorthand).") - flag.IntVar(&flags.warn, "warning", -1, "Validity threshold below which a warning state is issued, in days.") - flag.IntVar(&flags.warn, "W", -1, "Validity threshold below which a warning state is issued, in days (shorthand).") - flag.IntVar(&flags.crit, "critical", -1, "Validity threshold below which a critical state is issued, in days.") - flag.IntVar(&flags.crit, "C", -1, "Validity threshold below which a critical state is issued, in days (shorthand).") - flag.BoolVar(&flags.ignoreCnOnly, "ignore-cn-only", false, - "Do not issue warnings regarding certificates that do not use SANs at all.") - flag.Parse() - return + program.parseArguments() + return program } func (program *checkProgram) close() { diff --git a/go.mod b/go.mod index c5a9fc8..b415473 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module nocternity.net/monitoring go 1.15 + +require github.com/karrick/golf v1.4.0