resource-scraper/internal/flag.go

61 lines
1.7 KiB
Go

package scraper
import (
"flag"
"fmt"
"log"
"runtime"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"git.amok.space/yevhen/resource-scraper/types/constant"
)
const usage = `Music Database (MDB) server/cli craftware'
Usage:
mdb [command] -c [config-file-path]
Commands:
-help, -h Print this message
-version -v Print version.
-config -c Set configuration file without extension. It is %s by default.
-section -s Select section e.g.: rutracker, bandcamp etc.
The Go runtime version: %s
Report bugs to https://git.amok.space/yevhen/resource-scraper/issues`
const (
version = "0.1"
defaultConfigPath = constant.DefaultConfigPath
defaultConfigEnv = constant.DefaultEnvProd
)
func ParseFlags() {
flag.Bool(constant.FlagHelpShort, false, "")
flag.Bool(constant.FlagHelp, false, "")
flag.Bool(constant.FlagVersionShort, false, "")
flag.Bool(constant.FlagVersion, false, "")
flag.Bool(constant.FlagDebug, false, "")
flag.String(constant.FlagConfigFile, defaultConfigPath, "config file location")
flag.String(constant.FlagScopeEnable, "", "")
flag.String(constant.FlagSingleUri, "", "")
flag.String(constant.FlagEnv, defaultConfigEnv, "")
flag.String("create", defaultConfigEnv, "used to create e.g. scope etc.")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
log.Fatalf("[ERR] Failed to bind flags to config: %s", err)
}
if viper.GetBool(constant.FlagHelpShort) || viper.GetBool(constant.FlagHelp) {
msg := fmt.Sprintf(usage, defaultConfigPath, runtime.Version())
fmt.Println(msg)
} else if viper.GetBool(constant.FlagVersionShort) || viper.GetBool(constant.FlagVersion) {
fmt.Println("MDB version", version)
}
}