forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
75 lines (61 loc) · 1.83 KB
/
resource.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
66
67
68
69
70
71
72
73
74
75
package generate
import (
"errors"
"github.com/gobuffalo/buffalo/generators/resource"
"github.com/gobuffalo/makr"
"github.com/markbates/inflect"
"github.com/spf13/cobra"
)
const resourceExamples = `$ buffalo g resource users
Generates:
- actions/users.go
- actions/users_test.go
- models/user.go
- models/user_test.go
- migrations/2016020216301234_create_users.up.fizz
- migrations/2016020216301234_create_users.down.fizz
$ buffalo g resource users --skip-migration
Generates:
- actions/users.go
- actions/users_test.go
- models/user.go
- models/user_test.go
$ buffalo g resource users --skip-model
Generates:
- actions/users.go
- actions/users_test.go`
//SkipResourceMigration allows to generate a resource without the migration.
var SkipResourceMigration = false
//SkipResourceModel allows to generate a resource without the model and Migration.
var SkipResourceModel = false
// ResourceCmd generates a new actions/resource file and a stub test.
var ResourceCmd = &cobra.Command{
Use: "resource [name]",
Example: resourceExamples,
Aliases: []string{"r"},
Short: "Generates a new actions/resource file",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("you must specify a resource name")
}
name := args[0]
data := makr.Data{
"name": name,
"singular": inflect.Singularize(name),
"plural": inflect.Pluralize(name),
"camel": inflect.Camelize(name),
"under": inflect.Underscore(name),
"downFirstCap": inflect.CamelizeDownFirst(name),
"actions": []string{"List", "Show", "New", "Create", "Edit", "Update", "Destroy"},
"args": args,
// Flags
"skipMigration": SkipResourceMigration,
"skipModel": SkipResourceModel,
}
g, err := resource.New(data)
if err != nil {
return err
}
return g.Run(".", data)
},
}