66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package global
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
var Args = func() map[string]string {
|
|
args := map[string]string{}
|
|
|
|
index := 0
|
|
for index < len(os.Args[1:]) {
|
|
arg := os.Args[index + 1]
|
|
if !strings.HasPrefix(arg, "-") {
|
|
fmt.Printf("FATAL: Parameters must follow an argument (%s).\n", arg)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if index + 3 > len(os.Args) || strings.HasPrefix(os.Args[index + 2], "-") {
|
|
args[arg[1:]] = "true"
|
|
index += 1
|
|
continue
|
|
}
|
|
|
|
val := os.Args[index + 2]
|
|
args[arg[1:]] = val
|
|
// fmt.Printf("%s: %s\n", arg[1:], val)
|
|
index += 2
|
|
}
|
|
|
|
return args
|
|
}()
|
|
|
|
var HTTP_DOMAIN = func() string {
|
|
domain := os.Getenv("ARIMELODY_HTTP_DOMAIN")
|
|
if domain == "" {
|
|
return "https://arimelody.me"
|
|
}
|
|
return domain
|
|
}()
|
|
|
|
var DATA_DIR = func() string {
|
|
dir, err := filepath.Abs(os.Getenv("ARIMELODY_DATA_DIR"))
|
|
if err != nil {
|
|
fmt.Printf("FATAL: Failed to get working directory: %s\n", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
if dir != "" {
|
|
os.MkdirAll(dir, os.ModePerm)
|
|
} else {
|
|
var err error
|
|
dir, err = os.Getwd()
|
|
if err != nil {
|
|
fmt.Printf("FATAL: Failed to get working directory: %s\n", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
return dir
|
|
}()
|
|
|
|
var DB *sqlx.DB
|