-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
123 lines (97 loc) · 2.42 KB
/
main.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"embed"
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"tsp-web/api"
"tsp-web/internal/args"
userconf "tsp-web/internal/user-conf"
utils "tsp-web/internal"
log "github.com/sirupsen/logrus"
)
//go:embed web/*
var static embed.FS
//go:embed .VERSION
var version string
const maxPort uint64 = 65535
func parseArgs() (args.TspWebArgs, error) {
envPort, _ := strconv.ParseUint(utils.Getenv("TSP_WEB_PORT", "3000"), 10, 16)
TsBin := flag.String("ts-bin", utils.Getenv("TSP_WEB_TS_BIN", "tsp"), "The binary for tsp")
portArg := flag.Uint64("port", envPort, "The port for tsp-web")
logLevel := flag.String("log-level", utils.Getenv("TSP_WEB_LOG_LEVEL", "info"), "The log level for tsp-web")
noColor := flag.Bool("no-color", false, "Disable colorized output")
host := flag.String("host", utils.Getenv("TSP_WEB_HOSTNAME", "localhost"), "The host for tsp-web")
version := flag.Bool("version", false, "Print the version and exit")
flag.Parse()
if *portArg > maxPort {
log.Errorf("invalid value \"%d\" for flag -port: value out of range", *portArg)
flag.Usage()
os.Exit(1)
}
Port := uint16(*portArg)
return args.TspWebArgs{
TsBin: *TsBin,
Port: Port,
LogLevel: *logLevel,
NoColor: *noColor,
Host: *host,
Version: *version,
}, nil
}
func setLogLevel(logLevel string) {
switch logLevel {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
default:
log.SetLevel(log.InfoLevel)
}
}
func ensureTsBinExists(tsBin string) {
cmd := exec.Command(tsBin)
err := cmd.Run()
if err != nil {
log.Fatalf(`Error running tsp binary: %s
Troubleshooting:
1. Did you install tsp?
# Debian/Ubuntu
$ sudo apt install task-spooler
# Mac
$ brew install task-spooler
2. Did you set the correct path to tsp with the --ts-bin flag or TSP_WEB_TS_BIN env variable?
# Mac
$ tsp-web --ts-bin ts
`, err)
}
}
func main() {
api.Static = static
args, err := parseArgs()
if args.Version {
fmt.Println(version)
os.Exit(0)
}
setLogLevel(args.LogLevel)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
DisableColors: args.NoColor,
})
ensureTsBinExists(args.TsBin)
userconf.Load(args)
go userconf.StartWatcher(args)
err = api.Run(args)
if errors.Is(err, http.ErrServerClosed) {
log.Info("server closed")
} else if err != nil {
log.Fatal("error starting server: ", err)
}
os.Exit(0)
}