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.
|
2017-03-06 23:58:53 +00:00
|
|
|
package run
|
2013-07-24 12:01:14 +00:00
|
|
|
|
|
|
|
import (
|
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"
|
2017-03-06 23:58:53 +00:00
|
|
|
|
2020-12-16 05:20:41 +00:00
|
|
|
"github.com/beego/bee/v2/cmd/commands"
|
|
|
|
"github.com/beego/bee/v2/cmd/commands/version"
|
|
|
|
"github.com/beego/bee/v2/config"
|
|
|
|
beeLogger "github.com/beego/bee/v2/logger"
|
|
|
|
"github.com/beego/bee/v2/utils"
|
2013-07-24 12:01:14 +00:00
|
|
|
)
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
var CmdRun = &commands.Command{
|
2017-03-07 05:00:37 +00:00
|
|
|
UsageLine: "run [appname] [watchall] [-main=*.go] [-downdoc=true] [-gendoc=true] [-vendor=true] [-e=folderToExclude] [-ex=extraPackageToWatch] [-tags=goBuildTags] [-runmode=BEEGO_RUNMODE]",
|
2016-12-03 10:54:41 +00:00
|
|
|
Short: "Run the application by starting a local development server",
|
2013-07-24 12:01:14 +00:00
|
|
|
Long: `
|
2016-12-03 10:54:41 +00:00
|
|
|
Run command will supervise the filesystem of the application for any changes, and recompile/restart it.
|
2013-07-24 12:01:14 +00:00
|
|
|
|
|
|
|
`,
|
2017-03-15 14:18:33 +00:00
|
|
|
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
|
|
|
|
Run: RunApp,
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 12:19:42 +00:00
|
|
|
var (
|
2017-03-06 23:58:53 +00:00
|
|
|
mainFiles utils.ListOpts
|
|
|
|
downdoc utils.DocValue
|
|
|
|
gendoc utils.DocValue
|
2016-07-28 12:19:42 +00:00
|
|
|
// The flags list of the paths excluded from watching
|
2017-03-06 23:58:53 +00:00
|
|
|
excludedPaths utils.StrFlags
|
2016-07-28 12:19:42 +00:00
|
|
|
// Pass through to -tags arg of "go build"
|
|
|
|
buildTags string
|
2020-06-25 14:29:27 +00:00
|
|
|
// Pass through to -ldflags arg of "go build"
|
|
|
|
buildLDFlags string
|
2016-07-28 12:19:42 +00:00
|
|
|
// 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
|
2018-06-26 10:07:49 +00:00
|
|
|
// Extra args to run application
|
|
|
|
runargs string
|
2017-03-07 04:56:47 +00:00
|
|
|
// Extra directories
|
2017-03-06 23:58:53 +00:00
|
|
|
extraPackages utils.StrFlags
|
2016-07-28 12:19:42 +00:00
|
|
|
)
|
2017-03-06 23:58:53 +00:00
|
|
|
var started = make(chan bool)
|
2016-07-22 22:24:44 +00:00
|
|
|
|
2013-07-24 12:01:14 +00:00
|
|
|
func init() {
|
2017-03-06 23:58:53 +00:00
|
|
|
CmdRun.Flag.Var(&mainFiles, "main", "Specify main go files.")
|
|
|
|
CmdRun.Flag.Var(&gendoc, "gendoc", "Enable auto-generate the docs.")
|
|
|
|
CmdRun.Flag.Var(&downdoc, "downdoc", "Enable auto-download of the swagger file if it does not exist.")
|
|
|
|
CmdRun.Flag.Var(&excludedPaths, "e", "List of paths to exclude.")
|
|
|
|
CmdRun.Flag.BoolVar(&vendorWatch, "vendor", false, "Enable watch vendor folder.")
|
|
|
|
CmdRun.Flag.StringVar(&buildTags, "tags", "", "Set the build tags. See: https://golang.org/pkg/go/build/")
|
2020-06-25 14:29:27 +00:00
|
|
|
CmdRun.Flag.StringVar(&buildLDFlags, "ldflags", "", "Set the build ldflags. See: https://golang.org/pkg/go/build/")
|
2017-03-06 23:58:53 +00:00
|
|
|
CmdRun.Flag.StringVar(&runmode, "runmode", "", "Set the Beego run mode.")
|
2018-06-26 10:07:49 +00:00
|
|
|
CmdRun.Flag.StringVar(&runargs, "runargs", "", "Extra args to run application")
|
2017-03-06 23:58:53 +00:00
|
|
|
CmdRun.Flag.Var(&extraPackages, "ex", "List of extra package to watch.")
|
2016-07-28 12:19:42 +00:00
|
|
|
exit = make(chan bool)
|
2017-03-06 23:58:53 +00:00
|
|
|
commands.AvailableCommands = append(commands.AvailableCommands, CmdRun)
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 11:35:26 +00:00
|
|
|
// RunApp locates files to watch, and starts the beego application
|
2017-03-06 23:58:53 +00:00
|
|
|
func RunApp(cmd *commands.Command, args []string) int {
|
2018-10-06 11:35:26 +00:00
|
|
|
// The default app path is the current working directory
|
|
|
|
appPath, _ := os.Getwd()
|
|
|
|
|
|
|
|
// If an argument is presented, we use it as the app path
|
|
|
|
if len(args) != 0 && args[0] != "watchall" {
|
|
|
|
if path.IsAbs(args[0]) {
|
|
|
|
appPath = args[0]
|
2016-07-30 11:42:38 +00:00
|
|
|
} else {
|
2018-10-06 11:35:26 +00:00
|
|
|
appPath = path.Join(appPath, args[0])
|
2016-07-29 15:45:15 +00:00
|
|
|
}
|
2018-10-06 11:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if utils.IsInGOPATH(appPath) {
|
|
|
|
if found, _gopath, _path := utils.SearchGOPATHs(appPath); found {
|
|
|
|
appPath = _path
|
|
|
|
appname = path.Base(appPath)
|
2016-07-29 15:45:15 +00:00
|
|
|
currentGoPath = _gopath
|
|
|
|
} else {
|
2018-10-06 11:35:26 +00:00
|
|
|
beeLogger.Log.Fatalf("No application '%s' found in your GOPATH", appPath)
|
2016-07-22 15:33:05 +00:00
|
|
|
}
|
2018-10-06 11:35:26 +00:00
|
|
|
if strings.HasSuffix(appname, ".go") && utils.IsExist(appPath) {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Warnf("The appname is in conflict with file's current path. Do you want to build appname as '%s'", appname)
|
|
|
|
beeLogger.Log.Info("Do you want to overwrite it? [yes|no] ")
|
|
|
|
if !utils.AskForConfirmation() {
|
2015-05-09 07:31:55 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
2018-10-06 11:35:26 +00:00
|
|
|
} else {
|
|
|
|
beeLogger.Log.Warn("Running application outside of GOPATH")
|
|
|
|
appname = path.Base(appPath)
|
|
|
|
currentGoPath = appPath
|
2013-07-24 12:01:14 +00:00
|
|
|
}
|
2016-07-22 15:33:05 +00:00
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Infof("Using '%s' as 'appname'", appname)
|
2016-11-13 14:14:48 +00:00
|
|
|
|
2018-10-06 11:35:26 +00:00
|
|
|
beeLogger.Log.Debugf("Current path: %s", utils.FILE(), utils.LINE(), appPath)
|
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)
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Infof("Using '%s' as 'runmode'", 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)
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Warnf("Using '%s' as 'runmode'", os.Getenv("BEEGO_RUNMODE"))
|
2016-09-26 18:32:14 +00:00
|
|
|
} else if os.Getenv("BEEGO_RUNMODE") != "" {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Warnf("Using '%s' as 'runmode'", os.Getenv("BEEGO_RUNMODE"))
|
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
|
2018-10-06 11:35:26 +00:00
|
|
|
readAppDirectories(appPath, &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.
|
2017-03-06 23:58:53 +00:00
|
|
|
for _, p := range config.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
|
|
|
|
2017-03-07 04:56:47 +00:00
|
|
|
if len(extraPackages) > 0 {
|
|
|
|
// get the full path
|
|
|
|
for _, packagePath := range extraPackages {
|
2017-03-06 23:58:53 +00:00
|
|
|
if found, _, _fullPath := utils.SearchGOPATHs(packagePath); found {
|
2017-03-07 04:56:47 +00:00
|
|
|
readAppDirectories(_fullPath, &paths)
|
|
|
|
} else {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Warnf("No extra package '%s' found in your GOPATH", packagePath)
|
2017-03-07 04:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// let paths unique
|
|
|
|
strSet := make(map[string]struct{})
|
|
|
|
for _, p := range paths {
|
|
|
|
strSet[p] = struct{}{}
|
|
|
|
}
|
|
|
|
paths = make([]string, len(strSet))
|
|
|
|
index := 0
|
|
|
|
for i := range strSet {
|
|
|
|
paths[index] = i
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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" {
|
2018-10-06 11:35:26 +00:00
|
|
|
if _, err := os.Stat(path.Join(appPath, "swagger", "index.html")); err != nil {
|
2016-08-16 16:52:57 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
downloadFromURL(swaggerlink, "swagger.zip")
|
|
|
|
unzipAndDelete("swagger.zip")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 21:05:32 +00:00
|
|
|
|
|
|
|
// Start the Reload server (if enabled)
|
2017-03-06 23:58:53 +00:00
|
|
|
if config.Conf.EnableReload {
|
2017-02-13 21:05:32 +00:00
|
|
|
startReloadServer()
|
|
|
|
}
|
2014-08-08 16:55:55 +00:00
|
|
|
if gendoc == "true" {
|
2014-06-18 13:31:54 +00:00
|
|
|
NewWatcher(paths, files, true)
|
2016-11-13 14:14:48 +00:00
|
|
|
AutoBuild(files, true)
|
2014-06-18 13:31:54 +00:00
|
|
|
} else {
|
|
|
|
NewWatcher(paths, files, false)
|
2016-11-13 14:14:48 +00:00
|
|
|
AutoBuild(files, false)
|
2014-06-18 13:31:54 +00:00
|
|
|
}
|
2016-08-16 16:52:57 +00:00
|
|
|
|
2013-07-24 12:01:14 +00:00
|
|
|
for {
|
2017-03-11 08:57:06 +00:00
|
|
|
<-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
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
if fileInfo.IsDir() && fileInfo.Name()[0] != '.' {
|
2014-01-22 16:57:16 +00:00
|
|
|
readAppDirectories(directory+"/"+fileInfo.Name(), paths)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
if useDirectory {
|
2014-01-22 16:57:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
if path.Ext(fileInfo.Name()) == ".go" || (ifStaticFile(fileInfo.Name()) && config.Conf.EnableReload) {
|
2014-01-22 16:57:16 +00:00
|
|
|
*paths = append(*paths, directory)
|
2014-10-28 01:58:28 +00:00
|
|
|
useDirectory = true
|
2014-01-22 16:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Errorf("Cannot get absolute path of '%s'", p)
|
2016-01-11 04:22:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
absFilePath, err := path.Abs(filePath)
|
|
|
|
if err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Errorf("Cannot get absolute path of '%s'", filePath)
|
2016-01-11 04:22:38 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(absFilePath, absP) {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Infof("'%s' is not being watched", filePath)
|
2015-06-17 06:59:59 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|