Skip to content

Commit 3479a4b

Browse files
committed
Add more options to the down command
1 parent 34f5b3e commit 3479a4b

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

foundation/console/down_command.go

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
package console
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
48
"github.com/goravel/framework/contracts/console"
59
"github.com/goravel/framework/contracts/console/command"
610
"github.com/goravel/framework/contracts/foundation"
711
"github.com/goravel/framework/support/file"
12+
"github.com/goravel/framework/support/str"
813
)
914

1015
type DownCommand struct {
1116
app foundation.Application
1217
}
1318

19+
type DownOptions struct {
20+
Reason string `json:"reason,omitempty"`
21+
Redirect string `json:"redirect,omitempty"`
22+
Render string `json:"render,omitempty"`
23+
Secret string `json:"secret,omitempty"`
24+
Status int `json:"status"`
25+
}
26+
1427
func NewDownCommand(app foundation.Application) *DownCommand {
1528
return &DownCommand{app}
1629
}
@@ -34,6 +47,27 @@ func (r *DownCommand) Extend() command.Extend {
3447
Usage: "The reason for maintenance to show in the response",
3548
Value: "The application is under maintenance",
3649
},
50+
&command.StringFlag{
51+
Name: "redirect",
52+
Usage: "The path that the user should be redirected to",
53+
},
54+
&command.StringFlag{
55+
Name: "render",
56+
Usage: "The view should be prerendered for display during maintenance mode",
57+
},
58+
&command.StringFlag{
59+
Name: "secret",
60+
Usage: "The secret phrase that may be used to bypass the maintenance mode",
61+
},
62+
&command.BoolFlag{
63+
Name: "with-secret",
64+
Usage: "Generate a random secret phrase that may be used to bypass the maintenance mode",
65+
},
66+
&command.IntFlag{
67+
Name: "status",
68+
Usage: "The status code that should be used when returning the maintenance mode response",
69+
Value: http.StatusServiceUnavailable,
70+
},
3771
},
3872
}
3973
}
@@ -48,7 +82,55 @@ func (r *DownCommand) Handle(ctx console.Context) error {
4882
return nil
4983
}
5084

51-
if err := file.PutContent(path, ctx.Option("reason")); err != nil {
85+
options := DownOptions{}
86+
87+
options.Status = ctx.OptionInt("status")
88+
89+
if redirect := ctx.Option("redirect"); redirect != "" {
90+
options.Redirect = redirect
91+
}
92+
93+
if render := ctx.Option("render"); render != "" {
94+
if r.app.MakeView().Exists(render) {
95+
options.Render = render
96+
} else {
97+
ctx.Error("Unable to find the view template")
98+
return nil
99+
}
100+
}
101+
102+
if options.Redirect == "" && options.Render == "" {
103+
options.Reason = ctx.Option("reason")
104+
}
105+
106+
if secret := ctx.Option("secret"); secret != "" {
107+
hash, err := r.app.MakeHash().Make(secret)
108+
if err != nil {
109+
ctx.Error("Unable to generate and hash the secret")
110+
} else {
111+
options.Secret = hash
112+
}
113+
}
114+
115+
if withSecret := ctx.OptionBool("with-secret"); withSecret {
116+
secret := str.Random(40)
117+
hash, err := r.app.MakeHash().Make(secret)
118+
119+
if err != nil {
120+
ctx.Error("Unable to generate and hash the secret")
121+
} else {
122+
options.Secret = hash
123+
ctx.Info(fmt.Sprintf("Using secret: %s", secret))
124+
}
125+
}
126+
127+
jsonBytes, err := json.Marshal(options)
128+
129+
if err != nil {
130+
return err
131+
}
132+
133+
if err := file.PutContent(path, string(jsonBytes)); err != nil {
52134
return err
53135
}
54136

0 commit comments

Comments
 (0)