forked from jumpserver/koko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
koko.go
88 lines (76 loc) · 1.56 KB
/
koko.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"syscall"
"github.com/sevlyar/go-daemon"
"github.com/meowgen/koko/pkg/koko"
)
func startAsDaemon() {
ctx := &daemon.Context{
PidFileName: "/tmp/koko.pid",
PidFilePerm: 0644,
Umask: 027,
WorkDir: "./",
}
child, err := ctx.Reborn()
if err != nil {
log.Fatalf("run failed: %v", err)
}
if child != nil {
return
}
defer ctx.Release()
koko.RunForever(configPath)
}
var (
Buildstamp = ""
Githash = ""
Goversion = ""
pidPath = "/tmp/koko.pid"
daemonFlag = false
runSignalFlag = "start"
infoFlag = false
configPath = ""
)
func init() {
flag.BoolVar(&daemonFlag, "d", false, "start as Daemon")
flag.StringVar(&runSignalFlag, "s", "start", "start | stop")
flag.StringVar(&configPath, "f", "config.yml", "config.yml path")
flag.BoolVar(&infoFlag, "V", false, "version info")
}
func main() {
flag.Parse()
if infoFlag {
fmt.Printf("Version: %s\n", koko.Version)
fmt.Printf("Git Commit Hash: %s\n", Githash)
fmt.Printf("UTC Build Time : %s\n", Buildstamp)
fmt.Printf("Go Version: %s\n", Goversion)
return
}
if runSignalFlag == "stop" {
pid, err := ioutil.ReadFile(pidPath)
if err != nil {
log.Fatal("File not exist")
return
}
pidInt, _ := strconv.Atoi(string(pid))
err = syscall.Kill(pidInt, syscall.SIGTERM)
if err != nil {
log.Fatalf("Stop failed: %v", err)
} else {
_ = os.Remove(pidPath)
}
return
}
switch {
case daemonFlag:
startAsDaemon()
default:
koko.RunForever(configPath)
}
}