Load configuration for all commands

This removes the filepath.Walk() when loading configuration,
as it can read a Beefile from another project in the $GOPATH.
Now config.LoadConfig() is called for all available commands.
This commit is contained in:
Faissal Elamraoui 2017-03-15 15:18:33 +01:00
parent e57cc7b94e
commit 792cb3f311
6 changed files with 42 additions and 40 deletions

View File

@ -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() {

View File

@ -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)

View File

@ -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

View File

@ -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 (

View File

@ -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 {

16
main.go
View File

@ -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) {