forked from gobuffalo/buffalo
/
new.go
151 lines (127 loc) · 4.61 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
package cmd
import (
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"github.com/markbates/inflect"
"github.com/sirupsen/logrus"
"github.com/pkg/errors"
"github.com/gobuffalo/buffalo/generators/newapp"
"github.com/gobuffalo/buffalo/meta"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/makr"
"github.com/gobuffalo/plush"
"github.com/gobuffalo/pop"
"github.com/spf13/cobra"
)
var rootPath string
var app = newapp.Generator{
App: meta.New("."),
DBType: "postgres",
CIProvider: "none",
AsWeb: true,
Docker: "multi",
Bootstrap: 3,
}
var newCmd = &cobra.Command{
Use: "new [name]",
Short: "Creates a new Buffalo application",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) <= 0 {
return errors.New("you must enter a name for your new application")
}
app.Name = inflect.Name(args[0])
app.Version = Version
if app.Name == "." {
app.Name = inflect.Name(filepath.Base(app.Root))
} else {
app.Root = filepath.Join(app.Root, app.Name.File())
}
aa := meta.New(app.Root)
app.ActionsPkg = aa.ActionsPkg
app.GriftsPkg = aa.GriftsPkg
app.ModelsPkg = aa.ModelsPkg
app.PackagePkg = aa.PackagePkg
if err := app.Validate(); err != nil {
if errors.Cause(err) == newapp.ErrNotInGoPath {
return notInGoPath(app)
}
return errors.WithStack(err)
}
app.WithPop = !app.SkipPop
app.WithWebpack = !app.SkipWebpack
app.WithYarn = !app.SkipYarn
app.AsWeb = !app.AsAPI
if app.AsAPI {
app.WithWebpack = false
}
if err := app.Run(app.Root, makr.Data{}); err != nil {
return errors.WithStack(err)
}
logrus.Infof("Congratulations! Your application, %s, has been successfully built!\n\n", app.Name)
logrus.Infof("You can find your new application at:\n%v", app.Root)
logrus.Info("\nPlease read the README.md file in your new application for next steps on running your application.")
return nil
},
}
func currentUser() (string, error) {
if _, err := exec.LookPath("git"); err == nil {
if b, err := exec.Command("git", "config", "github.user").Output(); err != nil {
return string(b), nil
}
}
u, err := user.Current()
if err != nil {
return "", err
}
username := u.Username
if t := strings.Split(username, `\`); len(t) > 0 {
username = t[len(t)-1]
}
return username, nil
}
func notInGoPath(ag newapp.Generator) error {
username, err := currentUser()
if err != nil {
return errors.WithStack(err)
}
pwd, _ := os.Getwd()
t, err := plush.Render(notInGoWorkspace, plush.NewContextWith(map[string]interface{}{
"name": ag.Name,
"gopath": envy.GoPath(),
"current": pwd,
"username": username,
}))
if err != nil {
return err
}
logrus.Error(t)
os.Exit(-1)
return nil
}
func init() {
pwd, _ := os.Getwd()
app.App = meta.New(pwd)
decorate("new", newCmd)
RootCmd.AddCommand(newCmd)
newCmd.Flags().BoolVar(&app.AsAPI, "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 commands")
newCmd.Flags().BoolVar(&app.SkipPop, "skip-pop", false, "skips adding pop/soda to your app")
newCmd.Flags().BoolVar(&app.WithDep, "with-dep", false, "adds 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.SkipYarn, "skip-yarn", false, "use npm instead of yarn for frontend dependencies management")
newCmd.Flags().StringVar(&app.DBType, "db-type", "postgres", fmt.Sprintf("specify the type of database you want to use [%s]", strings.Join(pop.AvailableDialects, ", ")))
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]")
newCmd.Flags().StringVar(&app.VCS, "vcs", "git", "specify the Version control system you would like to use [none, git, bzr]")
newCmd.Flags().IntVar(&app.Bootstrap, "bootstrap", app.Bootstrap, "specify version for Bootstrap [3, 4]")
}
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.`