From 172bc44b22709d83140191933657660721d678f2 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 24 Jul 2013 20:01:14 +0800 Subject: [PATCH] Add user conf support for bee.json --- main.go => bee.go | 2 +- bee.json | 5 +++ run.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++ start.go | 54 --------------------------- watch.go | 11 +++--- 5 files changed, 104 insertions(+), 61 deletions(-) rename main.go => bee.go (99%) create mode 100644 bee.json create mode 100644 run.go delete mode 100644 start.go diff --git a/main.go b/bee.go similarity index 99% rename from main.go rename to bee.go index 091ebd4..1ad6cb7 100644 --- a/main.go +++ b/bee.go @@ -58,7 +58,7 @@ func (c *Command) Runnable() bool { var commands = []*Command{ cmdNew, - cmdStart, + cmdRun, cmdPack, cmdApiapp, //cmdReStart, diff --git a/bee.json b/bee.json new file mode 100644 index 0000000..51dcd07 --- /dev/null +++ b/bee.json @@ -0,0 +1,5 @@ +{ + "dir_structure":{ + "controllers": "routers" + } +} \ No newline at end of file diff --git a/run.go b/run.go new file mode 100644 index 0000000..212652e --- /dev/null +++ b/run.go @@ -0,0 +1,93 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + path "path/filepath" + "runtime" +) + +var cmdRun = &Command{ + UsageLine: "run [appname]", + Short: "run the app which can hot compile", + Long: ` +start the appname throw exec.Command + +then start a inotify watch for current dir + +when the file has changed bee will auto go build and restart the app + + file changed + | + check if it's go file + | + yes no + | | + go build do nothing + | + restart app +`, +} + +func init() { + cmdRun.Run = runApp +} + +var appname string +var conf struct { + DirStruct struct { + Controllers string + Models string + } `json:"dir_structure"` +} + +func runApp(cmd *Command, args []string) { + if len(args) != 1 { + fmt.Println("[ERRO] Argument [appname] is missing") + os.Exit(2) + } + crupath, _ := os.Getwd() + Debugf("current path:%s\n", crupath) + + err := loadConfig() + if err != nil { + fmt.Println("[ERRO] Fail to parse bee.json:", err) + } + var paths []string + paths = append(paths, + path.Join(crupath, conf.DirStruct.Controllers), + path.Join(crupath, conf.DirStruct.Models)) + + NewWatcher(paths) + appname = args[0] + Autobuild() + for { + runtime.Gosched() + } +} + +// loadConfig loads customized configuration. +func loadConfig() error { + f, err := os.Open("bee.json") + if err != nil { + // Use default. + return nil + } + defer f.Close() + + d := json.NewDecoder(f) + err = d.Decode(&conf) + if err != nil { + return err + } + + // Set variables. + if len(conf.DirStruct.Controllers) == 0 { + conf.DirStruct.Controllers = "controllers" + } + if len(conf.DirStruct.Models) == 0 { + conf.DirStruct.Models = "models" + } + return nil +} diff --git a/start.go b/start.go deleted file mode 100644 index 4e8acef..0000000 --- a/start.go +++ /dev/null @@ -1,54 +0,0 @@ -package main - -import ( - "fmt" - "os" - path "path/filepath" - "runtime" -) - -var cmdStart = &Command{ - UsageLine: "start [appname]", - Short: "start the app which can hot compile", - Long: ` -start the appname throw exec.Command - -then start a inotify watch for current dir - -when the file has changed bee will auto go build and restart the app - - file changed - | - checked is go file - | - yes no - | | - go build do nothing - | - restart app -`, -} - -func init() { - cmdStart.Run = startapp -} - -var appname string - -func startapp(cmd *Command, args []string) { - if len(args) != 1 { - fmt.Println("error args") - os.Exit(2) - } - crupath, _ := os.Getwd() - Debugf("current path:%s\n", crupath) - - var paths []string - paths = append(paths, path.Join(crupath, "controllers"), path.Join(crupath, "models")) - NewWatcher(paths) - appname = args[0] - Autobuild() - for { - runtime.Gosched() - } -} diff --git a/watch.go b/watch.go index 012fc4f..5fa0c02 100644 --- a/watch.go +++ b/watch.go @@ -45,7 +45,7 @@ func NewWatcher(paths []string) { eventTime[e.Name] = time.Now() if isbuild { - fmt.Println(e) + fmt.Println("[EVEN]", e) go Autobuild() } case err := <-watcher.Error: @@ -67,7 +67,7 @@ func Autobuild() { state.Lock() defer state.Unlock() - fmt.Println("start autobuild") + fmt.Println("[INFO] Start building...") path, _ := os.Getwd() os.Chdir(path) bcmd := exec.Command("go", "build") @@ -76,10 +76,10 @@ func Autobuild() { err := bcmd.Run() if err != nil { - fmt.Println("============== build failed ===================") + fmt.Println("[ERRO] ============== Build failed ===================") return } - fmt.Println("build success") + fmt.Println("[SUCC] Build was successful") Restart(appname) } @@ -101,8 +101,7 @@ func Restart(appname string) { } func Start(appname string) { - fmt.Println("start", appname) - + fmt.Println("[INFO] Restarting", appname) if strings.Index(appname, "./") == -1 { appname = "./" + appname }