summaryrefslogtreecommitdiff
path: root/args/args.go
diff options
context:
space:
mode:
Diffstat (limited to 'args/args.go')
-rw-r--r--args/args.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/args/args.go b/args/args.go
new file mode 100644
index 0000000..458d36e
--- /dev/null
+++ b/args/args.go
@@ -0,0 +1,87 @@
+package args
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "sync"
+)
+
+type Arguments struct {
+ DatabasePath string
+ TemplatePath string
+ StaticPath string
+
+ Migrate bool
+ Scheduler bool
+
+ HttpSmsEndpoint 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")
+
+ 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,
+ }
+ err := validateArgs(args)
+ if err != nil {
+ return nil, err
+ }
+
+ return args, nil
+}