-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_make_command.go
65 lines (50 loc) · 1.66 KB
/
middleware_make_command.go
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
package console
import (
"errors"
"os"
"strings"
"github.com/gookit/color"
"gopkg.in/go-mixed/framework.v1/contracts/console"
"gopkg.in/go-mixed/framework.v1/contracts/console/command"
"gopkg.in/go-mixed/framework.v1/support/file"
"gopkg.in/go-mixed/framework.v1/support/str"
)
type MiddlewareMakeCommand struct {
}
// Signature The name and signature of the console command.
func (receiver *MiddlewareMakeCommand) Signature() string {
return "make:middleware"
}
// Description The console command description.
func (receiver *MiddlewareMakeCommand) Description() string {
return "Create a new middleware class"
}
// Extend The console command extend.
func (receiver *MiddlewareMakeCommand) Extend() command.Extend {
return command.Extend{
Category: "make",
}
}
// Handle Execute the console command.
func (receiver *MiddlewareMakeCommand) Handle(ctx console.Context) error {
name := ctx.Argument(0)
if name == "" {
return errors.New("Not enough arguments (missing: name) ")
}
file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
color.Greenln("Middleware created successfully")
return nil
}
func (receiver *MiddlewareMakeCommand) getStub() string {
return Stubs{}.Middleware()
}
// populateStub Populate the place-holders in the command stub.
func (receiver *MiddlewareMakeCommand) populateStub(stub string, name string) string {
stub = strings.ReplaceAll(stub, "DummyMiddleware", str.Case2Camel(name))
return stub
}
// getPath Get the full path to the command.
func (receiver *MiddlewareMakeCommand) getPath(name string) string {
pwd, _ := os.Getwd()
return pwd + "/app/http/middleware/" + str.Camel2Case(name) + ".go"
}