forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.go
124 lines (109 loc) · 3.59 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
// Copyright © 2016 Mark Bates <mark@markbates.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/markbates/inflect"
"github.com/spf13/cobra"
)
var force bool
var verbose bool
var skipPop bool
var skipWebpack bool
var dbType = "postgres"
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.")
}
name := args[0]
rootPath, err := rootPath(name)
if err != nil {
return err
}
s, _ := os.Stat(rootPath)
if s != nil {
if force {
os.RemoveAll(rootPath)
} else {
return fmt.Errorf("%s already exists! Either delete it or use the -f flag to force.\n", name)
}
}
return genNewFiles(name, rootPath)
},
}
func rootPath(name string) (string, error) {
pwd, err := os.Getwd()
if err != nil {
return "", err
}
rootPath := filepath.Join(pwd, name)
return rootPath, nil
}
func packagePath(rootPath string) string {
return strings.Replace(rootPath, filepath.Join(os.Getenv("GOPATH"), "src")+"/", "", 1)
}
func goInstall(pkg string) *exec.Cmd {
args := []string{"install"}
if verbose {
args = append(args, "-v")
}
args = append(args, pkg)
return exec.Command("go", args...)
}
func goGet(pkg string) *exec.Cmd {
args := []string{"get"}
if verbose {
args = append(args, "-v")
}
args = append(args, pkg)
return exec.Command("go", args...)
}
func genNewFiles(name, rootPath string) error {
packagePath := packagePath(rootPath)
data := map[string]interface{}{
"name": name,
"titleName": inflect.Titleize(name),
"packagePath": packagePath,
"actionsPath": filepath.Join(packagePath, "actions"),
"modelsPath": filepath.Join(packagePath, "models"),
"withPop": !skipPop,
"withWebpack": !skipWebpack,
"dbType": dbType,
"version": Version,
}
g := newAppGenerator(data)
return g.Run(rootPath, data)
}
func init() {
RootCmd.AddCommand(newCmd)
newCmd.Flags().BoolVarP(&force, "force", "f", false, "delete and remake if the app already exists")
newCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "verbosely print out the go get/install commands")
newCmd.Flags().BoolVar(&skipPop, "skip-pop", false, "skips adding pop/soda to your app")
newCmd.Flags().BoolVar(&skipWebpack, "skip-webpack", false, "skips adding Webpack to your app")
newCmd.Flags().StringVar(&dbType, "db-type", "postgres", "specify the type of database you want to use [postgres, mysql, sqlite3]")
}