From 4c1cfc13868e7779570dc7b3c6494c428160308d Mon Sep 17 00:00:00 2001 From: coseyo Date: Sun, 24 Jan 2016 00:18:16 +0800 Subject: [PATCH 1/4] fix path issue --- config.go | 11 ++++++++++- parser.go | 6 +++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index e91ca28b..d090d247 100644 --- a/config.go +++ b/config.go @@ -103,6 +103,8 @@ var ( BConfig *Config // AppConfig is the instance of Config, store the config information from file AppConfig *beegoAppConfig + // AppPath is the absolute path to the app + AppPath string // AppConfigPath is the path to the config files AppConfigPath string // AppConfigProvider is the provider for the config, default is ini @@ -179,9 +181,16 @@ func init() { // ParseConfig parsed default config file. // now only support ini, next will support json. func ParseConfig() (err error) { + AppPath, _ := filepath.Abs(filepath.Dir(os.Args[0])) + workPath, _ := os.Getwd() + workPath, _ = filepath.Abs(workPath) + + if workPath != AppPath { + os.Chdir(AppPath) + } + if AppConfigPath == "" { // initialize default configurations - AppPath, _ := filepath.Abs(filepath.Dir(os.Args[0])) AppConfigPath = filepath.Join(AppPath, "conf", "app.conf") if !utils.FileExists(AppConfigPath) { AppConfig = &beegoAppConfig{config.NewFakeConfig()} diff --git a/parser.go b/parser.go index b14d74b9..f23f4720 100644 --- a/parser.go +++ b/parser.go @@ -130,7 +130,7 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat } func genRouterCode() { - os.Mkdir("routers", 0755) + os.Mkdir(path.Join(AppPath, "routers"), 0755) Info("generate router from comments") var ( globalinfo string @@ -172,7 +172,7 @@ func genRouterCode() { } } if globalinfo != "" { - f, err := os.Create(path.Join("routers", commentFilename)) + f, err := os.Create(path.Join(AppPath, "routers", commentFilename)) if err != nil { panic(err) } @@ -182,7 +182,7 @@ func genRouterCode() { } func compareFile(pkgRealpath string) bool { - if !utils.FileExists(path.Join("routers", commentFilename)) { + if !utils.FileExists(path.Join(AppPath, "routers", commentFilename)) { return true } if utils.FileExists(lastupdateFilename) { From 3031bdd1762bfb4a04fb36ea764e4c1209a5c9d8 Mon Sep 17 00:00:00 2001 From: coseyo Date: Sun, 24 Jan 2016 00:40:03 +0800 Subject: [PATCH 2/4] fix test error --- config.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index d090d247..ea329596 100644 --- a/config.go +++ b/config.go @@ -185,18 +185,17 @@ func ParseConfig() (err error) { workPath, _ := os.Getwd() workPath, _ = filepath.Abs(workPath) - if workPath != AppPath { - os.Chdir(AppPath) - } - if AppConfigPath == "" { // initialize default configurations AppConfigPath = filepath.Join(AppPath, "conf", "app.conf") - if !utils.FileExists(AppConfigPath) { + if utils.FileExists(AppConfigPath) && workPath != AppPath { + os.Chdir(AppPath) + } else { AppConfig = &beegoAppConfig{config.NewFakeConfig()} return } } + AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath) if err != nil { return err From 09d3d89c6f0444f92c03a382bc2cdade17363af1 Mon Sep 17 00:00:00 2001 From: coseyo Date: Sun, 24 Jan 2016 00:47:37 +0800 Subject: [PATCH 3/4] fix test error again --- config.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index ea329596..3ceae12e 100644 --- a/config.go +++ b/config.go @@ -188,14 +188,16 @@ func ParseConfig() (err error) { if AppConfigPath == "" { // initialize default configurations AppConfigPath = filepath.Join(AppPath, "conf", "app.conf") - if utils.FileExists(AppConfigPath) && workPath != AppPath { - os.Chdir(AppPath) - } else { + if !utils.FileExists(AppConfigPath) { AppConfig = &beegoAppConfig{config.NewFakeConfig()} return } } + if workPath != AppPath { + os.Chdir(AppPath) + } + AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath) if err != nil { return err From d1481ea659253f6f66bae053fd358f66c68f384c Mon Sep 17 00:00:00 2001 From: coseyo Date: Mon, 25 Jan 2016 23:00:09 +0800 Subject: [PATCH 4/4] move assignment to init --- config.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 3ceae12e..bf9077a7 100644 --- a/config.go +++ b/config.go @@ -113,9 +113,15 @@ var ( TemplateCache map[string]*template.Template // GlobalSessions is the instance for the session manager GlobalSessions *session.Manager + + workPath string ) func init() { + AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0])) + workPath, _ = os.Getwd() + workPath, _ = filepath.Abs(workPath) + BConfig = &Config{ AppName: "beego", RunMode: DEV, @@ -181,10 +187,6 @@ func init() { // ParseConfig parsed default config file. // now only support ini, next will support json. func ParseConfig() (err error) { - AppPath, _ := filepath.Abs(filepath.Dir(os.Args[0])) - workPath, _ := os.Getwd() - workPath, _ = filepath.Abs(workPath) - if AppConfigPath == "" { // initialize default configurations AppConfigPath = filepath.Join(AppPath, "conf", "app.conf")