-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathps.go
More file actions
187 lines (149 loc) · 3.97 KB
/
ps.go
File metadata and controls
187 lines (149 loc) · 3.97 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
package parser
import (
"os"
docopt "github.com/docopt/docopt-go"
"github.com/drycc/workflow-cli/cmd"
"golang.org/x/exp/slices"
)
// Ps routes ps commands to their specific function.
func Ps(argv []string, cmdr cmd.Commander) error {
usage := `
Valid commands for processes:
ps:list list application processes
ps:logs print the logs for a container
ps:exec execute a command in a container
ps:describe print a detailed description of the selected process
ps:delete delete the selected processes
Use 'drycc help [command]' to learn more.
`
switch argv[0] {
case "ps:list":
return psList(argv, cmdr)
case "ps:logs":
return psLogs(argv, cmdr)
case "ps:exec":
return psExec(argv, cmdr)
case "ps:describe":
return psDescribe(argv, cmdr)
case "ps:delete":
return psDelete(argv, cmdr)
default:
if printHelp(argv, usage) {
return nil
}
if argv[0] == "ps" {
argv[0] = "ps:list"
return psList(argv, cmdr)
}
PrintUsage(cmdr)
return nil
}
}
func psList(argv []string, cmdr cmd.Commander) error {
usage := `
Lists processes servicing an application.
Usage: drycc ps:list [options]
Options:
-a --app=<app>
the uniquely identifiable name for the application.
`
args, err := docopt.ParseArgs(usage, argv, "")
if err != nil {
return err
}
// The 1000 is fake for now until API understands limits
return cmdr.PsList(safeGetString(args, "--app"), 1000)
}
func psLogs(argv []string, cmdr cmd.Commander) error {
usage := `
Print the logs for a container in a pod or specified resource.
Usage: drycc ps:logs <pod> [options]
Arguments:
<pod> the pod name for the application.
Options:
-a --app=<app>
the uniquely identifiable name for the application.
-n --lines=<lines>
the number of lines to display, default to 300 lines, -1 showing all log lines
-f --follow
specify if the logs should be streamed.
-c --container=<container>
print the logs of this container.
`
args, err := docopt.ParseArgs(usage, argv, "")
if err != nil {
return err
}
app := safeGetString(args, "--app")
lines := safeGetInt(args, "--lines")
if lines < 0 {
lines = -1
} else if lines == 0 {
lines = 300
}
follow := safeGetBool(args, "--follow")
podID := safeGetString(args, "<pod>")
container := safeGetString(args, "--container")
return cmdr.PsLogs(app, podID, lines, follow, container)
}
func psExec(argv []string, cmdr cmd.Commander) error {
usage := `
Execute a command in a container.
Usage: drycc ps:exec <pod> [options] -- <command>...
Arguments:
<pod> the pod name for the application.
Options:
-a --app=<app>
the uniquely identifiable name for the application.
-t --tty
stdin is a TTY.
-i --stdin
pass stdin to the container.
`
args, err := docopt.ParseArgs(usage, argv, "")
if err != nil {
return err
}
app := safeGetString(args, "--app")
pod := safeGetString(args, "<pod>")
tty := args["--tty"].(bool)
stdin := args["--stdin"].(bool)
index := slices.Index(os.Args, "--")
command := os.Args[index+1:]
return cmdr.PsExec(app, pod, tty, stdin, command)
}
func psDescribe(argv []string, cmdr cmd.Commander) error {
usage := `
Print a detailed description of the selected process.
Usage: drycc ps:describe <pod> [options]
Arguments:
<pod> the pod name for the application.
Options:
-a --app=<app>
the uniquely identifiable name for the application.
`
args, err := docopt.ParseArgs(usage, argv, "")
if err != nil {
return err
}
app := safeGetString(args, "--app")
pod := safeGetString(args, "<pod>")
return cmdr.PsDescribe(app, pod)
}
func psDelete(argv []string, cmdr cmd.Commander) error {
usage := `
Delete the selected processes.
Usage: drycc ps:delete <pod>... [options]
Arguments:
<pod> the pod name for the application.
Options:
-a --app=<app>
the uniquely identifiable name for the application.
`
args, err := docopt.ParseArgs(usage, argv, "")
if err != nil {
return err
}
app := safeGetString(args, "--app")
return cmdr.PsDelete(app, args["<pod>"].([]string))
}