country-geo-locations/cfg/cfg.go

46 lines
1 KiB
Go
Raw Normal View History

2023-12-19 13:56:03 +01:00
package cfg
import (
"fmt"
"github.com/alecthomas/kong"
)
type AppSettings 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}"`
2024-03-02 10:29:59 +01:00
DataURL string `name:"data-url" env:"GEOIP_DATA_URL" help:"url to data file"`
}
func NewAppSettings() *AppSettings {
return &AppSettings{}
2023-12-19 13:56:03 +01:00
}
2024-03-02 10:29:59 +01:00
func (s *AppSettings) Parse() error {
2023-12-19 13:56:03 +01:00
ctx := kong.Parse(
2024-03-02 10:29:59 +01:00
s,
2023-12-19 13:56:03 +01:00
kong.Vars{
"default_address": ":8080",
"default_file_path": "./data.csv",
},
kong.Name("country_geo_locations"),
kong.Description("🚀 Start a simple web server for GeoIP data"),
kong.UsageOnError(),
)
2024-03-02 10:29:59 +01:00
err := validateFlags(*s)
if err != nil {
ctx.PrintUsage(false)
fmt.Println()
return err
2023-12-19 13:56:03 +01:00
}
2024-03-02 10:29:59 +01:00
return nil
2023-12-19 13:56:03 +01:00
}
2024-03-02 10:29:59 +01:00
func validateFlags(settings AppSettings) error {
if settings.DataURL == "" {
return fmt.Errorf("error: invalid data url, must not be blank")
2023-12-19 13:56:03 +01:00
}
2024-03-02 10:29:59 +01:00
return nil
2023-12-19 13:56:03 +01:00
}