Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from NETWAYS/feature/flags
- Loading branch information
Showing
5 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters