summaryrefslogtreecommitdiff
path: root/args/args.go
blob: ae18865e2fbe0a861fdd4ce68dbe8d6b1e4a3dd6 (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 (
	"flag"
	"fmt"
	"os"
	"sync"
)

type Arguments struct {
	DatabasePath string
	TemplatePath string
	StaticPath   string

	Migrate   bool
	Scheduler bool

	HttpSmsEndpoint string

	NtfyEndpoint string

	Port   int
	Server bool
}

func isDirectory(path string) (bool, error) {
	fileInfo, err := os.Stat(path)
	if err != nil {
		return false, err
	}

	return fileInfo.IsDir(), err
}

func validateArgs(args *Arguments) error {
	templateIsDir, err := isDirectory(args.TemplatePath)
	if err != nil || !templateIsDir {
		return fmt.Errorf("template path is not an accessible directory %s", err)
	}
	staticPathIsDir, err := isDirectory(args.StaticPath)
	if err != nil || !staticPathIsDir {
		return fmt.Errorf("static path is not an accessible directory %s", err)
	}
	return nil
}

var lock = &sync.Mutex{}
var args *Arguments

func GetArgs() (*Arguments, error) {
	lock.Lock()
	defer lock.Unlock()

	if args != nil {
		return args, nil
	}

	databasePath := flag.String("database-path", "./phoneof.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")

	httpSmsEndpoint := flag.String("httpsms-endpoint", "https://httpsms.com", "HTTPSMS endpoint")

	ntfyEndpoint := flag.String("ntfy-endpoint", "https://ntfy.simponic.hatecomputers.club", "NTFY endpoint")

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

	flag.Parse()

	args = &Arguments{
		DatabasePath:    *databasePath,
		TemplatePath:    *templatePath,
		StaticPath:      *staticPath,
		Port:            *port,
		Server:          *server,
		Migrate:         *migrate,
		Scheduler:       *scheduler,
		HttpSmsEndpoint: *httpSmsEndpoint,
		NtfyEndpoint:    *ntfyEndpoint,
	}
	err := validateArgs(args)
	if err != nil {
		return nil, err
	}

	return args, nil
}