forked from gobuffalo/buffalo
/
new.go
218 lines (178 loc) · 6.07 KB
/
new.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package cmd
import (
"errors"
"fmt"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
"github.com/gobuffalo/buffalo/generators/newapp"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/plush"
"github.com/markbates/inflect"
"github.com/spf13/cobra"
)
var rootPath string
var app = &newapp.App{}
var newCmd = &cobra.Command{
Use: "new [name]",
Short: "Creates a new Buffalo application",
RunE: func(cmd *cobra.Command, args []string) error {
if !validDbType() {
return fmt.Errorf("Unknown db-type %s expecting one of postgres, mysql or sqlite3", app.DBType)
}
if len(args) == 0 {
return errors.New("you must enter a name for your new application")
}
app.Name = args[0]
if forbiddenName() {
return fmt.Errorf("name %s is not allowed, try a different application name", app.Name)
}
if nameHasIllegalCharacter(app.Name) {
return fmt.Errorf("name %s is not allowed, application name can only be contain [a-Z0-9-_]", app.Name)
}
if app.Name == "." {
app.Name = filepath.Base(app.RootPath)
} else {
app.RootPath = filepath.Join(app.RootPath, app.Name)
}
err := validateInGoPath()
if err != nil {
return err
}
s, _ := os.Stat(app.RootPath)
if s != nil {
if app.Force {
os.RemoveAll(app.RootPath)
} else {
return fmt.Errorf("%s already exists! Either delete it or use the -f flag to force", app.Name)
}
}
err = genNewFiles()
if err != nil {
return err
}
fmt.Printf("Congratulations! Your application, %s, has been successfully built!\n\n", app.Name)
fmt.Println("You can find your new application at:")
fmt.Println(app.RootPath)
fmt.Println("\nPlease read the README.md file in your new application for next steps on running your application.")
return nil
},
}
func validDbType() bool {
return app.DBType == "postgres" || app.DBType == "mysql" || app.DBType == "sqlite3"
}
func forbiddenName() bool {
return contains(forbiddenAppNames, strings.ToLower(app.Name))
}
var nameRX = regexp.MustCompile("^[\\w-]+$")
func nameHasIllegalCharacter(name string) bool {
return !nameRX.MatchString(name)
}
func validateInGoPath() error {
gpMultiple := envy.GoPaths()
var gp string
larp := strings.ToLower(app.RootPath)
for i := 0; i < len(gpMultiple); i++ {
lgpm := strings.ToLower(filepath.Join(gpMultiple[i], "src"))
if strings.HasPrefix(larp, lgpm) {
gp = gpMultiple[i]
break
}
}
if gp == "" {
u, err := user.Current()
if err != nil {
return err
}
t, err := plush.Render(notInGoWorkspace, plush.NewContextWith(map[string]interface{}{
"name": app.Name,
"gopath": envy.GoPath(),
"current": app.RootPath,
"username": u.Username,
}))
if err != nil {
return err
}
fmt.Println(t)
os.Exit(-1)
}
return nil
}
func goPath(root string) string {
gpMultiple := envy.GoPaths()
path := ""
for i := 0; i < len(gpMultiple); i++ {
if strings.HasPrefix(root, filepath.Join(gpMultiple[i], "src")) {
path = gpMultiple[i]
break
}
}
return path
}
func packagePath(rootPath string) string {
gosrcpath := strings.Replace(filepath.Join(goPath(rootPath), "src"), "\\", "/", -1)
rootPath = strings.Replace(rootPath, "\\", "/", -1)
return strings.Replace(rootPath, gosrcpath+"/", "", 2)
}
func genNewFiles() error {
packagePath := packagePath(app.RootPath)
data := map[string]interface{}{
"name": app.Name,
"titleName": inflect.Titleize(app.Name),
"packagePath": packagePath,
"actionsPath": packagePath + "/actions",
"modelsPath": packagePath + "/models",
"withPop": !app.SkipPop,
"withWebpack": !app.SkipWebpack && !app.API,
"dbType": app.DBType,
"version": Version,
"ciProvider": app.CIProvider,
"asAPI": app.API,
"asWeb": !app.API,
"docker": app.Docker,
}
if app.WithYarn {
data["withYarn"] = true
}
g, err := app.Generator(data)
if err != nil {
return err
}
return g.Run(app.RootPath, data)
}
func init() {
pwd, _ := os.Getwd()
rootPath = pwd
app.RootPath = pwd
decorate("new", newCmd)
RootCmd.AddCommand(newCmd)
newCmd.Flags().BoolVar(&app.API, "api", false, "skip all front-end code and configure for an API server")
newCmd.Flags().BoolVarP(&app.Force, "force", "f", false, "delete and remake if the app already exists")
newCmd.Flags().BoolVarP(&app.Verbose, "verbose", "v", false, "verbosely print out the go get/install commands")
newCmd.Flags().BoolVar(&app.SkipPop, "skip-pop", false, "skips adding pop/soda to your app")
newCmd.Flags().BoolVar(&app.SkipDep, "skip-dep", false, "skips adding github.com/golang/dep to your app")
newCmd.Flags().BoolVar(&app.SkipWebpack, "skip-webpack", false, "skips adding Webpack to your app")
newCmd.Flags().BoolVar(&app.WithYarn, "with-yarn", false, "allows the use of yarn instead of npm as dependency manager")
newCmd.Flags().StringVar(&app.DBType, "db-type", "postgres", "specify the type of database you want to use [postgres, mysql, sqlite3]")
newCmd.Flags().StringVar(&app.Docker, "docker", "multi", "specify the type of Docker file to generate [none, multi, standard]")
newCmd.Flags().StringVar(&app.CIProvider, "ci-provider", "none", "specify the type of ci file you would like buffalo to generate [none, travis, gitlab-ci]")
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
var forbiddenAppNames = []string{"buffalo"}
const notInGoWorkspace = `Oops! It would appear that you are not in your Go Workspace.
Your $GOPATH is set to "<%= gopath %>".
You are currently in "<%= current %>".
The standard location for putting Go projects is something along the lines of "$GOPATH/src/github.com/<%= username %>/<%= name %>" (adjust accordingly).
We recommend you go to "$GOPATH/src/github.com/<%= username %>/" and try "buffalo new <%= name %>" again.`
const noGoPath = `You do not have a $GOPATH set. In order to work with Go, you must set up your $GOPATH and your Go Workspace.
We recommend reading this tutorial on setting everything up: https://www.goinggo.net/2016/05/installing-go-and-your-workspace.html
When you're ready come back and try again. Don't worry, Buffalo will be right here waiting for you. :)`