-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
109 lines (86 loc) · 2.93 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
// Copyright 2020 David Sheets
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"log"
"net"
"os"
)
const certFile string = "cert.pem"
const keyFile string = "key.pem"
const jsonFile string = "initservices.json"
const headersDir string = "initservices.headers"
var version string = "dev"
func main() {
exitCode := 0
if len(os.Args) < 2 {
usageError(os.Args[0], "At least one argument required")
}
if os.Args[1] == "-h" || os.Args[1] == "--help" || os.Args[1] == "/?" {
printUsage(os.Args[0])
os.Exit(0)
}
if os.Args[1] == "-s" {
if len(os.Args) < 3 {
usageError(os.Args[0], "Service action name missing")
}
if os.Args[2] == "install" || os.Args[2] == "run" {
if len(os.Args) < 4 {
usageError(os.Args[0], "Bind address missing")
}
exitCode = execServiceAction(configuration{
bindAddress: parseIPOrDie(os.Args[3]),
serviceStarted: make(chan error),
}, os.Args[2])
} else {
exitCode = execServiceAction(configuration{
serviceStarted: make(chan error),
}, os.Args[2])
}
} else {
run(configuration{bindAddress: parseIPOrDie(os.Args[1])})
}
os.Exit(exitCode)
}
func parseIPOrDie(ipStr string) net.IP {
address := net.ParseIP(ipStr)
if address == nil {
log.Fatalf("Could not parse %s as IP address", ipStr)
}
return address
}
func usageError(cmdName string, message string) {
eprintf("%s:\n\n", message)
printUsage(cmdName)
os.Exit(1)
}
func eprintf(fmtStr string, args ...interface{}) {
fmt.Fprintf(os.Stderr, fmtStr, args...)
}
func printUsage(cmdName string) {
eprintf("lgtv-sdp version %s\n\n", version)
eprintf("%s BIND_ADDRESS\n", cmdName)
eprintf(" start the server listening on IP address BIND_ADDRESS\n\n")
eprintf("%s -s install BIND_ADDRESS\n", cmdName)
eprintf(" install the server as a system service and start it\n\n")
eprintf("%s -s ACTION\n", cmdName)
eprintf(" perform the service action ACTION\n")
eprintf(" service actions are:\n")
eprintf(" uninstall: removes the server as a system service\n")
eprintf(" start: starts the system service\n")
eprintf(" restart: restarts the system service\n")
eprintf(" stop: stops the system service\n")
eprintf(" status: return the status of the service via stdout and exit code\n\n")
eprintf("%s -h | %s --help | %s /?\n", cmdName, cmdName, cmdName)
eprintf(" print this usage information\n\n")
}