/
policy_make_command.go
65 lines (50 loc) · 1.61 KB
/
policy_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/chenyusolar/framework/contracts/console"
"github.com/chenyusolar/framework/contracts/console/command"
"github.com/chenyusolar/framework/support/file"
"github.com/chenyusolar/framework/support/str"
"github.com/gookit/color"
)
type PolicyMakeCommand struct {
}
//Signature The name and signature of the console command.
func (receiver *PolicyMakeCommand) Signature() string {
return "make:policy"
}
//Description The console command description.
func (receiver *PolicyMakeCommand) Description() string {
return "Create a new policy class"
}
//Extend The console command extend.
func (receiver *PolicyMakeCommand) Extend() command.Extend {
return command.Extend{
Category: "make",
}
}
//Handle Execute the console command.
func (receiver *PolicyMakeCommand) 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("Policy created successfully")
return nil
}
func (receiver *PolicyMakeCommand) getStub() string {
return PolicyStubs{}.Policy()
}
//populateStub Populate the place-holders in the command stub.
func (receiver *PolicyMakeCommand) populateStub(stub string, name string) string {
stub = strings.ReplaceAll(stub, "DummyPolicy", str.Case2Camel(name))
return stub
}
//getPath Get the full path to the command.
func (receiver *PolicyMakeCommand) getPath(name string) string {
pwd, _ := os.Getwd()
return pwd + "/app/policies/" + str.Camel2Case(name) + ".go"
}