V2bX/cmd/action_linux.go

111 lines
2.7 KiB
Go
Raw Normal View History

2023-05-23 22:26:14 -04:00
package cmd
import (
"fmt"
"github.com/Yuzuki616/V2bX/common/exec"
2023-05-23 22:26:14 -04:00
"github.com/spf13/cobra"
"time"
)
var (
startCommand = cobra.Command{
Use: "start",
Short: "Start V2bX service",
Run: startHandle,
}
stopCommand = cobra.Command{
Use: "stop",
Short: "Stop V2bX service",
Run: stopHandle,
}
restartCommand = cobra.Command{
Use: "restart",
Short: "Restart V2bX service",
Run: restartHandle,
}
logCommand = cobra.Command{
Use: "log",
Short: "Output V2bX log",
Run: func(_ *cobra.Command, _ []string) {
exec.RunCommandStd("journalctl", "-u", "V2bX.service", "-e", "--no-pager", "-f")
2023-05-23 22:26:14 -04:00
},
}
)
func init() {
command.AddCommand(&startCommand)
command.AddCommand(&stopCommand)
command.AddCommand(&restartCommand)
command.AddCommand(&logCommand)
}
func startHandle(_ *cobra.Command, _ []string) {
r, err := checkRunning()
if err != nil {
fmt.Println(Err("check status error: ", err))
fmt.Println(Err("V2bX启动失败"))
return
}
if r {
fmt.Println(Ok("V2bX已运行无需再次启动如需重启请选择重启"))
}
_, err = exec.RunCommandByShell("systemctl start V2bX.service")
2023-05-23 22:26:14 -04:00
if err != nil {
fmt.Println(Err("exec start cmd error: ", err))
fmt.Println(Err("V2bX启动失败"))
return
}
time.Sleep(time.Second * 3)
r, err = checkRunning()
if err != nil {
fmt.Println(Err("check status error: ", err))
fmt.Println(Err("V2bX启动失败"))
}
if !r {
fmt.Println(Err("V2bX可能启动失败请稍后使用 V2bX log 查看日志信息"))
return
}
fmt.Println(Ok("V2bX 启动成功,请使用 V2bX log 查看运行日志"))
}
func stopHandle(_ *cobra.Command, _ []string) {
_, err := exec.RunCommandByShell("systemctl stop V2bX.service")
2023-05-23 22:26:14 -04:00
if err != nil {
fmt.Println(Err("exec stop cmd error: ", err))
fmt.Println(Err("V2bX停止失败"))
return
}
time.Sleep(2 * time.Second)
r, err := checkRunning()
if err != nil {
fmt.Println(Err("check status error:", err))
fmt.Println(Err("V2bX停止失败"))
return
}
if r {
fmt.Println(Err("V2bX停止失败可能是因为停止时间超过了两秒请稍后查看日志信息"))
return
}
fmt.Println(Ok("V2bX 停止成功"))
}
func restartHandle(_ *cobra.Command, _ []string) {
_, err := exec.RunCommandByShell("systemctl restart V2bX.service")
2023-05-23 22:26:14 -04:00
if err != nil {
fmt.Println(Err("exec restart cmd error: ", err))
fmt.Println(Err("V2bX重启失败"))
return
}
r, err := checkRunning()
if err != nil {
fmt.Println(Err("check status error: ", err))
fmt.Println(Err("V2bX重启失败"))
return
}
if !r {
fmt.Println(Err("V2bX可能启动失败请稍后使用 V2bX log 查看日志信息"))
return
}
fmt.Println(Ok("V2bX重启成功"))
}