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
This commit is contained in:
Emmanuel BENOîT 2021-02-19 10:53:07 +01:00
parent 2c5d27684a
commit 916dc35de6
3 changed files with 35 additions and 19 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
bin
go.sum

View file

@ -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() {

2
go.mod
View file

@ -1,3 +1,5 @@
module nocternity.net/monitoring
go 1.15
require github.com/karrick/golf v1.4.0