-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlimits.go
More file actions
158 lines (136 loc) · 5.37 KB
/
limits.go
File metadata and controls
158 lines (136 loc) · 5.37 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
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"
)
// NewLimitsCommand creates a command for managing resource limits.
func NewLimitsCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "limits",
Short: i18n.T("Manage resource limits for your application"),
RunE: func(_ *cobra.Command, _ []string) error {
return cmdr.LimitsList(app, version)
},
}
cmd.PersistentFlags().StringVarP(&app, "app", "a", "", i18n.T("The uniquely identifiable name for the application"))
cmd.Flags().IntVarP(&version, "version", "v", 0, i18n.T("The version for which the limit needs to be listed"))
appCompletion := completion.AppCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("app", appCompletion.CompletionFunc)
releaseCompletion := completion.ReleaseCompletion{ConfigFile: &cmdr.ConfigFile, AppID: &app}
cmd.RegisterFlagCompletionFunc("version", releaseCompletion.CompletionFunc)
cmd.AddCommand(limitsListCommand(cmdr))
cmd.AddCommand(limitSetCommand(cmdr))
cmd.AddCommand(limitUnsetCommand(cmdr))
cmd.AddCommand(limitSpecsCommand(cmdr))
cmd.AddCommand(limitPlansCommand(cmdr))
return cmd
}
func limitsListCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Example: "drycc limits list",
Short: i18n.T("List resource limits for an app"),
RunE: func(_ *cobra.Command, _ []string) error {
return cmdr.LimitsList(app, version)
},
}
cmd.Flags().IntVarP(&version, "version", "v", 0, i18n.T("The version for which the limit needs to be listed"))
releaseCompletion := completion.ReleaseCompletion{ConfigFile: &cmdr.ConfigFile, AppID: &app}
cmd.RegisterFlagCompletionFunc("version", releaseCompletion.CompletionFunc)
return cmd
}
func limitSetCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
limits []string
}
limitSetPlanCompletion := completion.LimitSetPlanCompletion{AppID: &app, ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "set <ptype>=<value>...",
Args: cobra.MinimumNArgs(1),
Example: template.CustomExample(
"drycc limits set web=std1.large.c1m2",
map[string]string{
"<ptype>": i18n.T("The process type as defined in your Procfile, such as 'web' or 'worker'"),
"<value>": i18n.T("The limit plan id to apply to the process type"),
},
),
Short: i18n.T("Set resource limits for an app"),
Long: i18n.T(`Sets resource limits for an application.
A resource limit is a finite resource within a pod which we can apply
restrictions through Kubernetes.`),
ValidArgsFunction: limitSetPlanCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
flags.limits = args
return cmdr.LimitsSet(app, flags.limits)
},
}
return cmd
}
func limitUnsetCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
limits []string
}
ptsCompletion := completion.PtsCompletion{AppID: &app, ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "unset <ptype>...",
Args: cobra.MinimumNArgs(1),
Example: template.CustomExample(
"drycc limits unset web",
map[string]string{
"<ptype>": i18n.T("The process type as defined in your Procfile, such as 'web' or 'worker'"),
},
),
Short: i18n.T("Unset resource limits for an app"),
ValidArgsFunction: ptsCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
flags.limits = args
return cmdr.LimitsUnset(app, flags.limits)
},
}
return cmd
}
func limitSpecsCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
keywords string
limit int
}
cmd := &cobra.Command{
Use: "specs",
Short: i18n.T("List specification information of the server"),
Long: i18n.T("List all available limit specs"),
RunE: func(_ *cobra.Command, _ []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.LimitsSpecs(flags.keywords, results)
},
}
cmd.Flags().StringVarP(&flags.keywords, "keywords", "k", "", i18n.T("Search keywords separated by commas, matching must satisfy all of the specified."))
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
return cmd
}
func limitPlansCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
specID string
cpu int
memory int
limit int
}
cmd := &cobra.Command{
Use: "plans",
Short: i18n.T("List all available limit plans"),
RunE: func(_ *cobra.Command, _ []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.LimitsPlans(flags.specID, flags.cpu, flags.memory, results)
},
}
cmd.Flags().IntVar(&flags.cpu, "cpu", 0, i18n.T("Query plans that meet the specified number of cpu cores"))
cmd.Flags().IntVarP(&flags.memory, "memory", "m", 0, i18n.T("Query plans that meet the specified memory capacity, unit GiB"))
cmd.Flags().StringVar(&flags.specID, "spec-id", "", i18n.T("Query plans that meet the specified spec id, see [specs] subcommand."))
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
cmd.Flags().SortFlags = false
completion := completion.LimitSpecCompletion{ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("spec-id", completion.CompletionFunc)
return cmd
}