2013-09-03 17:23:58 +00:00
|
|
|
// 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.
|
|
|
|
|
2013-07-24 12:01:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-07-30 11:42:38 +00:00
|
|
|
"fmt"
|
2014-01-22 16:57:16 +00:00
|
|
|
"io/ioutil"
|
2013-07-24 12:01:14 +00:00
|
|
|
"os"
|
|
|
|
path "path/filepath"
|
|
|
|
"runtime"
|
2013-09-11 23:06:04 +00:00
|
|
|
"strings"
|
2013-07-24 12:01:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdRun = &Command{
|
2016-08-21 02:55:10 +00:00
|
|
|
UsageLine: "run [appname] [watchall] [-main=*.go] [-downdoc=true] [-gendoc=true] [-vendor=true] [-e=folderToExclude] [-tags=goBuildTags] [-runmode=BEEGO_RUNMODE]",
|
2014-12-18 16:19:34 +00:00
|
|
|
Short: "run the app and start a Web server for development",
|
2013-07-24 12:01:14 +00:00
|
|
|
Long: `
|
2014-12-18 16:19:34 +00:00
|
|
|
Run command will supervise the file system of the beego project using inotify,
|
|
|
|
it will recompile and restart the app after any modifications.
|
2013-07-24 12:01:14 +00:00
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2016-07-28 12:19:42 +00:00
|
|
|
var (
|
|
|
|
mainFiles ListOpts
|
2016-07-30 11:42:38 +00:00
|
|
|
downdoc docValue
|
|
|
|
gendoc docValue
|
2016-07-28 12:19:42 +00:00
|
|
|
// The flags list of the paths excluded from watching
|
|
|
|
excludedPaths strFlags
|
|
|
|
// Pass through to -tags arg of "go build"
|
|
|
|
buildTags string
|
|
|
|
// Application path
|
|
|
|
currpath string
|
|
|
|
// Application name
|
|
|
|
appname string
|
|
|
|
// Channel to signal an Exit
|
|
|
|
exit chan bool
|
|
|
|
// Flag to watch the vendor folder
|
|
|
|
vendorWatch bool
|
2016-07-29 15:45:15 +00:00
|
|
|
// Current user workspace
|
|
|
|
currentGoPath string
|
2016-08-21 02:55:10 +00:00
|
|
|
// Current runmode
|
|
|
|
runmode string
|
2016-07-28 12:19:42 +00:00
|
|
|
)
|
2016-07-22 22:24:44 +00:00
|
|
|
|
2013-07-24 12:01:14 +00:00
|
|
|
func init() {
|
|
|
|
cmdRun.Run = runApp
|
2014-02-21 17:51:18 +00:00
|
|
|
cmdRun.Flag.Var(&mainFiles, "main", "specify main go files")
|
2014-08-08 16:55:55 +00:00
|
|
|
cmdRun.Flag.Var(&gendoc, "gendoc", "auto generate the docs")
|
2014-06-23 15:02:46 +00:00
|
|
|
cmdRun.Flag.Var(&downdoc, "downdoc", "auto download swagger file when not exist")
|
2015-06-17 06:59:59 +00:00
|
|
|
cmdRun.Flag.Var(&excludedPaths, "e", "Excluded paths[].")
|
2016-07-22 22:24:44 +00:00
|
|
|
cmdRun.Flag.BoolVar(&vendorWatch, "vendor", false, "Watch vendor folder")
|
2016-02-10 19:39:02 +00:00
|
|
|
cmdRun.Flag.StringVar(&buildTags, "tags", "", "Build tags (https://golang.org/pkg/go/build/)")
|
2016-08-21 02:55:10 +00:00
|
|
|
cmdRun.Flag.StringVar(&runmode, "runmode", "", "Set BEEGO_RUNMODE env variable.")
|
2016-07-28 12:19:42 +00:00
|
|
|
exit = make(chan bool)
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 09:38:51 +00:00
|
|
|
func runApp(cmd *Command, args []string) int {
|
2016-06-01 12:30:29 +00:00
|
|
|
ShowShortVersionBanner()
|
2016-01-25 13:49:49 +00:00
|
|
|
|
2014-01-22 16:57:16 +00:00
|
|
|
if len(args) == 0 || args[0] == "watchall" {
|
2016-07-22 15:33:05 +00:00
|
|
|
currpath, _ = os.Getwd()
|
2016-07-29 15:45:15 +00:00
|
|
|
|
2016-07-30 11:42:38 +00:00
|
|
|
if found, _gopath, _ := SearchGOPATHs(currpath); found {
|
|
|
|
appname = path.Base(currpath)
|
|
|
|
currentGoPath = _gopath
|
|
|
|
} else {
|
2016-07-29 15:45:15 +00:00
|
|
|
exitPrint(fmt.Sprintf("Bee does not support non Beego project: %s", currpath))
|
|
|
|
}
|
2016-07-30 11:42:38 +00:00
|
|
|
ColorLog("[INFO] Using '%s' as 'appname'\n", appname)
|
2013-09-11 22:35:24 +00:00
|
|
|
} else {
|
2016-07-29 15:45:15 +00:00
|
|
|
// Check if passed Bee application path/name exists in the GOPATH(s)
|
2016-07-30 11:42:38 +00:00
|
|
|
if found, _gopath, _path := SearchGOPATHs(args[0]); found {
|
2016-07-29 15:45:15 +00:00
|
|
|
currpath = _path
|
|
|
|
currentGoPath = _gopath
|
|
|
|
appname = path.Base(currpath)
|
|
|
|
} else {
|
2016-09-26 18:32:14 +00:00
|
|
|
exitPrint(fmt.Sprintf("No Beego application '%s' found in your GOPATH", args[0]))
|
2016-07-22 15:33:05 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 11:42:38 +00:00
|
|
|
ColorLog("[INFO] Using '%s' as 'appname'\n", appname)
|
|
|
|
|
2016-07-22 15:33:05 +00:00
|
|
|
if strings.HasSuffix(appname, ".go") && isExist(currpath) {
|
|
|
|
ColorLog("[WARN] The appname is in conflict with currpath's file, do you want to build appname as %s\n", appname)
|
2015-05-09 07:31:55 +00:00
|
|
|
ColorLog("[INFO] Do you want to overwrite it? [yes|no]] ")
|
|
|
|
if !askForConfirmation() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
2016-07-22 15:33:05 +00:00
|
|
|
|
|
|
|
Debugf("current path:%s\n", currpath)
|
2013-07-24 12:01:14 +00:00
|
|
|
|
2016-09-26 18:32:14 +00:00
|
|
|
if runmode == "prod" || runmode == "dev" {
|
2016-08-21 02:55:10 +00:00
|
|
|
os.Setenv("BEEGO_RUNMODE", runmode)
|
|
|
|
ColorLog("[INFO] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE"))
|
2016-09-26 18:32:14 +00:00
|
|
|
} else if runmode != "" {
|
2016-08-21 02:55:10 +00:00
|
|
|
os.Setenv("BEEGO_RUNMODE", runmode)
|
|
|
|
ColorLog("[WARN] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE"))
|
2016-09-26 18:32:14 +00:00
|
|
|
} else if os.Getenv("BEEGO_RUNMODE") != "" {
|
2016-08-21 02:55:10 +00:00
|
|
|
ColorLog("[WARN] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE"))
|
|
|
|
}
|
|
|
|
|
2016-06-01 12:30:29 +00:00
|
|
|
err := loadConfig()
|
2013-07-24 12:01:14 +00:00
|
|
|
if err != nil {
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err)
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
2013-11-04 03:16:15 +00:00
|
|
|
|
2013-07-24 12:01:14 +00:00
|
|
|
var paths []string
|
2016-07-22 15:33:05 +00:00
|
|
|
readAppDirectories(currpath, &paths)
|
2014-06-18 13:31:54 +00:00
|
|
|
|
2013-07-31 06:44:56 +00:00
|
|
|
// Because monitor files has some issues, we watch current directory
|
|
|
|
// and ignore non-go files.
|
2013-09-11 23:06:04 +00:00
|
|
|
for _, p := range conf.DirStruct.Others {
|
2016-07-29 15:45:15 +00:00
|
|
|
paths = append(paths, strings.Replace(p, "$GOPATH", currentGoPath, -1))
|
2013-09-11 23:06:04 +00:00
|
|
|
}
|
2013-07-24 12:01:14 +00:00
|
|
|
|
2014-02-21 17:51:18 +00:00
|
|
|
files := []string{}
|
|
|
|
for _, arg := range mainFiles {
|
|
|
|
if len(arg) > 0 {
|
|
|
|
files = append(files, arg)
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 16:52:57 +00:00
|
|
|
if downdoc == "true" {
|
|
|
|
if _, err := os.Stat(path.Join(currpath, "swagger", "index.html")); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
downloadFromURL(swaggerlink, "swagger.zip")
|
|
|
|
unzipAndDelete("swagger.zip")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-08 16:55:55 +00:00
|
|
|
if gendoc == "true" {
|
2014-06-18 13:31:54 +00:00
|
|
|
NewWatcher(paths, files, true)
|
|
|
|
Autobuild(files, true)
|
|
|
|
} else {
|
|
|
|
NewWatcher(paths, files, false)
|
|
|
|
Autobuild(files, false)
|
|
|
|
}
|
2016-08-16 16:52:57 +00:00
|
|
|
|
2013-07-24 12:01:14 +00:00
|
|
|
for {
|
2013-08-09 09:49:14 +00:00
|
|
|
select {
|
|
|
|
case <-exit:
|
|
|
|
runtime.Goexit()
|
|
|
|
}
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-22 16:57:16 +00:00
|
|
|
|
|
|
|
func readAppDirectories(directory string, paths *[]string) {
|
|
|
|
fileInfos, err := ioutil.ReadDir(directory)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-28 01:58:28 +00:00
|
|
|
useDirectory := false
|
2014-01-22 16:57:16 +00:00
|
|
|
for _, fileInfo := range fileInfos {
|
2014-06-18 13:31:54 +00:00
|
|
|
if strings.HasSuffix(fileInfo.Name(), "docs") {
|
|
|
|
continue
|
|
|
|
}
|
2016-08-16 16:52:57 +00:00
|
|
|
if strings.HasSuffix(fileInfo.Name(), "swagger") {
|
|
|
|
continue
|
|
|
|
}
|
2015-06-17 06:59:59 +00:00
|
|
|
|
2016-07-22 22:24:44 +00:00
|
|
|
if !vendorWatch && strings.HasSuffix(fileInfo.Name(), "vendor") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-11 04:22:38 +00:00
|
|
|
if isExcluded(path.Join(directory, fileInfo.Name())) {
|
2015-06-17 06:59:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-01-22 16:57:16 +00:00
|
|
|
if fileInfo.IsDir() == true && fileInfo.Name()[0] != '.' {
|
|
|
|
readAppDirectories(directory+"/"+fileInfo.Name(), paths)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-10-28 01:58:28 +00:00
|
|
|
if useDirectory == true {
|
2014-01-22 16:57:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if path.Ext(fileInfo.Name()) == ".go" {
|
|
|
|
*paths = append(*paths, directory)
|
2014-10-28 01:58:28 +00:00
|
|
|
useDirectory = true
|
2014-01-22 16:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2015-06-17 06:59:59 +00:00
|
|
|
|
|
|
|
// If a file is excluded
|
2016-01-11 04:22:38 +00:00
|
|
|
func isExcluded(filePath string) bool {
|
2015-06-17 06:59:59 +00:00
|
|
|
for _, p := range excludedPaths {
|
2016-01-11 04:22:38 +00:00
|
|
|
absP, err := path.Abs(p)
|
|
|
|
if err != nil {
|
|
|
|
ColorLog("[ERROR] Can not get absolute path of [ %s ]\n", p)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
absFilePath, err := path.Abs(filePath)
|
|
|
|
if err != nil {
|
|
|
|
ColorLog("[ERROR] Can not get absolute path of [ %s ]\n", filePath)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(absFilePath, absP) {
|
|
|
|
ColorLog("[INFO] Excluding from watching [ %s ]\n", filePath)
|
2015-06-17 06:59:59 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|