1
0
mirror of https://github.com/beego/bee.git synced 2024-11-25 20:10:55 +00:00

Added support for multiple paths GOPATH

This commit is contained in:
Faissal Elamraoui 2016-08-01 11:42:16 +02:00
parent 6bf3ea6140
commit 00e358fc03
4 changed files with 52 additions and 46 deletions

View File

@ -647,19 +647,22 @@ func createapi(cmd *Command, args []string) int {
}
func checkEnv(appname string) (apppath, packpath string, err error) {
gopath := os.Getenv("GOPATH")
Debugf("gopath:%s", gopath)
if gopath == "" {
err = fmt.Errorf("you should set GOPATH in the env")
return
gps := GetGOPATHs()
if len(gps) == 0 {
ColorLog("[ERRO] Fail to start[ %s ]\n", "GOPATH environment variable is not set or empty")
os.Exit(2)
}
// In case of multiple paths in the GOPATH, by default
// we use the first path
gopath := gps[0]
Debugf("GOPATH: %s", gopath)
gosrcpath := path.Join(gopath, "src")
apppath = path.Join(gosrcpath, appname)
if _, e := os.Stat(apppath); os.IsNotExist(e) == false {
err = fmt.Errorf("Cannot create application without removing `%s` first.", apppath)
ColorLog("[ERRO] Path `%s` already exists\n", apppath)
err = fmt.Errorf("Cannot create application without removing '%s' first.", apppath)
ColorLog("[ERRO] Path '%s' already exists\n", apppath)
return
}
packpath = strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/")

28
g.go
View File

@ -79,19 +79,19 @@ func init() {
func generateCode(cmd *Command, args []string) int {
ShowShortVersionBanner()
curpath, _ := os.Getwd()
currpath, _ := os.Getwd()
if len(args) < 1 {
ColorLog("[ERRO] command is missing\n")
os.Exit(2)
}
gopath := os.Getenv("GOPATH")
Debugf("gopath:%s", gopath)
if gopath == "" {
ColorLog("[ERRO] $GOPATH not found\n")
ColorLog("[HINT] Set $GOPATH in your environment vairables\n")
gps := GetGOPATHs()
if len(gps) == 0 {
ColorLog("[ERRO] Fail to start[ %s ]\n", "GOPATH environment variable is not set or empty")
os.Exit(2)
}
gopath := gps[0]
Debugf("GOPATH: %s", gopath)
gcmd := args[0]
switch gcmd {
@ -124,9 +124,9 @@ func generateCode(cmd *Command, args []string) int {
os.Exit(2)
}
sname := args[1]
generateScaffold(sname, fields.String(), curpath, driver.String(), conn.String())
generateScaffold(sname, fields.String(), currpath, driver.String(), conn.String())
case "docs":
generateDocs(curpath)
generateDocs(currpath)
case "appcode":
// load config
err := loadConfig()
@ -157,7 +157,7 @@ func generateCode(cmd *Command, args []string) int {
ColorLog("[INFO] Using '%s' as 'conn'\n", conn)
ColorLog("[INFO] Using '%s' as 'tables'\n", tables)
ColorLog("[INFO] Using '%s' as 'level'\n", level)
generateAppcode(driver.String(), conn.String(), level.String(), tables.String(), curpath)
generateAppcode(driver.String(), conn.String(), level.String(), tables.String(), currpath)
case "migration":
if len(args) < 2 {
ColorLog("[ERRO] Wrong number of arguments\n")
@ -176,11 +176,11 @@ func generateCode(cmd *Command, args []string) int {
downsql = strings.Replace(downsql, "`", "", -1)
}
}
generateMigration(mname, upsql, downsql, curpath)
generateMigration(mname, upsql, downsql, currpath)
case "controller":
if len(args) == 2 {
cname := args[1]
generateController(cname, curpath)
generateController(cname, currpath)
} else {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: bee generate controller [controllername]\n")
@ -199,18 +199,18 @@ func generateCode(cmd *Command, args []string) int {
os.Exit(2)
}
sname := args[1]
generateModel(sname, fields.String(), curpath)
generateModel(sname, fields.String(), currpath)
case "view":
if len(args) == 2 {
cname := args[1]
generateView(cname, curpath)
generateView(cname, currpath)
} else {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: bee generate view [viewpath]\n")
os.Exit(2)
}
default:
ColorLog("[ERRO] command is missing\n")
ColorLog("[ERRO] Command is missing\n")
}
ColorLog("[SUCC] %s successfully generated!\n", strings.Title(gcmd))
return 0

View File

@ -64,15 +64,16 @@ func init() {
func runMigration(cmd *Command, args []string) int {
ShowShortVersionBanner()
crupath, _ := os.Getwd()
currpath, _ := os.Getwd()
gopath := os.Getenv("GOPATH")
Debugf("gopath:%s", gopath)
if gopath == "" {
ColorLog("[ERRO] $GOPATH not found\n")
ColorLog("[HINT] Set $GOPATH in your environment vairables\n")
gps := GetGOPATHs()
if len(gps) == 0 {
ColorLog("[ERRO] Fail to start[ %s ]\n", "GOPATH environment variable is not set or empty")
os.Exit(2)
}
gopath := gps[0]
Debugf("GOPATH: %s", gopath)
// load config
err := loadConfig()
if err != nil {
@ -100,19 +101,19 @@ func runMigration(cmd *Command, args []string) int {
if len(args) == 0 {
// run all outstanding migrations
ColorLog("[INFO] Running all outstanding migrations\n")
migrateUpdate(crupath, driverStr, connStr)
migrateUpdate(currpath, driverStr, connStr)
} else {
mcmd := args[0]
switch mcmd {
case "rollback":
ColorLog("[INFO] Rolling back the last migration operation\n")
migrateRollback(crupath, driverStr, connStr)
migrateRollback(currpath, driverStr, connStr)
case "reset":
ColorLog("[INFO] Reseting all migrations\n")
migrateReset(crupath, driverStr, connStr)
migrateReset(currpath, driverStr, connStr)
case "refresh":
ColorLog("[INFO] Refreshing all migrations\n")
migrateRefresh(crupath, driverStr, connStr)
migrateRefresh(currpath, driverStr, connStr)
default:
ColorLog("[ERRO] Command is missing\n")
os.Exit(2)
@ -123,28 +124,28 @@ func runMigration(cmd *Command, args []string) int {
}
// migrateUpdate does the schema update
func migrateUpdate(crupath, driver, connStr string) {
migrate("upgrade", crupath, driver, connStr)
func migrateUpdate(currpath, driver, connStr string) {
migrate("upgrade", currpath, driver, connStr)
}
// migrateRollback rolls back the latest migration
func migrateRollback(crupath, driver, connStr string) {
migrate("rollback", crupath, driver, connStr)
func migrateRollback(currpath, driver, connStr string) {
migrate("rollback", currpath, driver, connStr)
}
// migrateReset rolls back all migrations
func migrateReset(crupath, driver, connStr string) {
migrate("reset", crupath, driver, connStr)
func migrateReset(currpath, driver, connStr string) {
migrate("reset", currpath, driver, connStr)
}
// migrationRefresh rolls back all migrations and start over again
func migrateRefresh(crupath, driver, connStr string) {
migrate("refresh", crupath, driver, connStr)
func migrateRefresh(currpath, driver, connStr string) {
migrate("refresh", currpath, driver, connStr)
}
// migrate generates source code, build it, and invoke the binary who does the actual migration
func migrate(goal, crupath, driver, connStr string) {
dir := path.Join(crupath, "database", "migrations")
func migrate(goal, currpath, driver, connStr string) {
dir := path.Join(currpath, "database", "migrations")
binary := "m"
source := binary + ".go"
// connect to database

12
new.go
View File

@ -64,13 +64,15 @@ func createApp(cmd *Command, args []string) int {
os.Exit(2)
}
gopath := os.Getenv("GOPATH")
Debugf("gopath:%s", gopath)
if gopath == "" {
ColorLog("[ERRO] $GOPATH not found\n")
ColorLog("[HINT] Set $GOPATH in your environment variables\n")
gps := GetGOPATHs()
if len(gps) == 0 {
ColorLog("[ERRO] Fail to start[ %s ]\n", "GOPATH environment variable is not set or empty")
os.Exit(2)
}
// In case of multiple paths in the GOPATH, by default
// we use the first path
gopath := gps[0]
Debugf("GOPATH: %s", gopath)
gosrcpath := path.Join(gopath, "src") // User's workspace
apppath := path.Join(gosrcpath, args[0])