summaryrefslogtreecommitdiff
path: root/args/args.go
blob: a360d57bb02684477395bedfb7e5b0530d73e425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package args

import (
	"errors"
	"flag"
	"os"
	"strings"

	"golang.org/x/oauth2"
)

type Arguments struct {
	DatabasePath    string
	TemplatePath    string
	StaticPath      string
	CloudflareToken string
	CloudflareZone  string
	Port            int
	Server          bool
	Migrate         bool

	OauthConfig      *oauth2.Config
	OauthUserInfoURI string
}

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")

	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")

	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"},
	}

	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,

		OauthConfig:      oauthConfig,
		OauthUserInfoURI: oauthUserInfoURI,
	}

	return arguments, nil
}