package args import ( "errors" "flag" "os" "strings" "golang.org/x/oauth2" ) type Arguments struct { DatabasePath string TemplatePath string StaticPath string Migrate bool Scheduler bool Port int Server bool OauthConfig *oauth2.Config OauthUserInfoURI string DnsResolvers []string Dns bool DnsPort int CloudflareToken string CloudflareZone string HcaptchaSecret string HcaptchaSiteKey string } func GetArgs() (*Arguments, error) { databasePath := flag.String("database-path", "./hatecomputers.db", "Path to the SQLite database") templatePath := flag.String("template-path", "./templates", "Path to the template directory") staticPath := flag.String("static-path", "./static", "Path to the static directory") dnsResolvers := flag.String("dns-resolvers", "1.1.1.1:53,1.0.0.1:53", "Comma-separated list of DNS resolvers") scheduler := flag.Bool("scheduler", false, "Run scheduled jobs via cron") migrate := flag.Bool("migrate", false, "Run the migrations") port := flag.Int("port", 8080, "Port to listen on") server := flag.Bool("server", false, "Run the server") dns := flag.Bool("dns", false, "Run DNS resolver") dnsPort := flag.Int("dns-port", 8053, "Port to listen on for DNS resolver") flag.Parse() cloudflareToken := os.Getenv("CLOUDFLARE_TOKEN") cloudflareZone := os.Getenv("CLOUDFLARE_ZONE") oauthClientID := os.Getenv("OAUTH_CLIENT_ID") oauthClientSecret := os.Getenv("OAUTH_CLIENT_SECRET") oauthScopes := os.Getenv("OAUTH_SCOPES") oauthAuthURL := os.Getenv("OAUTH_AUTH_URL") oauthTokenURL := os.Getenv("OAUTH_TOKEN_URL") oauthRedirectURI := os.Getenv("OAUTH_REDIRECT_URI") oauthUserInfoURI := os.Getenv("OAUTH_USER_INFO_URI") hcaptchaSecret := os.Getenv("HCAPTCHA_SECRET") hcaptchaSiteKey := os.Getenv("HCAPTCHA_SITE_KEY") envVars := [][]string{ {cloudflareToken, "CLOUDFLARE_TOKEN"}, {cloudflareZone, "CLOUDFLARE_ZONE"}, {oauthClientID, "OAUTH_CLIENT_ID"}, {oauthClientSecret, "OAUTH_CLIENT_SECRET"}, {oauthScopes, "OAUTH_SCOPES"}, {oauthAuthURL, "OAUTH_AUTH_URL"}, {oauthTokenURL, "OAUTH_TOKEN_URL"}, {oauthRedirectURI, "OAUTH_REDIRECT_URI"}, {oauthUserInfoURI, "OAUTH_USER_INFO_URI"}, } for _, envVar := range envVars { if envVar[0] == "" { return nil, errors.New("please set the " + envVar[1] + " environment variable") } } oauthConfig := &oauth2.Config{ ClientID: oauthClientID, ClientSecret: oauthClientSecret, Scopes: strings.Split(oauthScopes, ","), Endpoint: oauth2.Endpoint{ AuthURL: oauthAuthURL, TokenURL: oauthTokenURL, }, RedirectURL: oauthRedirectURI, } arguments := &Arguments{ DatabasePath: *databasePath, TemplatePath: *templatePath, StaticPath: *staticPath, CloudflareToken: cloudflareToken, CloudflareZone: cloudflareZone, Port: *port, Server: *server, Migrate: *migrate, Scheduler: *scheduler, Dns: *dns, DnsPort: *dnsPort, DnsResolvers: strings.Split(*dnsResolvers, ","), OauthConfig: oauthConfig, OauthUserInfoURI: oauthUserInfoURI, HcaptchaSecret: hcaptchaSecret, HcaptchaSiteKey: hcaptchaSiteKey, } return arguments, nil }