From 6923aeb1aefa7c0586def89e492c8418daa00a9d Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 24 Aug 2013 16:41:46 +0800 Subject: [PATCH] Use package com colorlog for uniform management --- bee.json | 8 +++++++ new.go | 14 +++++++----- run.go | 8 ++++--- test.go | 42 ++++++++++++++++++---------------- util.go | 69 -------------------------------------------------------- watch.go | 31 +++++++++++++------------ 6 files changed, 60 insertions(+), 112 deletions(-) create mode 100644 bee.json diff --git a/bee.json b/bee.json new file mode 100644 index 0000000..725d11c --- /dev/null +++ b/bee.json @@ -0,0 +1,8 @@ +{ + "go_install": false, + "dir_structure":{ + "controllers": "", + "models": "", + "others": [] + } +} \ No newline at end of file diff --git a/new.go b/new.go index b4ed4ca..cb5f96e 100644 --- a/new.go +++ b/new.go @@ -5,6 +5,8 @@ import ( "os" path "path/filepath" "strings" + + "github.com/Unknwon/com" ) var cmdNew = &Command{ @@ -40,15 +42,15 @@ func init() { func createApp(cmd *Command, args []string) { curpath, _ := os.Getwd() if len(args) != 1 { - colorLog("[ERRO] Argument [appname] is missing\n") + com.ColorLog("[ERRO] Argument [appname] is missing\n") os.Exit(2) } gopath := os.Getenv("GOPATH") Debugf("gopath:%s", gopath) if gopath == "" { - colorLog("[ERRO] $GOPATH not found\n") - colorLog("[HINT] Set $GOPATH in your environment vairables\n") + com.ColorLog("[ERRO] $GOPATH not found\n") + com.ColorLog("[HINT] Set $GOPATH in your environment vairables\n") os.Exit(2) } haspath := false @@ -66,8 +68,8 @@ func createApp(cmd *Command, args []string) { } if !haspath { - colorLog("[ERRO] Unable to create an application outside of $GOPATH(%s)\n", gopath) - colorLog("[HINT] Change your work directory by `cd ($GOPATH%ssrc)`\n", string(path.Separator)) + com.ColorLog("[ERRO] Unable to create an application outside of $GOPATH(%s)\n", gopath) + com.ColorLog("[HINT] Change your work directory by `cd ($GOPATH%ssrc)`\n", string(path.Separator)) os.Exit(2) } @@ -110,7 +112,7 @@ func createApp(cmd *Command, args []string) { fmt.Println(path.Join(apppath, "main.go")) writetofile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), string(path.Separator)), -1)) - colorLog("[SUCC] New application successfully created!\n") + com.ColorLog("[SUCC] New application successfully created!\n") } var appconf = `appname = {{.Appname}} diff --git a/run.go b/run.go index 4fb3c97..ad0297d 100644 --- a/run.go +++ b/run.go @@ -5,6 +5,8 @@ import ( "os" path "path/filepath" "runtime" + + "github.com/Unknwon/com" ) var cmdRun = &Command{ @@ -68,7 +70,7 @@ var conf struct { func runApp(cmd *Command, args []string) { exit := make(chan bool) if len(args) != 1 { - colorLog("[ERRO] Cannot start running[ %s ]\n", + com.ColorLog("[ERRO] Cannot start running[ %s ]\n", "argument 'appname' is missing") os.Exit(2) } @@ -77,7 +79,7 @@ func runApp(cmd *Command, args []string) { err := loadConfig() if err != nil { - colorLog("[ERRO] Fail to parse bee.json[ %s ]", err) + com.ColorLog("[ERRO] Fail to parse bee.json[ %s ]", err) } var paths []string paths = append(paths, @@ -111,7 +113,7 @@ func loadConfig() error { } } else { defer f.Close() - colorLog("[INFO] Detected bee.json\n") + com.ColorLog("[INFO] Detected bee.json\n") d := json.NewDecoder(f) err = d.Decode(&conf) if err != nil { diff --git a/test.go b/test.go index 56446b4..0bb9468 100644 --- a/test.go +++ b/test.go @@ -1,28 +1,30 @@ package main import ( - "os" - path "path/filepath" - "os/exec" - "time" "bytes" + "os" + "os/exec" + path "path/filepath" + "time" + + "github.com/Unknwon/com" ) var cmdTest = &Command{ UsageLine: "test [appname]", Short: "test the app", - Long: ``, + Long: ``, } func init() { cmdTest.Run = testApp } -var started= make(chan bool) +var started = make(chan bool) func testApp(cmd *Command, args []string) { if len(args) != 1 { - colorLog("[ERRO] Cannot start running[ %s ]\n", + com.ColorLog("[ERRO] Cannot start running[ %s ]\n", "argument 'appname' is missing") os.Exit(2) } @@ -31,7 +33,7 @@ func testApp(cmd *Command, args []string) { err := loadConfig() if err != nil { - colorLog("[ERRO] Fail to parse bee.json[ %s ]", err) + com.ColorLog("[ERRO] Fail to parse bee.json[ %s ]", err) } var paths []string paths = append(paths, @@ -56,27 +58,27 @@ func testApp(cmd *Command, args []string) { } } -func runTest(){ - colorLog("[INFO] Start testing...\n") - time.Sleep(time.Second*5) +func runTest() { + com.ColorLog("[INFO] Start testing...\n") + time.Sleep(time.Second * 5) path, _ := os.Getwd() - os.Chdir(path+"/tests") + os.Chdir(path + "/tests") var err error icmd := exec.Command("go", "test") - var out,errbuffer bytes.Buffer + var out, errbuffer bytes.Buffer icmd.Stdout = &out icmd.Stderr = &errbuffer - colorLog("[INFO] ============== Test Begin ===================\n") + com.ColorLog("[INFO] ============== Test Begin ===================\n") err = icmd.Run() - colorLog(out.String()) - colorLog(errbuffer.String()) - colorLog("[INFO] ============== Test End ===================\n") + com.ColorLog(out.String()) + com.ColorLog(errbuffer.String()) + com.ColorLog("[INFO] ============== Test End ===================\n") if err != nil { - colorLog("[ERRO] ============== Test failed ===================\n") - colorLog("[ERRO] " ,err) + com.ColorLog("[ERRO] ============== Test failed ===================\n") + com.ColorLog("[ERRO] ", err) return } - colorLog("[SUCC] Test finish\n") + com.ColorLog("[SUCC] Test finish\n") } diff --git a/util.go b/util.go index 6cafb46..2649ef0 100644 --- a/util.go +++ b/util.go @@ -5,7 +5,6 @@ import ( "os" "path/filepath" "runtime" - "strings" ) // Go is a basic promise implementation: it wraps calls a function in a goroutine @@ -31,71 +30,3 @@ func Debugf(format string, a ...interface{}) { fmt.Fprintf(os.Stderr, fmt.Sprintf("[debug] %s:%d %s\n", file, line, format), a...) } } - -const ( - Gray = uint8(iota + 90) - Red - Green - Yellow - Blue - Magenta - //NRed = uint8(31) // Normal - EndColor = "\033[0m" -) - -// colorLog colors log and print to stdout. -// Log format: [] [ error ]. -// Level: ERRO -> red; WARN -> Magenta; SUCC -> green; others -> default. -// Content: default; path: yellow; error -> red. -// Errors have to surrounded by "[ " and " ]"(space). -func colorLog(format string, a ...interface{}) { - log := fmt.Sprintf(format, a...) - if len(log) == 0 { - return - } - - if runtime.GOOS != "windows" { - var clog string - - // Level. - i := strings.Index(log, "]") - if log[0] == '[' && i > -1 { - clog += "[" + getColorLevel(log[1:i]) + "]" - } - - log = log[i+1:] - - // Error. - log = strings.Replace(log, "[ ", fmt.Sprintf("[\033[%dm", Red), -1) - log = strings.Replace(log, " ]", EndColor+"]", -1) - - // Path. - log = strings.Replace(log, "( ", fmt.Sprintf("(\033[%dm", Yellow), -1) - log = strings.Replace(log, " )", EndColor+")", -1) - - // Highlights. - log = strings.Replace(log, "# ", fmt.Sprintf("\033[%dm", Gray), -1) - log = strings.Replace(log, " #", EndColor, -1) - - log = clog + log - } - - fmt.Print(log) -} - -// getColorLevel returns colored level string by given level. -func getColorLevel(level string) string { - level = strings.ToUpper(level) - switch level { - case "TRAC": - return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level) - case "ERRO": - return fmt.Sprintf("\033[%dm%s\033[0m", Red, level) - case "WARN": - return fmt.Sprintf("\033[%dm%s\033[0m", Magenta, level) - case "SUCC": - return fmt.Sprintf("\033[%dm%s\033[0m", Green, level) - default: - return level - } -} diff --git a/watch.go b/watch.go index 8e77305..5b4d71f 100644 --- a/watch.go +++ b/watch.go @@ -2,13 +2,15 @@ package main import ( "fmt" - "github.com/howeyc/fsnotify" "log" "os" "os/exec" "strings" "sync" "time" + + "github.com/Unknwon/com" + "github.com/howeyc/fsnotify" ) var ( @@ -20,7 +22,7 @@ var ( func NewWatcher(paths []string) { watcher, err := fsnotify.NewWatcher() if err != nil { - colorLog("[ERRO] Fail to create new Watcher[ %s ]\n", err) + com.ColorLog("[ERRO] Fail to create new Watcher[ %s ]\n", err) os.Exit(2) } @@ -40,14 +42,14 @@ func NewWatcher(paths []string) { mt := getFileModTime(e.Name) if t := eventTime[e.Name]; mt == t { - colorLog("[SKIP] # %s #\n", e.String()) + com.ColorLog("[SKIP] # %s #\n", e.String()) isbuild = false } eventTime[e.Name] = mt if isbuild { - colorLog("[EVEN] %s\n", e) + com.ColorLog("[EVEN] %s\n", e) go Autobuild() } case err := <-watcher.Error: @@ -58,12 +60,12 @@ func NewWatcher(paths []string) { time.Sleep(500 * time.Millisecond) }() - colorLog("[INFO] Initializing watcher...\n") + com.ColorLog("[INFO] Initializing watcher...\n") for _, path := range paths { - colorLog("[TRAC] Directory( %s )\n", path) + com.ColorLog("[TRAC] Directory( %s )\n", path) err = watcher.Watch(path) if err != nil { - colorLog("[ERRO] Fail to watch directory[ %s ]\n", err) + com.ColorLog("[ERRO] Fail to watch directory[ %s ]\n", err) os.Exit(2) } } @@ -75,14 +77,14 @@ func getFileModTime(path string) int64 { path = strings.Replace(path, "\\", "/", -1) f, err := os.Open(path) if err != nil { - colorLog("[ERRO] Fail to open file[ %s ]\n", err) + com.ColorLog("[ERRO] Fail to open file[ %s ]\n", err) return time.Now().Unix() } defer f.Close() fi, err := f.Stat() if err != nil { - colorLog("[ERRO] Fail to get file information[ %s ]\n", err) + com.ColorLog("[ERRO] Fail to get file information[ %s ]\n", err) return time.Now().Unix() } @@ -93,7 +95,7 @@ func Autobuild() { state.Lock() defer state.Unlock() - colorLog("[INFO] Start building...\n") + com.ColorLog("[INFO] Start building...\n") path, _ := os.Getwd() os.Chdir(path) @@ -115,10 +117,10 @@ func Autobuild() { } if err != nil { - colorLog("[ERRO] ============== Build failed ===================\n") + com.ColorLog("[ERRO] ============== Build failed ===================\n") return } - colorLog("[SUCC] Build was successful\n") + com.ColorLog("[SUCC] Build was successful\n") Restart(appname) } @@ -140,7 +142,7 @@ func Restart(appname string) { } func Start(appname string) { - colorLog("[INFO] Restarting %s ...\n", appname) + com.ColorLog("[INFO] Restarting %s ...\n", appname) if strings.Index(appname, "./") == -1 { appname = "./" + appname } @@ -150,7 +152,8 @@ func Start(appname string) { cmd.Stderr = os.Stderr go cmd.Run() - started<-true + started <- true + com.ColorLog("[INFO] %s is running...\n", appname) } // checkTMPFile returns true if the event was for TMP files.