-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (120 loc) · 3.76 KB
/
main.go
File metadata and controls
137 lines (120 loc) · 3.76 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"log"
"os"
"github.com/akamensky/argparse"
)
func main() {
// check program is run by go run or go build
isGoRun := false
for _, arg := range os.Args {
if arg == "run" {
isGoRun = true
}
}
fmt.Println("isGoRun", isGoRun)
if len(os.Args) == 2 {
if os.Args[1] == "daemon" {
SaveDaemonPid()
// set logs to file
// go
logFile, err := os.OpenFile(GetGpmLogFile(), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("Failed to open log file: %s", err)
}
defer logFile.Close()
// Redirect stdout and stderr to the log file
log.SetOutput(logFile)
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
defer DeleteDaemonPid()
defer logFile.Close()
StartIpcServer()
return
}
}
/*
This part will be moved to cli project
*/
// create new parser object
parser := argparse.NewParser("GPM", "A simple process manager made in Go")
// start command which get command
startCmd := parser.NewCommand("start", "Start a service")
startFileArg := startCmd.StringPositionalWithName("file", &argparse.Options{Required: true, Help: "The file to run"})
startName := startCmd.String("", "name", &argparse.Options{Required: false, Help: "The name of the service"})
startWatch := startCmd.Flag("", "watch", &argparse.Options{Required: false, Help: "Watch the service for changes"})
startForce := startCmd.Flag("", "force", &argparse.Options{Required: false, Help: "Force the service to start"})
// stop command which get command
stopCmd := parser.NewCommand("stop", "Stop a service")
stopName := stopCmd.StringPositionalWithName("name", &argparse.Options{Required: true, Help: "The name of the service"})
// restart command which get command
restartCmd := parser.NewCommand("restart", "Restart a service")
// status command which get command
statusCmd := parser.NewCommand("status", "Get the status of a service")
// list command which get command
listCmd := parser.NewCommand("list", "List all services")
// logs command which get command
logsCmd := parser.NewCommand("logs", "Get the logs of a service")
// config command which get command
configCmd := parser.NewCommand("config", "Get the config of a service")
// test command which get command
testCmd := parser.NewCommand("test", "not for production")
_ = testCmd // remove unused warning for testCmd variable
// x := testCmd.String("m", "memory", &argparse.Options{Required: false, Help: "memory dump"})
// parse command arguments
// start command arguments
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
return
}
if !DoesDaemonWork() {
StartDaemon()
}
Client := ConnectToIpcServer()
if startCmd.Happened() {
fmt.Println("start command")
fmt.Println("command: ", *startFileArg)
fmt.Println("name: ", *startName)
fmt.Println("watch: ", *startWatch)
fmt.Println("force: ", *startForce)
// get working directory
// if *path == "" {
// *path = "."
// }
cwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
return
}
if *startName == "" {
*startName = *startFileArg
}
var reply Reply
// send the command to the server
Client.Call("Server.Start", &Service{Name: *startName, Path: cwd, Command: *startFileArg}, &reply)
log.Println(reply.Message)
}
if stopCmd.Happened() {
fmt.Println("name: ", *stopName)
var reply Reply
// send the command to the server
Client.Call("Server.Stop", &Service{Name: *stopName}, &reply)
log.Println(reply.Message)
}
if restartCmd.Happened() {
fmt.Println("restart command")
}
if statusCmd.Happened() {
fmt.Println("status command")
}
if listCmd.Happened() {
fmt.Println("list command")
}
if logsCmd.Happened() {
fmt.Println("logs command")
}
if configCmd.Happened() {
fmt.Println("config command")
}
}