1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 10:10:53 +00:00
go simple
golint
structcheck
staticcheck
unused
unconvert
This commit is contained in:
Sergey Lanzman 2017-03-11 10:57:06 +02:00
parent 3d5b13d84e
commit feea8877c0
11 changed files with 71 additions and 86 deletions

View File

@ -16,8 +16,6 @@
package cmd package cmd
import ( import (
"os"
"github.com/beego/bee/cmd/commands" "github.com/beego/bee/cmd/commands"
_ "github.com/beego/bee/cmd/commands/api" _ "github.com/beego/bee/cmd/commands/api"
_ "github.com/beego/bee/cmd/commands/bale" _ "github.com/beego/bee/cmd/commands/bale"
@ -79,7 +77,6 @@ Use {{"bee help" | bold}} for more information.
func Usage() { func Usage() {
utils.Tmpl(usageTemplate, commands.AvailableCommands) utils.Tmpl(usageTemplate, commands.AvailableCommands)
os.Exit(2)
} }
func Help(args []string) { func Help(args []string) {

View File

@ -57,7 +57,7 @@ var CmdApiapp = &commands.Command{
user.go user.go
`, `,
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() }, PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
Run: createapi, Run: createAPI,
} }
var apiconf = `appname = {{.Appname}} var apiconf = `appname = {{.Appname}}
httpport = 8080 httpport = 8080
@ -139,7 +139,7 @@ func init() {
} }
` `
var ApiModels = `package models var APIModels = `package models
import ( import (
"errors" "errors"
@ -194,7 +194,7 @@ func Delete(ObjectId string) {
` `
var ApiModels2 = `package models var APIModels2 = `package models
import ( import (
"errors" "errors"
@ -543,7 +543,7 @@ func init() {
commands.AvailableCommands = append(commands.AvailableCommands, CmdApiapp) commands.AvailableCommands = append(commands.AvailableCommands, CmdApiapp)
} }
func createapi(cmd *commands.Command, args []string) int { func createAPI(cmd *commands.Command, args []string) int {
output := cmd.Out() output := cmd.Out()
if len(args) < 1 { if len(args) < 1 {
@ -551,10 +551,13 @@ func createapi(cmd *commands.Command, args []string) int {
} }
if len(args) > 1 { if len(args) > 1 {
cmd.Flag.Parse(args[1:]) err := cmd.Flag.Parse(args[1:])
if err != nil {
beeLogger.Log.Error(err.Error())
}
} }
apppath, packpath, err := utils.CheckEnv(args[0]) appPath, packPath, err := utils.CheckEnv(args[0])
if err != nil { if err != nil {
beeLogger.Log.Fatalf("%s", err) beeLogger.Log.Fatalf("%s", err)
} }
@ -564,30 +567,30 @@ func createapi(cmd *commands.Command, args []string) int {
beeLogger.Log.Info("Creating API...") beeLogger.Log.Info("Creating API...")
os.MkdirAll(apppath, 0755) os.MkdirAll(appPath, 0755)
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath, "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", appPath, "\x1b[0m")
os.Mkdir(path.Join(apppath, "conf"), 0755) os.Mkdir(path.Join(appPath, "conf"), 0755)
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "conf"), "\x1b[0m")
os.Mkdir(path.Join(apppath, "controllers"), 0755) os.Mkdir(path.Join(appPath, "controllers"), 0755)
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "controllers"), "\x1b[0m")
os.Mkdir(path.Join(apppath, "tests"), 0755) os.Mkdir(path.Join(appPath, "tests"), 0755)
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "tests"), "\x1b[0m")
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf", "app.conf"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "conf", "app.conf"), "\x1b[0m")
utils.WriteToFile(path.Join(apppath, "conf", "app.conf"), utils.WriteToFile(path.Join(appPath, "conf", "app.conf"),
strings.Replace(apiconf, "{{.Appname}}", path.Base(args[0]), -1)) strings.Replace(apiconf, "{{.Appname}}", path.Base(args[0]), -1))
if generate.SQLConn != "" { if generate.SQLConn != "" {
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "main.go"), "\x1b[0m")
maingoContent := strings.Replace(apiMainconngo, "{{.Appname}}", packpath, -1) mainGoContent := strings.Replace(apiMainconngo, "{{.Appname}}", packPath, -1)
maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(generate.SQLDriver), -1) mainGoContent = strings.Replace(mainGoContent, "{{.DriverName}}", string(generate.SQLDriver), -1)
if generate.SQLDriver == "mysql" { if generate.SQLDriver == "mysql" {
maingoContent = strings.Replace(maingoContent, "{{.DriverPkg}}", `_ "github.com/go-sql-driver/mysql"`, -1) mainGoContent = strings.Replace(mainGoContent, "{{.DriverPkg}}", `_ "github.com/go-sql-driver/mysql"`, -1)
} else if generate.SQLDriver == "postgres" { } else if generate.SQLDriver == "postgres" {
maingoContent = strings.Replace(maingoContent, "{{.DriverPkg}}", `_ "github.com/lib/pq"`, -1) mainGoContent = strings.Replace(mainGoContent, "{{.DriverPkg}}", `_ "github.com/lib/pq"`, -1)
} }
utils.WriteToFile(path.Join(apppath, "main.go"), utils.WriteToFile(path.Join(appPath, "main.go"),
strings.Replace( strings.Replace(
maingoContent, mainGoContent,
"{{.conn}}", "{{.conn}}",
generate.SQLConn.String(), generate.SQLConn.String(),
-1, -1,
@ -596,38 +599,38 @@ func createapi(cmd *commands.Command, args []string) int {
beeLogger.Log.Infof("Using '%s' as 'driver'", generate.SQLDriver) beeLogger.Log.Infof("Using '%s' as 'driver'", generate.SQLDriver)
beeLogger.Log.Infof("Using '%s' as 'conn'", generate.SQLConn) beeLogger.Log.Infof("Using '%s' as 'conn'", generate.SQLConn)
beeLogger.Log.Infof("Using '%s' as 'tables'", generate.Tables) beeLogger.Log.Infof("Using '%s' as 'tables'", generate.Tables)
generate.GenerateAppcode(string(generate.SQLDriver), string(generate.SQLConn), "3", string(generate.Tables), apppath) generate.GenerateAppcode(string(generate.SQLDriver), string(generate.SQLConn), "3", string(generate.Tables), appPath)
} else { } else {
os.Mkdir(path.Join(apppath, "models"), 0755) os.Mkdir(path.Join(appPath, "models"), 0755)
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "models"), "\x1b[0m")
os.Mkdir(path.Join(apppath, "routers"), 0755) os.Mkdir(path.Join(appPath, "routers"), 0755)
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers")+string(path.Separator), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "routers")+string(path.Separator), "\x1b[0m")
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers", "object.go"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "controllers", "object.go"), "\x1b[0m")
utils.WriteToFile(path.Join(apppath, "controllers", "object.go"), utils.WriteToFile(path.Join(appPath, "controllers", "object.go"),
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1)) strings.Replace(apiControllers, "{{.Appname}}", packPath, -1))
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers", "user.go"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "controllers", "user.go"), "\x1b[0m")
utils.WriteToFile(path.Join(apppath, "controllers", "user.go"), utils.WriteToFile(path.Join(appPath, "controllers", "user.go"),
strings.Replace(apiControllers2, "{{.Appname}}", packpath, -1)) strings.Replace(apiControllers2, "{{.Appname}}", packPath, -1))
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests", "default_test.go"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "tests", "default_test.go"), "\x1b[0m")
utils.WriteToFile(path.Join(apppath, "tests", "default_test.go"), utils.WriteToFile(path.Join(appPath, "tests", "default_test.go"),
strings.Replace(apiTests, "{{.Appname}}", packpath, -1)) strings.Replace(apiTests, "{{.Appname}}", packPath, -1))
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers", "router.go"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "routers", "router.go"), "\x1b[0m")
utils.WriteToFile(path.Join(apppath, "routers", "router.go"), utils.WriteToFile(path.Join(appPath, "routers", "router.go"),
strings.Replace(apirouter, "{{.Appname}}", packpath, -1)) strings.Replace(apirouter, "{{.Appname}}", packPath, -1))
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "object.go"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "models", "object.go"), "\x1b[0m")
utils.WriteToFile(path.Join(apppath, "models", "object.go"), ApiModels) utils.WriteToFile(path.Join(appPath, "models", "object.go"), APIModels)
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "user.go"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "models", "user.go"), "\x1b[0m")
utils.WriteToFile(path.Join(apppath, "models", "user.go"), ApiModels2) utils.WriteToFile(path.Join(appPath, "models", "user.go"), APIModels2)
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "main.go"), "\x1b[0m")
utils.WriteToFile(path.Join(apppath, "main.go"), utils.WriteToFile(path.Join(appPath, "main.go"),
strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1)) strings.Replace(apiMaingo, "{{.Appname}}", packPath, -1))
} }
beeLogger.Log.Success("New API successfully created!") beeLogger.Log.Success("New API successfully created!")
return 0 return 0

