-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathps.go
More file actions
163 lines (143 loc) · 5.25 KB
/
ps.go
File metadata and controls
163 lines (143 loc) · 5.25 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
package parser
import (
"github.com/drycc/workflow-cli/internal/commands"
"github.com/drycc/workflow-cli/internal/completion"
"github.com/drycc/workflow-cli/internal/template"
"github.com/drycc/workflow-cli/pkg/i18n"
"github.com/spf13/cobra"
)
// NewPsCommand creates a command for managing processes.
func NewPsCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "ps",
Short: i18n.T("Manage processes inside an app"),
RunE: func(_ *cobra.Command, _ []string) error {
return cmdr.PsList(app, 1000)
},
}
cmd.PersistentFlags().StringVarP(&app, "app", "a", "", i18n.T("The uniquely identifiable name for the application"))
appCompletion := completion.AppCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("app", appCompletion.CompletionFunc)
cmd.AddCommand(psListCommand(cmdr))
cmd.AddCommand(psLogsCommand(cmdr))
cmd.AddCommand(psExecCommand(cmdr))
cmd.AddCommand(psDescribeCommand(cmdr))
cmd.AddCommand(psDeleteCommand(cmdr))
return cmd
}
func psListCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: i18n.T("List application pods"),
Long: i18n.T("Lists processes servicing an application"),
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
return cmdr.PsList(app, 1000)
},
}
return cmd
}
func psLogsCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
app string
lines int
follow bool
container string
previous bool
}
psCompletion := completion.PsCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "logs <pod>",
Args: cobra.ExactArgs(1),
Example: "drycc ps logs my-pod",
Short: i18n.T("Print the logs for a container"),
Long: i18n.T("Print the logs for a container in a pod or specified resource"),
ValidArgsFunction: psCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
podID := args[0]
if flags.lines < 0 {
flags.lines = -1
}
return cmdr.PsLogs(app, podID, flags.lines, flags.follow, flags.container, flags.previous)
},
}
cmd.Flags().IntVarP(&flags.lines, "lines", "l", 300, i18n.T("The number of lines to display, -1 showing all log lines"))
cmd.Flags().BoolVarP(&flags.follow, "follow", "f", false, i18n.T("Specify if the logs should be streamed"))
cmd.Flags().StringVar(&flags.container, "container", "", i18n.T("Print the logs of this container"))
cmd.Flags().BoolVarP(&flags.previous, "previous", "p", false, i18n.T("Print the logs for the previous instance of the container in a pod if it exists"))
cmd.Flags().SortFlags = false
return cmd
}
func psExecCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
app string
pod string
command []string
tty bool
stdin bool
}
psCompletion := completion.PsCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "exec <pod> [flags] -- <command>...",
Args: cobra.MinimumNArgs(1),
Example: template.CustomExample(
"drycc ps exec my-pod -it -- bash",
map[string]string{
"<pod>": i18n.T("The pod name for the application"),
},
),
Short: i18n.T("Execute a command in a container"),
ValidArgsFunction: psCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
flags.pod = args[0]
flags.command = args[1:]
return cmdr.PsExec(app, flags.pod, flags.tty, flags.stdin, flags.command)
},
}
// shortcuts exec has not app flag
cmd.Flags().StringVarP(&app, "app", "a", "", i18n.T("The uniquely identifiable name for the application"))
cmd.Flags().BoolVarP(&flags.tty, "tty", "t", false, i18n.T("Stdin is a TTY"))
cmd.Flags().BoolVarP(&flags.stdin, "stdin", "i", false, i18n.T("Pass stdin to the container"))
appCompletion := completion.AppCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("app", appCompletion.CompletionFunc)
return cmd
}
func psDescribeCommand(cmdr *commands.DryccCmd) *cobra.Command {
psCompletion := completion.PsCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "describe <pod>",
Args: cobra.ExactArgs(1),
Example: template.CustomExample(
"drycc ps describe my-pod",
map[string]string{
"<pod>": i18n.T("The pod name for the application"),
},
),
Short: i18n.T("Print a detailed description of the selected process"),
ValidArgsFunction: psCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
podID := args[0]
return cmdr.PsDescribe(app, podID)
},
}
return cmd
}
func psDeleteCommand(cmdr *commands.DryccCmd) *cobra.Command {
psCompletion := completion.PsCompletion{AppID: &app, ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "delete <pod>...",
Args: cobra.MinimumNArgs(1),
Example: template.CustomExample(
"drycc ps delete my-pod another-pod",
map[string]string{
"<pod>": i18n.T("The pod name for the application"),
},
),
Short: i18n.T("Delete the selected processes"),
ValidArgsFunction: psCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
return cmdr.PsDelete(app, args)
},
}
return cmd
}