diff --git a/cmd/commands/bale/bale.go b/cmd/commands/bale/bale.go index 7fcf9c6..8ba9641 100644 --- a/cmd/commands/bale/bale.go +++ b/cmd/commands/bale/bale.go @@ -42,11 +42,8 @@ var CmdBale = &commands.Command{ It will auto-generate an unpack function to the main package then run it during the runtime. This is mainly used for zealots who are requiring 100% Go code. `, - PreRun: func(cmd *commands.Command, args []string) { - version.ShowShortVersionBanner() - config.LoadConfig() - }, - Run: runBale, + PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() }, + Run: runBale, } func init() { diff --git a/cmd/commands/generate/generate.go b/cmd/commands/generate/generate.go index d7aedf8..edf4691 100644 --- a/cmd/commands/generate/generate.go +++ b/cmd/commands/generate/generate.go @@ -117,8 +117,6 @@ func scaffold(cmd *commands.Command, args []string, currpath string) { beeLogger.Log.Fatal("Wrong number of arguments. Run: bee help generate") } - config.LoadConfig() - cmd.Flag.Parse(args[2:]) if generate.SQLDriver == "" { generate.SQLDriver = utils.DocValue(config.Conf.Database.Driver) @@ -141,8 +139,6 @@ func scaffold(cmd *commands.Command, args []string, currpath string) { } func appCode(cmd *commands.Command, args []string, currpath string) { - config.LoadConfig() - cmd.Flag.Parse(args[1:]) if generate.SQLDriver == "" { generate.SQLDriver = utils.DocValue(config.Conf.Database.Driver) diff --git a/cmd/commands/migrate/migrate.go b/cmd/commands/migrate/migrate.go index f1964d9..1870945 100644 --- a/cmd/commands/migrate/migrate.go +++ b/cmd/commands/migrate/migrate.go @@ -52,11 +52,8 @@ var CmdMigrate = &commands.Command{ $ bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] `, - PreRun: func(cmd *commands.Command, args []string) { - version.ShowShortVersionBanner() - config.LoadConfig() - }, - Run: RunMigration, + PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() }, + Run: RunMigration, } var mDriver utils.DocValue diff --git a/cmd/commands/run/run.go b/cmd/commands/run/run.go index aa32286..6820024 100644 --- a/cmd/commands/run/run.go +++ b/cmd/commands/run/run.go @@ -34,11 +34,8 @@ var CmdRun = &commands.Command{ Run command will supervise the filesystem of the application for any changes, and recompile/restart it. `, - PreRun: func(cmd *commands.Command, args []string) { - version.ShowShortVersionBanner() - config.LoadConfig() - }, - Run: RunApp, + PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() }, + Run: RunApp, } var ( diff --git a/config/conf.go b/config/conf.go index 717e588..529b7ae 100644 --- a/config/conf.go +++ b/config/conf.go @@ -15,7 +15,6 @@ package config import ( "encoding/json" - "io" "io/ioutil" "os" @@ -86,42 +85,43 @@ type database struct { // It looks for Beefile or bee.json in the current path, // and falls back to default configuration in case not found. func LoadConfig() { - err := filepath.Walk(".", func(path string, fileInfo os.FileInfo, err error) error { - if err != nil { - return nil - } + currentPath, err := os.Getwd() + if err != nil { + beeLogger.Log.Error(err.Error()) + } - if fileInfo.IsDir() { - return nil - } + dir, err := os.Open(currentPath) + if err != nil { + beeLogger.Log.Error(err.Error()) + } + defer dir.Close() - switch fileInfo.Name() { + files, err := dir.Readdir(-1) + if err != nil { + beeLogger.Log.Error(err.Error()) + } + + for _, file := range files { + switch file.Name() { case "bee.json": { beeLogger.Log.Info("Loading configuration from 'bee.json'...") - err = parseJSON(path, &Conf) + err = parseJSON(filepath.Join(currentPath, file.Name()), &Conf) if err != nil { beeLogger.Log.Errorf("Failed to parse JSON file: %s", err) - return err } - return io.EOF + break } case "Beefile": { beeLogger.Log.Info("Loading configuration from 'Beefile'...") - err = parseYAML(path, &Conf) + err = parseYAML(filepath.Join(currentPath, file.Name()), &Conf) if err != nil { beeLogger.Log.Errorf("Failed to parse YAML file: %s", err) - return err } - return io.EOF + break } } - return nil - }) - - if err != io.EOF { - beeLogger.Log.Info("Loading default configuration...") } // Check format version @@ -138,7 +138,6 @@ func LoadConfig() { if len(Conf.DirStruct.Models) == 0 { Conf.DirStruct.Models = "models" } - return } func parseJSON(path string, v interface{}) error { diff --git a/main.go b/main.go index 7a05f42..d9401dc 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,16 @@ +// 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. package main import ( @@ -8,6 +21,7 @@ import ( "github.com/beego/bee/cmd" "github.com/beego/bee/cmd/commands" + "github.com/beego/bee/config" "github.com/beego/bee/generate/swaggergen" "github.com/beego/bee/utils" ) @@ -46,6 +60,8 @@ func main() { c.PreRun(c, args) } + config.LoadConfig() + // Check if current directory is inside the GOPATH, // if so parse the packages inside it. if strings.Contains(currentpath, utils.GetGOPATHs()[0]+"/src") && cmd.IfGenerateDocs(c.Name(), args) {