You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.3 KiB

package main
import (
"flag"
"log"
"github.com/caarlos0/env"
)
type botOptions struct {
Debug bool `env:"BOT_DEBUG" envDefault:"false"`
LogConvIDStr string `env:"BOT_LOG_CONVID" envDefault:""`
HomePath string `envDefault:""`
JSON bool `env:"BOT_LOG_JSON" envDefault:"false"`
}
func parseArgs(args []string) botOptions {
// parse environment
opts := botOptions{}
if err := env.Parse(&opts); err != nil {
log.Fatalf("Unable to parse env vars: %+v", err)
}
// parse CLI
flags := flag.NewFlagSet(args[0], flag.ExitOnError)
cliOpts := botOptions{}
flags.BoolVar(&cliOpts.Debug, "debug", false, "enables debugging")
flags.BoolVar(&cliOpts.JSON, "json", false, "enables JSON logging")
flags.StringVar(&cliOpts.LogConvIDStr, "log-convid", "", "set the keybase conversation log id")
flags.StringVar(&cliOpts.HomePath, "kbhome", "", "sets alternate keybase home folder for debugging")
if err := flags.Parse(args[1:]); err != nil {
log.Fatalf("Unable to parse cli args: %+v", err)
}
//overwrite ENV with CLI
if flags.NFlag() > 0 {
if cliOpts.Debug == true {
opts.Debug = true
}
if cliOpts.JSON == true {
opts.JSON = true
}
if cliOpts.HomePath != "" {
opts.HomePath = cliOpts.HomePath
}
if cliOpts.LogConvIDStr != "" {
opts.LogConvIDStr = cliOpts.LogConvIDStr
}
}
return opts
}