-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (63 loc) · 1.48 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
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/kardianos/service"
)
var logger service.Logger
var srvConfig *config
var svcConfig *service.Config
type program struct{}
func (p *program) Start(s service.Service) error {
// Start should not block. Do the actual work async.
go p.run()
return nil
}
func (p *program) run() {
logger.Info("Starting Server...")
err := BuildTftpServer(srvConfig.Directory, srvConfig.Readonly).ListenAndServe(srvConfig.ConnectionString) // blocks until s.Shutdown() is called
if err != nil {
logger.Errorf("Server: %v\n", err)
}
}
func (p *program) Stop(s service.Service) error {
return nil
}
func main() {
srvConfig = argparse()
svcConfig = &service.Config{
Name: "just-a-tftp",
DisplayName: "just-a-tftp",
Description: "just-a-tftp",
Arguments: strings.Split(fmt.Sprintf("--ro=%s --dir=%s", strconv.FormatBool(srvConfig.Readonly), srvConfig.Directory), " "),
WorkingDirectory: srvConfig.Directory,
Executable: os.Args[0],
}
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
for _, command := range []string{"install", "uninstall"} {
for _, arg := range os.Args {
if command == strings.ToLower(arg) {
err = service.Control(s, command)
if err != nil {
log.Fatal(err)
}
return
}
}
}
logger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
err = s.Run()
if err != nil {
logger.Error(err)
}
}