-
Notifications
You must be signed in to change notification settings - Fork 664
/
service.go
174 lines (151 loc) · 3.34 KB
/
service.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/share"
)
// **** DEPRECATED ****
var serviceCmd = &cobra.Command{
Use: "service",
Short: L("Service manager"),
Long: L("Service manager"),
Run: func(cmd *cobra.Command, args []string) {
loadService(config.Conf)
command := "ps"
if len(args) > 0 {
command = args[0]
}
if len(args) > 1 {
args = args[1:]
} else {
args = []string{}
}
switch command {
case "ps":
services()
break
case "install", "start", "stop", "remove", "status":
serviceManage(command, args)
break
default:
serviceHelp()
}
},
}
func serviceHelp() {
fmt.Println("Usage:")
fmt.Println(" ", color.GreenString(L("%s service [ps|start|stop|install|status] [all|name]"), share.BUILDNAME))
fmt.Println("")
}
func serviceManageAll(command string) {
for name := range gou.Services {
if name == "all" {
continue
}
serviceManage(command, []string{name})
}
}
func serviceManage(command string, args []string) {
if len(args) == 0 {
serviceHelp()
os.Exit(1)
}
defer func() {
err := exception.Catch(recover())
if err != nil {
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
}
}()
name := args[0]
if name == "all" {
serviceManageAll(command)
return
}
srv := gou.SelectService(name)
var status = ""
var err error
switch command {
case "install":
status, err = srv.Install()
break
case "start":
status, err = srv.Start()
break
case "stop":
status, err = srv.Stop()
break
case "remove":
status, err = srv.Remove()
break
case "status":
status, err = srv.Status()
break
}
if err != nil {
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
}
fmt.Println(status)
}
func services() {
for name, srv := range gou.Services {
status, err := srv.Status()
if err != nil {
status = err.Error()
}
fmt.Println(
color.GreenString("%s", name),
srv.Name, "....",
statusInfo(status),
)
}
fmt.Println("")
}
func statusInfo(message string) string {
if strings.Contains(message, "stopped") {
return statusStop()
} else if strings.Contains(message, "not installed") {
return statusNotInstalled()
} else if strings.Contains(message, "running") {
return statusRunning()
}
return statusUnknown()
}
func statusUnknown() string {
return fmt.Sprintf("[ %s ]", color.WhiteString("UNKNOWN"))
}
func statusRunning() string {
return fmt.Sprintf("[ %s ]", color.GreenString("RUNNING"))
}
func statusNotInstalled() string {
return fmt.Sprintf("[ %s ]", color.YellowString("NOT INSTALLED"))
}
func statusStop() string {
return fmt.Sprintf("[ %s ]", color.RedString("STOPED"))
}
// Load 加载API
func loadService(cfg config.Config) {
var root = filepath.Join(cfg.Root, "services")
loadServiceFrom(root, "")
}
func loadServiceFrom(dir string, prefix string) error {
if share.DirNotExists(dir) {
return fmt.Errorf("%s does not exists", dir)
}
err := share.Walk(dir, ".srv.json", func(root, filename string) {
name := prefix + share.SpecName(root, filename)
content := share.ReadFile(filename)
_, err := gou.LoadService(string(content), name)
if err != nil {
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
os.Exit(1)
}
})
return err
}