github.com/jasonish/buffalo@v0.8.2-0.20170413145823-bacbdd415f1b/buffalo/cmd/new.go (about) 1 package cmd 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 "os/user" 8 "path/filepath" 9 "strings" 10 11 "github.com/gobuffalo/buffalo/generators/newapp" 12 "github.com/gobuffalo/envy" 13 "github.com/gobuffalo/plush" 14 "github.com/markbates/inflect" 15 "github.com/spf13/cobra" 16 ) 17 18 var rootPath string 19 var app = &newapp.App{} 20 21 var newCmd = &cobra.Command{ 22 Use: "new [name]", 23 Short: "Creates a new Buffalo application", 24 RunE: func(cmd *cobra.Command, args []string) error { 25 if !validDbType() { 26 return fmt.Errorf("Unknown db-type %s expecting one of postgres, mysql or sqlite3", app.DBType) 27 } 28 29 if len(args) == 0 { 30 return errors.New("you must enter a name for your new application") 31 } 32 33 app.Name = args[0] 34 35 if app.Name == "." { 36 app.Name = filepath.Base(app.RootPath) 37 } else { 38 app.RootPath = filepath.Join(app.RootPath, app.Name) 39 } 40 41 err := validateInGoPath() 42 if err != nil { 43 return err 44 } 45 46 s, _ := os.Stat(app.RootPath) 47 if s != nil { 48 if app.Force { 49 os.RemoveAll(app.RootPath) 50 } else { 51 return fmt.Errorf("%s already exists! Either delete it or use the -f flag to force", app.Name) 52 } 53 } 54 55 err = genNewFiles() 56 if err != nil { 57 return err 58 } 59 60 fmt.Printf("Congratulations! Your application, %s, has been successfully built!\n\n", app.Name) 61 fmt.Println("You can find your new application at:") 62 fmt.Println(app.RootPath) 63 fmt.Println("\nPlease read the README.md file in your new application for next steps on running your application.") 64 65 return nil 66 }, 67 } 68 69 func validDbType() bool { 70 return app.DBType == "postgres" || app.DBType == "mysql" || app.DBType == "sqlite3" 71 } 72 73 func validateInGoPath() error { 74 gpMultiple := envy.GoPaths() 75 76 var gp string 77 larp := strings.ToLower(app.RootPath) 78 for i := 0; i < len(gpMultiple); i++ { 79 lgpm := strings.ToLower(filepath.Join(gpMultiple[i], "src")) 80 if strings.HasPrefix(larp, lgpm) { 81 gp = gpMultiple[i] 82 break 83 } 84 } 85 86 if gp == "" { 87 u, err := user.Current() 88 if err != nil { 89 return err 90 } 91 t, err := plush.Render(notInGoWorkspace, plush.NewContextWith(map[string]interface{}{ 92 "name": app.Name, 93 "gopath": envy.GoPath(), 94 "current": app.RootPath, 95 "username": u.Username, 96 })) 97 if err != nil { 98 return err 99 } 100 fmt.Println(t) 101 os.Exit(-1) 102 } 103 return nil 104 } 105 106 func goPath(root string) string { 107 gpMultiple := envy.GoPaths() 108 path := "" 109 110 for i := 0; i < len(gpMultiple); i++ { 111 if strings.HasPrefix(root, filepath.Join(gpMultiple[i], "src")) { 112 path = gpMultiple[i] 113 break 114 } 115 } 116 return path 117 } 118 119 func packagePath(rootPath string) string { 120 gosrcpath := strings.Replace(filepath.Join(goPath(rootPath), "src"), "\\", "/", -1) 121 rootPath = strings.Replace(rootPath, "\\", "/", -1) 122 return strings.Replace(rootPath, gosrcpath+"/", "", 2) 123 } 124 125 func genNewFiles() error { 126 packagePath := packagePath(app.RootPath) 127 128 data := map[string]interface{}{ 129 "name": app.Name, 130 "titleName": inflect.Titleize(app.Name), 131 "packagePath": packagePath, 132 "actionsPath": packagePath + "/actions", 133 "modelsPath": packagePath + "/models", 134 "withPop": !app.SkipPop, 135 "withWebpack": !app.SkipWebpack, 136 "dbType": app.DBType, 137 "version": Version, 138 "ciProvider": app.CIProvider, 139 } 140 if app.WithYarn { 141 data["withYarn"] = true 142 } 143 144 g, err := app.Generator(data) 145 if err != nil { 146 return err 147 } 148 return g.Run(app.RootPath, data) 149 } 150 151 func init() { 152 pwd, _ := os.Getwd() 153 154 rootPath = pwd 155 app.RootPath = pwd 156 157 RootCmd.AddCommand(newCmd) 158 newCmd.Flags().BoolVarP(&app.Force, "force", "f", false, "delete and remake if the app already exists") 159 newCmd.Flags().BoolVarP(&app.Verbose, "verbose", "v", false, "verbosely print out the go get/install commands") 160 newCmd.Flags().BoolVar(&app.SkipPop, "skip-pop", false, "skips adding pop/soda to your app") 161 newCmd.Flags().BoolVar(&app.SkipWebpack, "skip-webpack", false, "skips adding Webpack to your app") 162 newCmd.Flags().BoolVar(&app.WithYarn, "with-yarn", false, "allows the use of yarn instead of npm as dependency manager") 163 newCmd.Flags().StringVar(&app.DBType, "db-type", "postgres", "specify the type of database you want to use [postgres, mysql, sqlite3]") 164 newCmd.Flags().StringVar(&app.CIProvider, "ci-provider", "none", "specify the type of ci file you would like buffalo to generate [none, travis, gitlab-ci]") 165 } 166 167 const notInGoWorkspace = `Oops! It would appear that you are not in your Go Workspace. 168 169 Your $GOPATH is set to "<%= gopath %>". 170 171 You are currently in "<%= current %>". 172 173 The standard location for putting Go projects is something along the lines of "$GOPATH/src/github.com/<%= username %>/<%= name %>" (adjust accordingly). 174 175 We recommend you go to "$GOPATH/src/github.com/<%= username %>/" and try "buffalo new <%= name %>" again.` 176 177 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. 178 179 We recommend reading this tutorial on setting everything up: https://www.goinggo.net/2016/05/installing-go-and-your-workspace.html 180 181 When you're ready come back and try again. Don't worry, Buffalo will be right here waiting for you. :)`