From 4be4a20df4998e3120f8e16b29211ce667be4b97 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Fri, 22 Jul 2016 17:33:05 +0200 Subject: [PATCH 1/7] Added ability to run an application either from the appname directory or anywhere in the $GOPATH/src folder. (Fixes #219) --- run.go | 56 ++++++++++++++++++++++++++++++++++---------------------- watch.go | 4 ++-- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/run.go b/run.go index 1200e4c..c2263bf 100644 --- a/run.go +++ b/run.go @@ -20,11 +20,12 @@ import ( path "path/filepath" "runtime" "strings" + "fmt" ) var cmdRun = &Command{ UsageLine: "run [appname] [watchall] [-main=*.go] [-downdoc=true] [-gendoc=true] [-e=Godeps -e=folderToExclude] [-tags=goBuildTags]", - Short: "run the app and start a Web server for development", + Short: "Run the app and start a Web server for development", Long: ` Run command will supervise the file system of the beego project using inotify, it will recompile and restart the app after any modifications. @@ -45,36 +46,55 @@ var buildTags string func init() { cmdRun.Run = runApp - cmdRun.Flag.Var(&mainFiles, "main", "specify main go files") - cmdRun.Flag.Var(&gendoc, "gendoc", "auto generate the docs") - cmdRun.Flag.Var(&downdoc, "downdoc", "auto download swagger file when not exist") + cmdRun.Flag.Var(&mainFiles, "main", "Specify main go files") + cmdRun.Flag.Var(&gendoc, "gendoc", "Auto generate the docs") + cmdRun.Flag.Var(&downdoc, "downdoc", "Auto download Swagger file when does not exist") cmdRun.Flag.Var(&excludedPaths, "e", "Excluded paths[].") cmdRun.Flag.StringVar(&buildTags, "tags", "", "Build tags (https://golang.org/pkg/go/build/)") } -var appname string +var ( + currpath = "" + appname = "" + exit = make(chan bool) +) func runApp(cmd *Command, args []string) int { ShowShortVersionBanner() - exit := make(chan bool) - crupath, _ := os.Getwd() + gps := GetGOPATHs() + if len(gps) == 0 { + ColorLog("[ERRO] Fail to start[ %s ]\n", "$GOPATH is not set or empty") + os.Exit(2) + } + gopath := gps[0] if len(args) == 0 || args[0] == "watchall" { - appname = path.Base(crupath) + currpath, _ = os.Getwd() + appname = path.Base(currpath) ColorLog("[INFO] Uses '%s' as 'appname'\n", appname) } else { - appname = args[0] + gopathsrc := path.Join(gopath, "src") + currpath = path.Join(gopathsrc, args[0]) + appname = path.Base(currpath) + + // Check if passed Bee application path/name exists + // in $GOPATH/src workspace + if !isExist(currpath) { + panic(fmt.Sprintf("No Beego application '%s' found in GOPATH: %s", args[0], gopathsrc)) + } + ColorLog("[INFO] Uses '%s' as 'appname'\n", appname) - if strings.HasSuffix(appname, ".go") && isExist(path.Join(crupath, appname)) { - ColorLog("[WARN] The appname has conflic with crupath's file, do you want to build appname as %s\n", appname) + 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) ColorLog("[INFO] Do you want to overwrite it? [yes|no]] ") if !askForConfirmation() { return 0 } } } - Debugf("current path:%s\n", crupath) + + Debugf("current path:%s\n", currpath) err := loadConfig() if err != nil { @@ -82,17 +102,10 @@ func runApp(cmd *Command, args []string) int { } var paths []string - - readAppDirectories(crupath, &paths) + readAppDirectories(currpath, &paths) // Because monitor files has some issues, we watch current directory // and ignore non-go files. - gps := GetGOPATHs() - if len(gps) == 0 { - ColorLog("[ERRO] Fail to start[ %s ]\n", "$GOPATH is not set or empty") - os.Exit(2) - } - gopath := gps[0] for _, p := range conf.DirStruct.Others { paths = append(paths, strings.Replace(p, "$GOPATH", gopath, -1)) } @@ -112,7 +125,7 @@ func runApp(cmd *Command, args []string) int { Autobuild(files, false) } if downdoc == "true" { - if _, err := os.Stat(path.Join(crupath, "swagger")); err != nil { + if _, err := os.Stat(path.Join(currpath, "swagger")); err != nil { if os.IsNotExist(err) { downloadFromUrl(swaggerlink, "swagger.zip") unzipAndDelete("swagger.zip", "swagger") @@ -157,7 +170,6 @@ func readAppDirectories(directory string, paths *[]string) { useDirectory = true } } - return } diff --git a/watch.go b/watch.go index 7d966bd..1b4af32 100644 --- a/watch.go +++ b/watch.go @@ -121,8 +121,8 @@ func Autobuild(files []string, isgenerate bool) { defer state.Unlock() ColorLog("[INFO] Start building...\n") - path, _ := os.Getwd() - os.Chdir(path) + + os.Chdir(currpath) cmdName := "go" if conf.Gopm.Enable { From 4f3ed96523c780ff32f6f7b8497969350ef90ac2 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Mon, 25 Jul 2016 21:10:12 +0200 Subject: [PATCH 2/7] grouped variable declarations --- run.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/run.go b/run.go index c2263bf..be06849 100644 --- a/run.go +++ b/run.go @@ -15,12 +15,12 @@ package main import ( + "fmt" "io/ioutil" "os" path "path/filepath" "runtime" "strings" - "fmt" ) var cmdRun = &Command{ @@ -33,16 +33,21 @@ it will recompile and restart the app after any modifications. `, } -var mainFiles ListOpts - -var downdoc docValue -var gendoc docValue - -// The flags list of the paths excluded from watching -var excludedPaths strFlags - -// Pass through to -tags arg of "go build" -var buildTags string +var ( + mainFiles ListOpts + downdoc docValue + gendoc docValue + // 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 +) func init() { cmdRun.Run = runApp @@ -51,14 +56,9 @@ func init() { cmdRun.Flag.Var(&downdoc, "downdoc", "Auto download Swagger file when does not exist") cmdRun.Flag.Var(&excludedPaths, "e", "Excluded paths[].") cmdRun.Flag.StringVar(&buildTags, "tags", "", "Build tags (https://golang.org/pkg/go/build/)") + exit = make(chan bool) } -var ( - currpath = "" - appname = "" - exit = make(chan bool) -) - func runApp(cmd *Command, args []string) int { ShowShortVersionBanner() From 7254acdb7fb5d6ae6ded9cb149f9673486dc1155 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Mon, 25 Jul 2016 21:11:07 +0200 Subject: [PATCH 3/7] added Makefile --- Makefile | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7259aa6 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +.PHONY: all test clean build install + +GOFLAGS ?= $(GOFLAGS:) + +all: install test + +build: + go build $(GOFLAGS) ./... + +install: + go get $(GOFLAGS) ./... + +test: install + go test $(GOFLAGS) ./... + +bench: install + go test -run=NONE -bench=. $(GOFLAGS) ./... + +clean: + go clean $(GOFLAGS) -i ./... From 23f03c61b128b65b5f579ef3d4fdb3a0837c2189 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Tue, 26 Jul 2016 21:07:17 +0200 Subject: [PATCH 4/7] Revert "grouped variable declarations" This reverts commit 4f3ed96523c780ff32f6f7b8497969350ef90ac2. --- run.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/run.go b/run.go index be06849..c2263bf 100644 --- a/run.go +++ b/run.go @@ -15,12 +15,12 @@ package main import ( - "fmt" "io/ioutil" "os" path "path/filepath" "runtime" "strings" + "fmt" ) var cmdRun = &Command{ @@ -33,21 +33,16 @@ it will recompile and restart the app after any modifications. `, } -var ( - mainFiles ListOpts - downdoc docValue - gendoc docValue - // 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 -) +var mainFiles ListOpts + +var downdoc docValue +var gendoc docValue + +// The flags list of the paths excluded from watching +var excludedPaths strFlags + +// Pass through to -tags arg of "go build" +var buildTags string func init() { cmdRun.Run = runApp @@ -56,9 +51,14 @@ func init() { cmdRun.Flag.Var(&downdoc, "downdoc", "Auto download Swagger file when does not exist") cmdRun.Flag.Var(&excludedPaths, "e", "Excluded paths[].") cmdRun.Flag.StringVar(&buildTags, "tags", "", "Build tags (https://golang.org/pkg/go/build/)") - exit = make(chan bool) } +var ( + currpath = "" + appname = "" + exit = make(chan bool) +) + func runApp(cmd *Command, args []string) int { ShowShortVersionBanner() From 24955fb0441a4f6e50a4cc034ff156a149c25068 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Thu, 28 Jul 2016 14:19:42 +0200 Subject: [PATCH 5/7] Fixed merge conflict --- .gitignore | 1 + run.go | 37 +++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index ffab5d0..a4c4e18 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # Folders _obj _test +.idea # Architecture specific extensions/prefixes *.[568vq] diff --git a/run.go b/run.go index b9d40ed..db1ceed 100644 --- a/run.go +++ b/run.go @@ -20,6 +20,7 @@ import ( path "path/filepath" "runtime" "strings" + "fmt" ) var cmdRun = &Command{ @@ -32,18 +33,23 @@ it will recompile and restart the app after any modifications. `, } -var mainFiles ListOpts - -var downdoc docValue -var gendoc docValue - -// The flags list of the paths excluded from watching -var excludedPaths strFlags - -// Pass through to -tags arg of "go build" -var buildTags string - -var vendorWatch bool +var ( + mainFiles ListOpts + downdoc docValue + gendoc docValue + // 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 +) func init() { cmdRun.Run = runApp @@ -53,14 +59,9 @@ func init() { cmdRun.Flag.Var(&excludedPaths, "e", "Excluded paths[].") cmdRun.Flag.BoolVar(&vendorWatch, "vendor", false, "Watch vendor folder") cmdRun.Flag.StringVar(&buildTags, "tags", "", "Build tags (https://golang.org/pkg/go/build/)") + exit = make(chan bool) } -var ( - currpath = "" - appname = "" - exit = make(chan bool) -) - func runApp(cmd *Command, args []string) int { ShowShortVersionBanner() From 5bc1c82a2b0d9aa16172663fd07e63cecb52e094 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Fri, 29 Jul 2016 17:45:15 +0200 Subject: [PATCH 6/7] Added check for multiple paths in GOPATH (instead of the first path in the list) --- run.go | 32 ++++++++++++++++---------------- util.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/run.go b/run.go index db1ceed..a3af6c3 100644 --- a/run.go +++ b/run.go @@ -49,6 +49,8 @@ var ( exit chan bool // Flag to watch the vendor folder vendorWatch bool + // Current user workspace + currentGoPath string ) func init() { @@ -65,26 +67,24 @@ func init() { func runApp(cmd *Command, args []string) int { ShowShortVersionBanner() - gps := GetGOPATHs() - if len(gps) == 0 { - ColorLog("[ERRO] Fail to start[ %s ]\n", "$GOPATH is not set or empty") - os.Exit(2) - } - gopath := gps[0] - if len(args) == 0 || args[0] == "watchall" { currpath, _ = os.Getwd() + + if !isBeegoProject(currpath) { + exitPrint(fmt.Sprintf("Bee does not support non Beego project: %s", currpath)) + } + + _, currentGoPath, _ = SearchGOPATHs(currpath) appname = path.Base(currpath) ColorLog("[INFO] Uses '%s' as 'appname'\n", appname) } else { - gopathsrc := path.Join(gopath, "src") - currpath = path.Join(gopathsrc, args[0]) - appname = path.Base(currpath) - - // Check if passed Bee application path/name exists - // in $GOPATH/src workspace - if !isExist(currpath) { - panic(fmt.Sprintf("No Beego application '%s' found in GOPATH: %s", args[0], gopathsrc)) + // Check if passed Bee application path/name exists in the GOPATH(s) + if ok, _gopath, _path := SearchGOPATHs(args[0]); ok { + currpath = _path + currentGoPath = _gopath + appname = path.Base(currpath) + } else { + panic(fmt.Sprintf("No Beego application '%s' found in your GOPATH", args[0])) } ColorLog("[INFO] Uses '%s' as 'appname'\n", appname) @@ -110,7 +110,7 @@ func runApp(cmd *Command, args []string) int { // Because monitor files has some issues, we watch current directory // and ignore non-go files. for _, p := range conf.DirStruct.Others { - paths = append(paths, strings.Replace(p, "$GOPATH", gopath, -1)) + paths = append(paths, strings.Replace(p, "$GOPATH", currentGoPath, -1)) } files := []string{} diff --git a/util.go b/util.go index 0f314df..2cd76d3 100644 --- a/util.go +++ b/util.go @@ -15,13 +15,14 @@ package main import ( - "fmt" "log" "os" "path/filepath" "runtime" "strings" "time" + "path" + "fmt" ) // Go is a basic promise implementation: it wraps calls a function in a goroutine @@ -173,6 +174,34 @@ func GetGOPATHs() []string { return paths } +func SearchGOPATHs(app string) (bool, string, string) { + gps := GetGOPATHs() + if len(gps) == 0 { + ColorLog("[ERRO] Fail to start [ %s ]\n", "GOPATH environment variable is not set or empty") + os.Exit(2) + } + + // Lookup the application inside the user workspace(s) + for _, gopath := range gps { + var currentPath string + + if !strings.Contains(app, "src") { + gopathsrc := path.Join(gopath, "src") + currentPath = path.Join(gopathsrc, app) + } else { + currentPath = app + } + + if isExist(currentPath) { + if !isBeegoProject(currentPath) { + continue + } + return true, gopath, currentPath + } + } + return false, "", "" +} + // askForConfirmation uses Scanln to parse user input. A user must type in "yes" or "no" and // then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", and "Yes" all count as // confirmations. If the input is not recognized, it will ask again. The function does not return From 828b8d8b532b6d42ece7af091fc7c0cc2e69caa2 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Sat, 30 Jul 2016 13:42:38 +0200 Subject: [PATCH 7/7] Removed redundant beego project check --- run.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/run.go b/run.go index a3af6c3..d4bc44a 100644 --- a/run.go +++ b/run.go @@ -15,12 +15,12 @@ package main import ( + "fmt" "io/ioutil" "os" path "path/filepath" "runtime" "strings" - "fmt" ) var cmdRun = &Command{ @@ -35,8 +35,8 @@ it will recompile and restart the app after any modifications. var ( mainFiles ListOpts - downdoc docValue - gendoc docValue + downdoc docValue + gendoc docValue // The flags list of the paths excluded from watching excludedPaths strFlags // Pass through to -tags arg of "go build" @@ -70,16 +70,16 @@ func runApp(cmd *Command, args []string) int { if len(args) == 0 || args[0] == "watchall" { currpath, _ = os.Getwd() - if !isBeegoProject(currpath) { + if found, _gopath, _ := SearchGOPATHs(currpath); found { + appname = path.Base(currpath) + currentGoPath = _gopath + } else { exitPrint(fmt.Sprintf("Bee does not support non Beego project: %s", currpath)) } - - _, currentGoPath, _ = SearchGOPATHs(currpath) - appname = path.Base(currpath) - ColorLog("[INFO] Uses '%s' as 'appname'\n", appname) + ColorLog("[INFO] Using '%s' as 'appname'\n", appname) } else { // Check if passed Bee application path/name exists in the GOPATH(s) - if ok, _gopath, _path := SearchGOPATHs(args[0]); ok { + if found, _gopath, _path := SearchGOPATHs(args[0]); found { currpath = _path currentGoPath = _gopath appname = path.Base(currpath) @@ -87,7 +87,8 @@ func runApp(cmd *Command, args []string) int { panic(fmt.Sprintf("No Beego application '%s' found in your GOPATH", args[0])) } - ColorLog("[INFO] Uses '%s' as 'appname'\n", appname) + ColorLog("[INFO] Using '%s' as 'appname'\n", appname) + 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) ColorLog("[INFO] Do you want to overwrite it? [yes|no]] ")