Skip to content

Commit

Permalink
Merge pull request #5 from NETWAYS/feature/flags
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyfrosch committed May 14, 2020
2 parents 4c933f4 + f6acb3e commit ec3fddb
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
6 changes: 5 additions & 1 deletion exit.go
Expand Up @@ -2,9 +2,10 @@ package check

import (
"fmt"
"github.com/mitchellh/go-ps"
"os"
"runtime/debug"

"github.com/mitchellh/go-ps"
)

// AllowExit lets you disable the call to os.Exit() in ExitXxx() functions of this package.
Expand All @@ -18,7 +19,10 @@ var PrintStack = true
// Exit prints the plugin output and exits the program
func Exit(rc int, output string, args ...interface{}) {
fmt.Println(StatusText(rc), "-", fmt.Sprintf(output, args...))
BaseExit(rc)
}

func BaseExit(rc int) {
if AllowExit {
os.Exit(rc)
} else {
Expand Down
73 changes: 73 additions & 0 deletions flags.go
@@ -0,0 +1,73 @@
package check

import (
"fmt"
"os"
"path"

flag "github.com/spf13/pflag"

log "github.com/sirupsen/logrus"
)

type Flags struct {
Name string
Readme string
Version string
Verbose bool
Debug bool
PrintVersion bool
Set *flag.FlagSet
}

func NewFlags() *Flags {
flags := &Flags{}
flags.Name = path.Base(os.Args[0])

flagSet := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
flagSet.SortFlags = false

flagSet.Usage = func() {
fmt.Printf("Usage of %s\n", flags.Name)
if flags.Readme != "" {
fmt.Println()
fmt.Println(flags.Readme)
}
fmt.Println()
fmt.Println("Arguments:")
flagSet.PrintDefaults()
}

flagSet.BoolVarP(&flags.Debug, "debug", "d", false, "Enable debug mode")
flagSet.BoolVarP(&flags.Verbose, "verbose", "v", false, "Enable verbose mode")
flagSet.BoolVarP(&flags.PrintVersion, "version", "V", false, "Print version and exit")

flags.Set = flagSet

return flags
}

func (f *Flags) Parse(arguments []string) {
err := f.Set.Parse(arguments)
if err != nil {
if err != flag.ErrHelp {
ExitError(err)
}
BaseExit(3)
}

if f.PrintVersion {
fmt.Println(f.Name, "version", f.Version)
BaseExit(3)
}
}

func (f *Flags) SetupLogging() {
if f.Debug {
log.SetLevel(log.DebugLevel)
} else if f.Verbose {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.WarnLevel)
}
}
26 changes: 26 additions & 0 deletions flags_test.go
@@ -0,0 +1,26 @@
package check

import (
log "github.com/sirupsen/logrus"
)

func ExampleFlags() {
flags := NewFlags()
flags.Name = "check_test"
flags.Readme = `Test Plugin`
flags.Version = "1.0.0"

_ = flags.Set.StringP("hostname", "H", "localhost", "Hostname to check")

// flags.Parse(os.Args[1:])
flags.Parse([]string{"--help"})
flags.SetupLogging()

log.Info("test")
// Output: Usage of check_test
//
// Test Plugin
//
// Arguments:
// would exit with code 3
}
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -4,5 +4,7 @@ go 1.14

require (
github.com/mitchellh/go-ps v1.0.0
github.com/sirupsen/logrus v1.6.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.5.1
)
11 changes: 11 additions & 0 deletions go.sum
@@ -1,12 +1,23 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
Expand Down

0 comments on commit ec3fddb

Please sign in to comment.