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) { func checkEnv(appname string) (apppath, packpath string, err error) {
gopath := os.Getenv("GOPATH") gps := GetGOPATHs()
Debugf("gopath:%s", gopath) if len(gps) == 0 {
if gopath == "" { ColorLog("[ERRO] Fail to start[ %s ]\n", "GOPATH environment variable is not set or empty")
err = fmt.Errorf("you should set GOPATH in the env") os.Exit(2)
return
} }
// 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") gosrcpath := path.Join(gopath, "src")
apppath = path.Join(gosrcpath, appname) apppath = path.Join(gosrcpath, appname)
if _, e := os.Stat(apppath); os.IsNotExist(e) == false { if _, e := os.Stat(apppath); os.IsNotExist(e) == false {
err = fmt.Errorf("Cannot create application without removing `%s` first.", apppath) err = fmt.Errorf("Cannot create application without removing '%s' first.", apppath)
ColorLog("[ERRO] Path `%s` already exists\n", apppath) ColorLog("[ERRO] Path '%s' already exists\n", apppath)
return return
} }
packpath = strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/") 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 { func generateCode(cmd *Command, args []string) int {
ShowShortVersionBanner() ShowShortVersionBanner()
curpath, _ := os.Getwd() currpath, _ := os.Getwd()
if len(args) < 1 { if len(args) < 1 {
ColorLog("[ERRO] command is missing\n") ColorLog("[ERRO] command is missing\n")
os.Exit(2) os.Exit(2)
} }
gopath := os.Getenv("GOPATH") gps := GetGOPATHs()
Debugf("gopath:%s", gopath) if len(gps) == 0 {
if gopath == "" { ColorLog("[ERRO] Fail to start[ %s ]\n", "GOPATH environment variable is not set or empty")
ColorLog("[ERRO] $GOPATH not found\n")
ColorLog("[HINT] Set $GOPATH in your environment vairables\n")
os.Exit(2) os.Exit(2)
} }
gopath := gps[0]
Debugf("GOPATH: %s", gopath)
gcmd := args[0] gcmd := args[0]
switch gcmd { switch gcmd {
@ -124,9 +124,9 @@ func generateCode(cmd *Command, args []string) int {
os.Exit(2) os.Exit(2)
} }
sname := args[1] sname := args[1]
generateScaffold(sname, fields.String(), curpath, driver.String(), conn.String()) generateScaffold(sname, fields.String(), currpath, driver.String(), conn.String())
case "docs": case "docs":
generateDocs(curpath) generateDocs(currpath)
case "appcode": case "appcode":
// load config // load config
err := loadConfig() 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 'conn'\n", conn)
ColorLog("[INFO] Using '%s' as 'tables'\n", tables) ColorLog("[INFO] Using '%s' as 'tables'\n", tables)
ColorLog("[INFO] Using '%s' as 'level'\n", level) 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": case "migration":
if len(args) < 2 { if len(args) < 2 {
ColorLog("[ERRO] Wrong number of arguments\n") ColorLog("[ERRO] Wrong number of arguments\n")
@ -176,11 +176,11 @@ func generateCode(cmd *Command, args []string) int {
downsql = strings.Replace(downsql, "`", "", -1) downsql = strings.Replace(downsql, "`", "", -1)
} }
} }
generateMigration(mname, upsql, downsql, curpath) generateMigration(mname, upsql, downsql, currpath)
case "controller": case "controller":
if len(args) == 2 { if len(args) == 2 {
cname := args[1] cname := args[1]
generateController(cname, curpath) generateController(cname, currpath)
} else { } else {
ColorLog("[ERRO] Wrong number of arguments\n") ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: bee generate controller [controllername]\n") ColorLog("[HINT] Usage: bee generate controller [controllername]\n")
@ -199,18 +199,18 @@ func generateCode(cmd *Command, args []string) int {
os.Exit(2) os.Exit(2)
} }
sname := args[1] sname := args[1]
generateModel(sname, fields.String(), curpath) generateModel(sname, fields.String(), currpath)
case "view": case "view":
if len(args) == 2 { if len(args) == 2 {
cname := args[1] cname := args[1]
generateView(cname, curpath) generateView(cname, currpath)
} else { } else {
ColorLog("[ERRO] Wrong number of arguments\n") ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: bee generate view [viewpath]\n") ColorLog("[HINT] Usage: bee generate view [viewpath]\n")
os.Exit(2) os.Exit(2)
} }
default: default:
ColorLog("[ERRO] command is missing\n") ColorLog("[ERRO] Command is missing\n")
} }
ColorLog("[SUCC] %s successfully generated!\n", strings.Title(gcmd)) ColorLog("[SUCC] %s successfully generated!\n", strings.Title(gcmd))
return 0 return 0

View File

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

12
new.go
View File

@ -64,13 +64,15 @@ func createApp(cmd *Command, args []string) int {
os.Exit(2) os.Exit(2)
} }
gopath := os.Getenv("GOPATH") gps := GetGOPATHs()
Debugf("gopath:%s", gopath) if len(gps) == 0 {
if gopath == "" { ColorLog("[ERRO] Fail to start[ %s ]\n", "GOPATH environment variable is not set or empty")
ColorLog("[ERRO] $GOPATH not found\n")
ColorLog("[HINT] Set $GOPATH in your environment variables\n")
os.Exit(2) 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 gosrcpath := path.Join(gopath, "src") // User's workspace
apppath := path.Join(gosrcpath, args[0]) apppath := path.Join(gosrcpath, args[0])