Files
EdgeNode/cmd/edge-node/main.go

64 lines
1.3 KiB
Go
Raw Normal View History

2020-07-21 11:18:47 +08:00
package main
import (
2020-09-09 18:53:53 +08:00
"fmt"
"github.com/TeaOSLab/EdgeNode/internal/apps"
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
2020-07-21 11:18:47 +08:00
"github.com/TeaOSLab/EdgeNode/internal/nodes"
2020-10-28 11:19:06 +08:00
"github.com/iwind/TeaGo/Tea"
2020-07-21 11:18:47 +08:00
_ "github.com/iwind/TeaGo/bootstrap"
2020-10-28 11:19:06 +08:00
"github.com/iwind/TeaGo/types"
"io/ioutil"
"os"
2020-10-28 11:19:06 +08:00
"syscall"
2020-07-21 11:18:47 +08:00
)
func main() {
2020-09-09 18:53:53 +08:00
app := apps.NewAppCmd().
Version(teaconst.Version).
Product(teaconst.ProductName).
2021-01-12 11:48:38 +08:00
Usage(teaconst.ProcessName + " [-v|start|stop|restart|quit|test|service|daemon]")
2020-09-09 18:53:53 +08:00
app.On("test", func() {
err := nodes.NewNode().Test()
if err != nil {
_, _ = os.Stderr.WriteString(err.Error())
}
})
2021-01-12 10:56:30 +08:00
app.On("daemon", func() {
2021-01-12 11:48:38 +08:00
nodes.NewNode().Daemon()
})
app.On("service", func() {
err := nodes.NewNode().InstallSystemService()
if err != nil {
fmt.Println("[ERROR]install failed: " + err.Error())
2021-01-12 10:56:30 +08:00
}
2021-01-12 11:48:38 +08:00
fmt.Println("done")
2021-01-12 10:56:30 +08:00
})
2020-10-28 11:19:06 +08:00
app.On("quit", func() {
pidFile := Tea.Root + "/bin/pid"
data, err := ioutil.ReadFile(pidFile)
if err != nil {
fmt.Println("[ERROR]quit failed: " + err.Error())
return
}
pid := types.Int(string(data))
if pid == 0 {
fmt.Println("[ERROR]quit failed: pid=0")
return
}
process, err := os.FindProcess(pid)
if err != nil {
return
}
if process != nil {
_ = process.Signal(syscall.SIGQUIT)
}
})
2020-09-09 18:53:53 +08:00
app.Run(func() {
node := nodes.NewNode()
node.Start()
})
2020-07-21 11:18:47 +08:00
}