2021-04-24 22:54:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-04-30 23:13:57 +00:00
|
|
|
"path/filepath"
|
2021-04-24 22:54:56 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-04-30 23:13:57 +00:00
|
|
|
func (app *appContext) loadArgs(firstCall bool) {
|
|
|
|
if firstCall {
|
|
|
|
flag.Usage = helpFunc
|
|
|
|
help := flag.Bool("help", false, "prints this message.")
|
|
|
|
flag.BoolVar(help, "h", false, "SHORTHAND")
|
|
|
|
|
|
|
|
DATA = flag.String("data", app.dataPath, "alternate path to data directory.")
|
|
|
|
flag.StringVar(DATA, "d", app.dataPath, "SHORTHAND")
|
|
|
|
CONFIG = flag.String("config", app.configPath, "alternate path to config file.")
|
|
|
|
flag.StringVar(CONFIG, "c", app.configPath, "SHORTHAND")
|
|
|
|
HOST = flag.String("host", "", "alternate address to host web ui on.")
|
|
|
|
PORT = flag.Int("port", 0, "alternate port to host web ui on.")
|
|
|
|
flag.IntVar(PORT, "p", 0, "SHORTHAND")
|
|
|
|
DEBUG = flag.Bool("debug", false, "Enables debug logging.")
|
|
|
|
PPROF = flag.Bool("pprof", false, "Exposes pprof profiler on /debug/pprof.")
|
|
|
|
SWAGGER = flag.Bool("swagger", false, "Enable swagger at /swagger/index.html")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
if *help {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
if *SWAGGER {
|
|
|
|
os.Setenv("SWAGGER", "1")
|
|
|
|
}
|
|
|
|
if *DEBUG {
|
|
|
|
os.Setenv("DEBUG", "1")
|
|
|
|
}
|
|
|
|
if *PPROF {
|
|
|
|
os.Setenv("PPROF", "1")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("SWAGGER") == "1" {
|
|
|
|
*SWAGGER = true
|
|
|
|
}
|
|
|
|
if os.Getenv("DEBUG") == "1" {
|
|
|
|
*DEBUG = true
|
|
|
|
}
|
|
|
|
if os.Getenv("PPROF") == "1" {
|
|
|
|
*PPROF = true
|
|
|
|
}
|
|
|
|
// attempt to apply command line flags correctly
|
|
|
|
if app.configPath == *CONFIG && app.dataPath != *DATA {
|
|
|
|
app.dataPath = *DATA
|
|
|
|
app.configPath = filepath.Join(app.dataPath, "config.ini")
|
|
|
|
} else if app.configPath != *CONFIG && app.dataPath == *DATA {
|
|
|
|
app.configPath = *CONFIG
|
|
|
|
} else {
|
|
|
|
app.configPath = *CONFIG
|
|
|
|
app.dataPath = *DATA
|
|
|
|
}
|
|
|
|
|
|
|
|
// Previously used for self-restarts but leaving them here as they might be useful.
|
|
|
|
if v := os.Getenv("JFA_CONFIGPATH"); v != "" {
|
|
|
|
app.configPath = v
|
|
|
|
}
|
|
|
|
if v := os.Getenv("JFA_DATAPATH"); v != "" {
|
|
|
|
app.dataPath = v
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv("JFA_CONFIGPATH", app.configPath)
|
|
|
|
os.Setenv("JFA_DATAPATH", app.dataPath)
|
|
|
|
}
|
|
|
|
|
2023-06-11 14:48:27 +00:00
|
|
|
/*
|
|
|
|
Adds start/stop/systemd to help message, and
|
|
|
|
|
2021-04-24 22:54:56 +00:00
|
|
|
also gets rid of usage for shorthand flags, and merge them with the full-length one.
|
|
|
|
implementation is 🤢, will clean this up eventually.
|
2023-06-11 14:48:27 +00:00
|
|
|
|
2021-04-24 22:54:56 +00:00
|
|
|
-h SHORTHAND
|
|
|
|
-help
|
|
|
|
prints this message.
|
2023-06-11 14:48:27 +00:00
|
|
|
|
2021-04-24 22:54:56 +00:00
|
|
|
becomes:
|
2023-06-11 14:48:27 +00:00
|
|
|
|
2021-04-24 22:54:56 +00:00
|
|
|
-help, -h
|
|
|
|
prints this message.
|
|
|
|
*/
|
|
|
|
func helpFunc() {
|
2023-06-11 14:48:27 +00:00
|
|
|
fmt.Fprint(stderr, `Usage of jfa-go:
|
2021-04-24 22:54:56 +00:00
|
|
|
start
|
|
|
|
start jfa-go as a daemon and run in the background.
|
|
|
|
stop
|
|
|
|
stop a daemonized instance of jfa-go.
|
|
|
|
systemd
|
|
|
|
generate a systemd .service file.
|
|
|
|
`)
|
|
|
|
shortHands := []string{"-help", "-data", "-config", "-port"}
|
|
|
|
var b bytes.Buffer
|
|
|
|
// Write defaults into buffer then remove any shorthands
|
|
|
|
flag.CommandLine.SetOutput(&b)
|
|
|
|
flag.PrintDefaults()
|
2023-06-11 14:48:27 +00:00
|
|
|
flag.CommandLine.SetOutput(stderr)
|
2021-04-24 22:54:56 +00:00
|
|
|
scanner := bufio.NewScanner(&b)
|
|
|
|
out := ""
|
|
|
|
line := scanner.Text()
|
|
|
|
eof := !scanner.Scan()
|
|
|
|
lastLine := false
|
|
|
|
for !eof || lastLine {
|
|
|
|
nextline := scanner.Text()
|
|
|
|
start := 0
|
|
|
|
if len(nextline) != 0 {
|
|
|
|
for nextline[start] == ' ' && start < len(nextline) {
|
|
|
|
start++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.Contains(line, "SHORTHAND") || (len(nextline) != 0 && strings.Contains(nextline, "SHORTHAND") && nextline[start] != '-') {
|
|
|
|
line = nextline
|
|
|
|
if lastLine {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
eof := !scanner.Scan()
|
|
|
|
if eof {
|
|
|
|
lastLine = true
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// if !strings.Contains(line, "SHORTHAND") && !(strings.Contains(nextline, "SHORTHAND") && !strings.Contains(nextline, "-")) {
|
|
|
|
match := false
|
|
|
|
for i, c := range line {
|
|
|
|
if c != '-' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, s := range shortHands {
|
|
|
|
if i+len(s) <= len(line) && line[i:i+len(s)] == s {
|
|
|
|
out += line[:i+len(s)] + ", " + s[:2] + line[i+len(s):] + "\n"
|
|
|
|
match = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
out += line + "\n"
|
|
|
|
}
|
|
|
|
line = nextline
|
|
|
|
if lastLine {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
eof := !scanner.Scan()
|
|
|
|
if eof {
|
|
|
|
lastLine = true
|
|
|
|
}
|
|
|
|
}
|
2023-06-11 14:48:27 +00:00
|
|
|
fmt.Fprint(stderr, out)
|
2021-04-24 22:54:56 +00:00
|
|
|
}
|