-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvcmds.go
145 lines (122 loc) · 3.8 KB
/
svcmds.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
// svctl
// Copyright (C) 2015 Karol 'Kenji Takahashi' Woźniak
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package main
import (
"fmt"
"strings"
)
// cmdUp Defines the "up / start" action.
type cmdUp struct{}
func (c *cmdUp) Action() []byte {
return []byte("u")
}
func (c *cmdUp) Help() string {
return strings.TrimSpace(`
up NAMES... Starts service(s) with matching NAMES.
NAMES support globing with '*' and '?'.
`)
}
func (c *cmdUp) Names() []string {
return []string{"up", "start"}
}
// cmdDown Defines the "down / stop" action.
type cmdDown struct{}
func (c *cmdDown) Action() []byte {
return []byte("d")
}
func (c *cmdDown) Help() string {
return strings.TrimSpace(`
down NAMES... Stops service(s) with matching NAMES.
NAMES support globing with '*' and '?'.
`)
}
func (c *cmdDown) Names() []string {
return []string{"down", "stop"}
}
// cmdRestart Defines the "restart" action.
type cmdRestart struct{}
func (c *cmdRestart) Action() []byte {
return []byte("tcu")
}
func (c *cmdRestart) Help() string {
return strings.TrimSpace(`
restart NAMES... Restarts service(s) with matching NAMES.
NAMES support globing with '*' and '?'.
Waits up to 7 seconds for the service to get back up, then
reports TIMEOUT.
`)
}
func (c *cmdRestart) Names() []string {
return []string{"r", "restart"}
}
// cmdOnce Defines the "once" action.
type cmdOnce struct{}
func (c *cmdOnce) Action() []byte {
return []byte{'o'}
}
func (c *cmdOnce) Help() string {
return strings.TrimSpace(`
once NAMES... Starts service once and does not try to restart it if it stops.
NAMES support globing with '*' and '?'.
`)
}
func (c *cmdOnce) Names() []string {
return []string{"once"}
}
// cmdSignal Defines common struct for handling all actions
// that send a single *NIX signal to the process.
type cmdSignal struct {
action string
}
func (c *cmdSignal) Action() []byte {
if c.action == "" {
return nil
}
if c.action == "reload" {
return []byte{'h'}
}
return []byte{c.action[0]}
}
func (c *cmdSignal) Help() string {
m := map[byte]string{
'p': "STOP", 'c': "CONT", 'h': "HUP", 'r': "HUP", 'a': "ALRM", 'i': "INT",
'q': "QUIT", '1': "USR1", '2': "USR2", 't': "TERM", 'k': "KILL",
}
return fmt.Sprintf(strings.TrimSpace(`
%s NAMES... Sends signal '%s' to service(s) with matching NAMES.
%-[3]*s NAMES support globing with '*' and '?'.
`), c.action, m[c.action[0]], len(c.action), "")
}
func (c *cmdSignal) Names() []string {
return []string{
"pause", "cont", "hup", "reload", "alarm",
"interrupt", "quit", "1", "2", "term", "kill",
}
}
func (c *cmdSignal) Match(name string) bool {
for _, s := range c.Names() {
if name == s || name == s[0:1] {
c.action = s
return true
}
}
return false
}