package cfg import ( "fmt" "os" "github.com/alecthomas/kong" ) type AppSettings struct { ServerAddress string DataFile string DataURL string } var cliStruct struct { ServerAddress string `name:"listen-address" env:"GEOIP_LISTEN_ADDRESS" help:"Address to use for the metrics server" default:"${default_address}"` DataFile string `name:"data-file" env:"GEOIP_DATA_FILE" help:"path to data file" default:"${default_file_path}"` DataURL string `name:"data-url" env:"GEOIP_DATA_URL" help:"url to data file" default:"${default_file_url}"` } func Parse() *AppSettings { ctx := kong.Parse( &cliStruct, kong.Vars{ "default_address": ":8080", "default_file_path": "./data.csv", "default_file_url": "https://data.neuber.io/data.csv", }, kong.Name("country_geo_locations"), kong.Description("🚀 Start a simple web server for GeoIP data"), kong.UsageOnError(), ) validateFlags(ctx) settings := &AppSettings{ ServerAddress: cliStruct.ServerAddress, DataFile: cliStruct.DataFile, DataURL: cliStruct.DataURL, } return settings } func validateFlags(cliCtx *kong.Context) { var flagsValid = true var messages = []string{} if cliStruct.ServerAddress == "" { messages = append(messages, "error: invalid server address, must not be blank") flagsValid = false } if !flagsValid { cliCtx.PrintUsage(false) fmt.Println() for i := 0; i < len(messages); i++ { fmt.Println(messages[i]) } os.Exit(1) } }