hatecomputers.club/args/args.go

96 lines
2.6 KiB
Go
Raw Normal View History

2024-03-26 18:00:05 -04:00
package args
import (
"errors"
"flag"
"os"
2024-03-27 17:02:31 -04:00
"strings"
"golang.org/x/oauth2"
2024-03-26 18:00:05 -04:00
)
type Arguments struct {
DatabasePath string
TemplatePath string
StaticPath string
CloudflareToken string
CloudflareZone string
Port int
Server bool
Migrate bool
Scheduler bool
2024-03-27 17:02:31 -04:00
OauthConfig *oauth2.Config
OauthUserInfoURI string
2024-03-26 18:00:05 -04:00
}
func GetArgs() (*Arguments, error) {
port := flag.Int("port", 8080, "Port to listen on")
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")
2024-03-27 17:38:53 -04:00
scheduler := flag.Bool("scheduler", false, "Run scheduled jobs via cron")
2024-03-26 18:00:05 -04:00
server := flag.Bool("server", false, "Run the server")
migrate := flag.Bool("migrate", false, "Run the migrations")
flag.Parse()
cloudflareToken := os.Getenv("CLOUDFLARE_TOKEN")
cloudflareZone := os.Getenv("CLOUDFLARE_ZONE")
2024-03-27 17:02:31 -04:00
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")
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"},
2024-03-26 18:00:05 -04:00
}
2024-03-27 17:02:31 -04:00
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,
2024-03-26 18:00:05 -04:00
}
arguments := &Arguments{
DatabasePath: *databasePath,
TemplatePath: *templatePath,
StaticPath: *staticPath,
CloudflareToken: cloudflareToken,
CloudflareZone: cloudflareZone,
Port: *port,
Server: *server,
Migrate: *migrate,
Scheduler: *scheduler,
2024-03-27 17:02:31 -04:00
OauthConfig: oauthConfig,
OauthUserInfoURI: oauthUserInfoURI,
2024-03-26 18:00:05 -04:00
}
return arguments, nil
}