Skip to content

Commit

Permalink
feat: add proper help flag, make mango charityware <3
Browse files Browse the repository at this point in the history
RIP Bram.
  • Loading branch information
tjhop committed Nov 10, 2023
1 parent 72e02f3 commit d2ea736
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,25 @@ All options:

```bash
~/go/src/github.com/tjhop/mango (main [ ]) -> ./mango -h
_ __ ___ __ _ _ __ __ _ ___
| '_ ` _ \ / _` || '_ \ / _` | / _ \
| | | | | || (_| || | | || (_| || (_) |
|_| |_| |_| \__,_||_| |_| \__, | \___/
|___/
Usage of ./mango:
--hostname string Custom hostname to use (default's to system hostname if unset)
-h, --help Prints help and usage information
--hostname string (Requires root) Custom hostname to use [default is system hostname]
-i, --inventory.path string Path to mango configuration inventory
--inventory.reload-interval string Time duration for how frequently mango will auto reload and apply the inventory [default disabled]
-l, --logging.level string Logging level may be one of: [trace, debug, info, warning, error, fatal and panic]
--logging.output string Logging format may be one of: [logfmt, json] (default "logfmt")
pflag: help requested
Mango is charityware, in honor of Bram Moolenaar and out of respect for Vim. You can use and copy it as much as you like, but you are encouraged to make a donation for needy children in Uganda. Please visit the ICCF web site, available at these URLs:
https://iccf-holland.org/
https://www.vim.org/iccf/
https://www.iccf.nl/
```
### Container Usage
Expand Down
22 changes: 15 additions & 7 deletions cmd/mango/mango.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
"|_| |_| |_| \\__,_||_| |_| \\__, | \\___/\n" +
" |___/\n"
defaultPrometheusPort = 9555
charitywareMsg = "\nMango is charityware, in honor of Bram Moolenaar and out of respect for Vim. You can use and copy it as much as you like, but you are encouraged to make a donation for needy children in Uganda. Please visit the ICCF web site, available at these URLs:\n\nhttps://iccf-holland.org/\nhttps://www.vim.org/iccf/\nhttps://www.iccf.nl/\n"
)

var (
Expand Down Expand Up @@ -447,13 +448,6 @@ func cleanup(ctx context.Context, logger *slog.Logger) {
}

func main() {
// prep and parse flags
flag.StringP("inventory.path", "i", "", "Path to mango configuration inventory")
flag.String("inventory.reload-interval", "", "Time duration for how frequently mango will auto reload and apply the inventory [default disabled]")
flag.StringP("logging.level", "l", "", "Logging level may be one of: [trace, debug, info, warning, error, fatal and panic]")
flag.String("logging.output", "logfmt", "Logging format may be one of: [logfmt, json]")
flag.String("hostname", "", "(Requires root) Custom hostname to use [default is system hostname]")

// create root logger with default configs, parse out updated configs from flags
logLevel := new(slog.LevelVar) // default to info level logging
logHandlerOpts := &slog.HandlerOptions{
Expand All @@ -463,6 +457,14 @@ func main() {
logger := slog.New(logHandler)
rootCtx := context.Background()

// prep and parse flags
flag.StringP("inventory.path", "i", "", "Path to mango configuration inventory")
flag.String("inventory.reload-interval", "", "Time duration for how frequently mango will auto reload and apply the inventory [default disabled]")
flag.StringP("logging.level", "l", "", "Logging level may be one of: [trace, debug, info, warning, error, fatal and panic]")
flag.String("logging.output", "logfmt", "Logging format may be one of: [logfmt, json]")
flag.String("hostname", "", "(Requires root) Custom hostname to use [default is system hostname]")
flag.BoolP("help", "h", false, "Prints help and usage information")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s\nUsage of %s:\n", programNameAsciiArt, os.Args[0])
flag.PrintDefaults()
Expand All @@ -479,6 +481,12 @@ func main() {
os.Exit(1)
}

if viper.GetBool("help") {
flag.Usage()
fmt.Fprintf(os.Stderr, charitywareMsg)

Check failure on line 486 in cmd/mango/mango.go

View workflow job for this annotation

GitHub Actions / lint

SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)

Check failure on line 486 in cmd/mango/mango.go

View workflow job for this annotation

GitHub Actions / lint

SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)
os.Exit(0)
}

// parse log level from flag
logLevelFlagVal := strings.TrimSpace(strings.ToLower(viper.GetString("logging.level")))
switch logLevelFlagVal {
Expand Down

0 comments on commit d2ea736

Please sign in to comment.