Skip to content

Commit 9810e4a

Browse files
authored
chore(ps): restart process types (#43)
1 parent 86d9696 commit 9810e4a

7 files changed

Lines changed: 37 additions & 18 deletions

File tree

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type Commander interface {
8787
PsLogs(string, string, int, bool, string) error
8888
PsExec(string, string, bool, bool, []string) error
8989
PsScale(string, []string) error
90-
PsRestart(string, string) error
90+
PsRestart(string, []string, string) error
9191
PsDescribe(string, string) error
9292
RegistryList(string) error
9393
RegistrySet(string, []string) error

cmd/ps.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,31 @@ func (d *DryccCmd) PsScale(appID string, targets []string) error {
138138
}
139139

140140
// PsRestart restarts an app's processes.
141-
func (d *DryccCmd) PsRestart(appID, target string) error {
141+
func (d *DryccCmd) PsRestart(appID string, targets []string, confirm string) error {
142142
s, appID, err := load(d.ConfigFile, appID)
143143
if err != nil {
144144
return err
145145
}
146+
if len(targets) == 0 && confirm == "" {
147+
d.Printf(` ! WARNING: Potentially Restart Action
148+
! This command will restart all processes of the application
149+
! To proceed, type "yes" !
146150
151+
> `)
152+
153+
fmt.Scanln(&confirm)
154+
if confirm != "yes" {
155+
return fmt.Errorf("cancel the restart action")
156+
}
157+
}
147158
d.Printf("Restarting processes... but first, %s!\n", drinkOfChoice())
148159
startTime := time.Now()
149160
quit := progress(d.WOut)
150-
151-
err = ps.Restart(s.Client, appID, target)
161+
ptypes := strings.Join(targets, ",")
162+
targetMap := map[string]string{
163+
"types": ptypes,
164+
}
165+
err = ps.Restart(s.Client, appID, targetMap)
152166
quit <- true
153167
<-quit
154168
if err != nil {

cmd/ps_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,33 +221,36 @@ func TestPsRestart(t *testing.T) {
221221
var b bytes.Buffer
222222
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
223223

224-
server.Mux.HandleFunc("/v2/apps/foo/pods/restart/", func(w http.ResponseWriter, _ *http.Request) {
224+
server.Mux.HandleFunc("/v2/apps/foo/pods/restart/", func(w http.ResponseWriter, r *http.Request) {
225+
testutil.AssertBody(t, map[string]string{"types": ""}, r)
225226
testutil.SetHeaders(w)
226227
w.WriteHeader(http.StatusNoContent)
227228
})
228229

229230
b.Reset()
230-
err = cmdr.PsRestart("foo", "")
231+
err = cmdr.PsRestart("foo", []string{""}, "yes")
231232
assert.NoError(t, err)
232233

233-
server.Mux.HandleFunc("/v2/apps/coolapp/pods/restart/", func(w http.ResponseWriter, _ *http.Request) {
234+
server.Mux.HandleFunc("/v2/apps/coolapp/pods/restart/", func(w http.ResponseWriter, r *http.Request) {
235+
testutil.AssertBody(t, map[string]string{"types": "web"}, r)
234236
testutil.SetHeaders(w)
235237
w.WriteHeader(http.StatusNoContent)
236238
})
237239

238240
b.Reset()
239241

240-
err = cmdr.PsRestart("coolapp", "")
242+
err = cmdr.PsRestart("coolapp", []string{"web"}, "")
241243
assert.NoError(t, err)
242244

243-
server.Mux.HandleFunc("/v2/apps/testapp/pods/web/restart/", func(w http.ResponseWriter, _ *http.Request) {
245+
server.Mux.HandleFunc("/v2/apps/testapp/pods/restart/", func(w http.ResponseWriter, r *http.Request) {
246+
testutil.AssertBody(t, map[string]string{"types": "web,worker"}, r)
244247
testutil.SetHeaders(w)
245248
w.WriteHeader(http.StatusNoContent)
246249
})
247250

248251
b.Reset()
249252

250-
err = cmdr.PsRestart("testapp", "web")
253+
err = cmdr.PsRestart("testapp", []string{"web", "worker"}, "")
251254
assert.NoError(t, err)
252255
}
253256

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22
55
require (
66
github.com/containerd/console v1.0.4
77
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
8-
github.com/drycc/controller-sdk-go v0.0.0-20240523052610-656dcb6b9db3
8+
github.com/drycc/controller-sdk-go v0.0.0-20240524075643-99bda554d60c
99
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f
1010
github.com/olekukonko/tablewriter v0.0.5
1111
github.com/stretchr/testify v1.9.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
66
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
7-
github.com/drycc/controller-sdk-go v0.0.0-20240523052610-656dcb6b9db3 h1:eITOqdzcBH/QQVDD/zwHyq5JY1+xXr3MsPg066uR4fk=
8-
github.com/drycc/controller-sdk-go v0.0.0-20240523052610-656dcb6b9db3/go.mod h1:n6eQe1irJqjwLo/7t9+Dhdv6faSESQN+ATnZRBP3/Uc=
7+
github.com/drycc/controller-sdk-go v0.0.0-20240524075643-99bda554d60c h1:wxbZlPUsQfs7a3TnfAhdJ37kS3Gy56WkQ1k0keJJs24=
8+
github.com/drycc/controller-sdk-go v0.0.0-20240524075643-99bda554d60c/go.mod h1:n6eQe1irJqjwLo/7t9+Dhdv6faSESQN+ATnZRBP3/Uc=
99
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f h1:kgjvUQJeAszDoU1Vo4vTTE92KI8Av3JPb6Qn890niXg=
1010
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f/go.mod h1:n+QxGif6ha9CEoxVnlipxb9IdmerybcUSzTEDFkvjiA=
1111
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=

parser/ps.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,17 @@ func psRestart(argv []string, cmdr cmd.Commander) error {
138138
usage := `
139139
Restart an application or process type.
140140
141-
Usage: drycc ps:restart [<type>] [options]
141+
Usage: drycc ps:restart [<type>...] [options]
142142
143143
Arguments:
144144
<type>
145-
the process name as defined in your Procfile, such as 'web' or 'worker'.
145+
the process name as defined in your Procfile, such as 'web' or 'web worker'.
146146
147147
Options:
148148
-a --app=<app>
149149
the uniquely identifiable name for the application.
150+
--confirm=yes
151+
To proceed, type "yes".
150152
`
151153

152154
args, err := docopt.ParseArgs(usage, argv, "")
@@ -156,8 +158,8 @@ Options:
156158
}
157159

158160
apps := safeGetString(args, "--app")
159-
tp := safeGetString(args, "<type>")
160-
return cmdr.PsRestart(apps, tp)
161+
confirm := safeGetString(args, "--confirm")
162+
return cmdr.PsRestart(apps, args["<type>"].([]string), confirm)
161163
}
162164

163165
func psScale(argv []string, cmdr cmd.Commander) error {

parser/ps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (d FakeDryccCmd) PsScale(string, []string) error {
2828
return errors.New("ps:scale")
2929
}
3030

31-
func (d FakeDryccCmd) PsRestart(string, string) error {
31+
func (d FakeDryccCmd) PsRestart(string, []string, string) error {
3232
return errors.New("ps:restart")
3333
}
3434

0 commit comments

Comments
 (0)