1
0
mirror of https://github.com/beego/bee.git synced 2024-12-22 15:10:49 +00:00

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. 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. This is mainly used for zealots who are requiring 100% Go code.
`, `,
PreRun: func(cmd *commands.Command, args []string) { PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
version.ShowShortVersionBanner() Run: runBale,
config.LoadConfig()
},
Run: runBale,
} }
func init() { 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") beeLogger.Log.Fatal("Wrong number of arguments. Run: bee help generate")
} }
config.LoadConfig()
cmd.Flag.Parse(args[2:]) cmd.Flag.Parse(args[2:])
if generate.SQLDriver == "" { if generate.SQLDriver == "" {
generate.SQLDriver = utils.DocValue(config.Conf.Database.Driver) 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) { func appCode(cmd *commands.Command, args []string, currpath string) {
config.LoadConfig()
cmd.Flag.Parse(args[1:]) cmd.Flag.Parse(args[1:])
if generate.SQLDriver == "" { if generate.SQLDriver == "" {
generate.SQLDriver = utils.DocValue(config.Conf.Database.Driver) 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"] $ bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
`, `,
PreRun: func(cmd *commands.Command, args []string) { PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
version.ShowShortVersionBanner() Run: RunMigration,
config.LoadConfig()
},
Run: RunMigration,
} }
var mDriver utils.DocValue 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. Run command will supervise the filesystem of the application for any changes, and recompile/restart it.
`, `,
PreRun: func(cmd *commands.Command, args []string) { PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
version.ShowShortVersionBanner() Run: RunApp,
config.LoadConfig()
},
Run: RunApp,
} }
var ( var (

View File

@ -15,7 +15,6 @@ package config
import ( import (
"encoding/json" "encoding/json"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
@ -86,42 +85,43 @@ type database struct {
// It looks for Beefile or bee.json in the current path, // It looks for Beefile or bee.json in the current path,
// and falls back to default configuration in case not found. // and falls back to default configuration in case not found.
func LoadConfig() { func LoadConfig() {
err := filepath.Walk(".", func(path string, fileInfo os.FileInfo, err error) error { currentPath, err := os.Getwd()
if err != nil { if err != nil {
return nil beeLogger.Log.Error(err.Error())
} }
if fileInfo.IsDir() { dir, err := os.Open(currentPath)
return nil 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": case "bee.json":
{ {
beeLogger.Log.Info("Loading configuration from '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 { if err != nil {
beeLogger.Log.Errorf("Failed to parse JSON file: %s", err) beeLogger.Log.Errorf("Failed to parse JSON file: %s", err)
return err
} }
return io.EOF break
} }
case "Beefile": case "Beefile":
{ {
beeLogger.Log.Info("Loading configuration from 'Beefile'...") beeLogger.Log.Info("Loading configuration from 'Beefile'...")
err = parseYAML(path, &Conf) err = parseYAML(filepath.Join(currentPath, file.Name()), &Conf)
if err != nil { if err != nil {
beeLogger.Log.Errorf("Failed to parse YAML file: %s", err) 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 // Check format version
@ -138,7 +138,6 @@ func LoadConfig() {
if len(Conf.DirStruct.Models) == 0 { if len(Conf.DirStruct.Models) == 0 {
Conf.DirStruct.Models = "models" Conf.DirStruct.Models = "models"
} }
return
} }
func parseJSON(path string, v interface{}) error { 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 package main
import ( import (
@ -8,6 +21,7 @@ import (
"github.com/beego/bee/cmd" "github.com/beego/bee/cmd"
"github.com/beego/bee/cmd/commands" "github.com/beego/bee/cmd/commands"
"github.com/beego/bee/config"
"github.com/beego/bee/generate/swaggergen" "github.com/beego/bee/generate/swaggergen"
"github.com/beego/bee/utils" "github.com/beego/bee/utils"
) )
@ -46,6 +60,8 @@ func main() {
c.PreRun(c, args) c.PreRun(c, args)
} }
config.LoadConfig()
// Check if current directory is inside the GOPATH, // Check if current directory is inside the GOPATH,
// if so parse the packages inside it. // if so parse the packages inside it.
if strings.Contains(currentpath, utils.GetGOPATHs()[0]+"/src") && cmd.IfGenerateDocs(c.Name(), args) { if strings.Contains(currentpath, utils.GetGOPATHs()[0]+"/src") && cmd.IfGenerateDocs(c.Name(), args) {