mirror of
https://github.com/beego/bee.git
synced 2024-12-22 20:20: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:
parent
e57cc7b94e
commit
792cb3f311
@ -42,10 +42,7 @@ 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()
|
|
||||||
config.LoadConfig()
|
|
||||||
},
|
|
||||||
Run: runBale,
|
Run: runBale,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
@ -52,10 +52,7 @@ 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()
|
|
||||||
config.LoadConfig()
|
|
||||||
},
|
|
||||||
Run: RunMigration,
|
Run: RunMigration,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,10 +34,7 @@ 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()
|
|
||||||
config.LoadConfig()
|
|
||||||
},
|
|
||||||
Run: RunApp,
|
Run: RunApp,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
|
||||||
|
files, err := dir.Readdir(-1)
|
||||||
|
if err != nil {
|
||||||
|
beeLogger.Log.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
switch fileInfo.Name() {
|
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
16
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
|
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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user