View File

@ -143,7 +143,7 @@ func saveFile(filePath string, b []byte) (int, error) {
var resFiles = make([]string, 0, 10) var resFiles = make([]string, 0, 10)
func walkFn(resPath string, info os.FileInfo, err error) error { func walkFn(resPath string, info os.FileInfo, _ error) error {
if info.IsDir() || filterSuffix(resPath) { if info.IsDir() || filterSuffix(resPath) {
return nil return nil
} }

View File

@ -105,10 +105,10 @@ func createhprose(cmd *commands.Command, args []string) int {
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models"), "\x1b[0m")
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "object.go"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "object.go"), "\x1b[0m")
utils.WriteToFile(path.Join(apppath, "models", "object.go"), apiapp.ApiModels) utils.WriteToFile(path.Join(apppath, "models", "object.go"), apiapp.APIModels)
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "user.go"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "user.go"), "\x1b[0m")
utils.WriteToFile(path.Join(apppath, "models", "user.go"), apiapp.ApiModels2) utils.WriteToFile(path.Join(apppath, "models", "user.go"), apiapp.APIModels2)
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m") fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
utils.WriteToFile(path.Join(apppath, "main.go"), utils.WriteToFile(path.Join(apppath, "main.go"),

View File

@ -178,12 +178,10 @@ func RunApp(cmd *commands.Command, args []string) int {
} }
for { for {
select { <-exit
case <-exit:
runtime.Goexit() runtime.Goexit()
} }
} }
}
func readAppDirectories(directory string, paths *[]string) { func readAppDirectories(directory string, paths *[]string) {
fileInfos, err := ioutil.ReadDir(directory) fileInfos, err := ioutil.ReadDir(directory)

View File

@ -123,7 +123,7 @@ func GetBeegoVersion() string {
return "" return ""
} }
if gopath == "" { if gopath == "" {
err = fmt.Errorf("You need to set GOPATH environment variable") beeLogger.Log.Error("You need to set GOPATH environment variable")
return "" return ""
} }
wgopath := path.SplitList(gopath) wgopath := path.SplitList(gopath)

View File

@ -19,7 +19,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"io"
"path/filepath" "path/filepath"
beeLogger "github.com/beego/bee/logger" beeLogger "github.com/beego/bee/logger"
@ -98,32 +97,22 @@ func loadConfig() {
if err != nil { if err != nil {
return nil return nil
} }
if fileInfo.IsDir() { if fileInfo.IsDir() {
return nil return nil
} }
switch fileInfo.Name() {
if fileInfo.Name() == "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) return parseJSON(path, &Conf)
if err != nil { case "Beefile":
beeLogger.Log.Errorf("Failed to parse JSON file: %s", err)
return err
}
return io.EOF
}
if fileInfo.Name() == "Beefile" {
beeLogger.Log.Info("Loading configuration from 'Beefile'...") beeLogger.Log.Info("Loading configuration from 'Beefile'...")
err = parseYAML(path, &Conf) return parseYAML(path, &Conf)
if err != nil {
beeLogger.Log.Errorf("Failed to parse YAML file: %s", err)
return err
}
return io.EOF
} }
return nil return nil
}) })
if err != nil {
beeLogger.Log.Errorf("Failed to parse config file: %s", err)
}
// Check format version // Check format version
if Conf.Version != confVer { if Conf.Version != confVer {
beeLogger.Log.Warn("Your configuration file is outdated. Please do consider updating it.") beeLogger.Log.Warn("Your configuration file is outdated. Please do consider updating it.")

View File

@ -732,7 +732,7 @@ func getModel(str string) (objectname string, m swagger.Schema, realTypes []stri
func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string, astPkgs map[string]*ast.Package, packageName string) { func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string, astPkgs map[string]*ast.Package, packageName string) {
ts, ok := d.Decl.(*ast.TypeSpec) ts, ok := d.Decl.(*ast.TypeSpec)
if !ok { if !ok {
// beeLogger.Log.Fatalf("Unknown type without TypeSec: %v\n", d) beeLogger.Log.Fatalf("Unknown type without TypeSec: %v\n", d)
} }
// TODO support other types, such as `ArrayType`, `MapType`, `InterfaceType` etc... // TODO support other types, such as `ArrayType`, `MapType`, `InterfaceType` etc...
st, ok := ts.Type.(*ast.StructType) st, ok := ts.Type.(*ast.StructType)

View File

@ -23,6 +23,8 @@ func main() {
if len(args) < 1 { if len(args) < 1 {
cmd.Usage() cmd.Usage()
os.Exit(2)
return
} }
if args[0] == "help" { if args[0] == "help" {

View File

@ -28,7 +28,7 @@ func Notify(text, title string) {
} }
func osxNotify(text, title string) { func osxNotify(text, title string) {
cmd := &exec.Cmd{} var cmd *exec.Cmd
if existTerminalNotifier() { if existTerminalNotifier() {
cmd = exec.Command("terminal-notifier", "-title", appName, "-message", text, "-subtitle", title) cmd = exec.Command("terminal-notifier", "-title", appName, "-message", text, "-subtitle", title)
} else if MacOSVersionSupport() { } else if MacOSVersionSupport() {
@ -53,13 +53,9 @@ func existTerminalNotifier() bool {
err := cmd.Start() err := cmd.Start()
if err != nil { if err != nil {
return false return false
} else { }
err = cmd.Wait() err = cmd.Wait()
if err != nil { return err != nil
return false
}
}
return true
} }
func MacOSVersionSupport() bool { func MacOSVersionSupport() bool {

View File

@ -320,7 +320,7 @@ func CheckEnv(appname string) (apppath, packpath string, err error) {
apppath = path.Join(gosrcpath, appname) apppath = path.Join(gosrcpath, appname)
if _, e := os.Stat(apppath); !os.IsNotExist(e) { if _, e := os.Stat(apppath); !os.IsNotExist(e) {
err = fmt.Errorf("Cannot create application without removing '%s' first.", apppath) err = fmt.Errorf("Cannot create application without removing '%s' first", apppath)
beeLogger.Log.Errorf("Path '%s' already exists", apppath) beeLogger.Log.Errorf("Path '%s' already exists", apppath)
return return
} }