// Copyright 2013 bee authors // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. package main import ( "fmt" "os" path "path/filepath" "strings" ) var cmdNew = &Command{ UsageLine: "new [appname]", Short: "Create a Beego application", Long: ` Creates a Beego application for the given app name in the current directory. The command 'new' creates a folder named [appname] and inside the folder deploy the following files/directories structure: . ├── conf │ └── app.conf ├── controllers │ └── index_controller.go ├── helpers ├── main.go ├── models ├── routers │ └── router.go ├── static │ ├── css │ ├── img │ └── js ├── structure ├── tests │ └── default_test.go └── views └── index.tpl `, } func init() { cmdNew.Run = createApp } func createApp(cmd *Command, args []string) int { ShowShortVersionBanner() if len(args) != 1 { ColorLog("[ERRO] Argument [appname] is missing\n") os.Exit(2) } gopaths := GetGOPATHs() Debugf("gopath:%s", gopaths) gopath := "" switch len(gopaths) { case 1: gopath = gopaths[0] case 2: gopath = gopaths[1] default: ColorLog("[ERRO] $GOPATH not found\n") ColorLog("[HINT] Set $GOPATH in your environment variables\n") os.Exit(2) } gosrcpath := path.Join(gopath, "src") // User's workspace apppath := path.Join(gosrcpath, args[0]) if isExist(apppath) { ColorLog("[ERRO] Path (%s) already exists\n", apppath) ColorLog("[WARN] Do you want to overwrite it? [yes|no]]") if !askForConfirmation() { os.Exit(2) } } ColorLog("[INFO] Creating application...\n") os.MkdirAll(apppath, 0755) fmt.Println("\tcreate\t", apppath+string(path.Separator)) os.Mkdir(path.Join(apppath, "conf"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "conf")+string(path.Separator)) os.Mkdir(path.Join(apppath, "controllers"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "controllers")+string(path.Separator)) os.Mkdir(path.Join(apppath, "helpers"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "helpers")+string(path.Separator)) os.Mkdir(path.Join(apppath, "models"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "models")+string(path.Separator)) os.Mkdir(path.Join(apppath, "routers"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "routers")+string(path.Separator)) os.Mkdir(path.Join(apppath, "tests"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "tests")+string(path.Separator)) os.Mkdir(path.Join(apppath, "structures"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "structures")+string(path.Separator)) os.Mkdir(path.Join(apppath, "static"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "static")+string(path.Separator)) os.Mkdir(path.Join(apppath, "static", "js"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "static", "js")+string(path.Separator)) os.Mkdir(path.Join(apppath, "static", "css"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "static", "css")+string(path.Separator)) os.Mkdir(path.Join(apppath, "static", "img"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "static", "img")+string(path.Separator)) fmt.Println("\tcreate\t", path.Join(apppath, "views")+string(path.Separator)) os.Mkdir(path.Join(apppath, "views"), 0755) fmt.Println("\tcreate\t", path.Join(apppath, "conf", "app.conf")) WriteToFile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", path.Base(args[0]), -1)) fmt.Println("\tcreate\t", path.Join(apppath, "controllers", "index_controller.go")) WriteToFile(path.Join(apppath, "controllers", "index_controller.go"), controllers) fmt.Println("\tcreate\t", path.Join(apppath, "views", "index.tpl")) WriteToFile(path.Join(apppath, "views", "index.tpl"), indextpl) fmt.Println("\tcreate\t", path.Join(apppath, "routers", "router.go")) WriteToFile(path.Join(apppath, "routers", "router.go"), strings.Replace(router, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) fmt.Println("\tcreate\t", path.Join(apppath, "tests", "default_test.go")) WriteToFile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(test, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) fmt.Println("\tcreate\t", path.Join(apppath, "main.go")) WriteToFile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) ColorLog("[SUCC] New application successfully created!\n") return 0 } var appconf = `appname = {{.Appname}} httpport = 8080 runmode = dev ` var maingo = `package main import ( _ "{{.Appname}}/routers" "github.com/astaxie/beego" ) func main() { beego.Run() } ` var router = `package routers import ( "{{.Appname}}/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/", &controllers.MainController{}) } ` var test = `package test import ( "net/http" "net/http/httptest" "testing" "runtime" "path/filepath" _ "{{.Appname}}/routers" "github.com/astaxie/beego" . "github.com/smartystreets/goconvey/convey" ) func init() { _, file, _, _ := runtime.Caller(1) apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator)))) beego.TestBeegoInit(apppath) } // TestMain is a sample to run an endpoint test func TestMain(t *testing.T) { r, _ := http.NewRequest("GET", "/", nil) w := httptest.NewRecorder() beego.BeeApp.Handlers.ServeHTTP(w, r) beego.Trace("testing", "TestMain", "Code[%d]\n%s", w.Code, w.Body.String()) Convey("Subject: Test Station Endpoint\n", t, func() { Convey("Status Code Should Be 200", func() { So(w.Code, ShouldEqual, 200) }) Convey("The Result Should Not Be Empty", func() { So(w.Body.Len(), ShouldBeGreaterThan, 0) }) }) } ` var controllers = `package controllers import ( "github.com/astaxie/beego" ) type MainController struct { beego.Controller } func (c *MainController) Get() { c.Data["Website"] = "beego.me" c.Data["Email"] = "astaxie@gmail.com" c.TplName = "index.tpl" } ` var indextpl = `