mirror of
https://github.com/beego/bee.git
synced 2024-11-21 23:50:54 +00:00
Use package com colorlog for uniform management
This commit is contained in:
parent
acc8c4ddd8
commit
6923aeb1ae
8
bee.json
Normal file
8
bee.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"go_install": false,
|
||||
"dir_structure":{
|
||||
"controllers": "",
|
||||
"models": "",
|
||||
"others": []
|
||||
}
|
||||
}
|
14
new.go
14
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}}
|
||||
|
8
run.go
8
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 {
|
||||
|
30
test.go
30
test.go
@ -1,11 +1,13 @@
|
||||
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{
|
||||
@ -22,7 +24,7 @@ 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,
|
||||
@ -57,7 +59,7 @@ func testApp(cmd *Command, args []string) {
|
||||
}
|
||||
|
||||
func runTest() {
|
||||
colorLog("[INFO] Start testing...\n")
|
||||
com.ColorLog("[INFO] Start testing...\n")
|
||||
time.Sleep(time.Second * 5)
|
||||
path, _ := os.Getwd()
|
||||
os.Chdir(path + "/tests")
|
||||
@ -67,16 +69,16 @@ func runTest(){
|
||||
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")
|
||||
}
|
||||
|
69
util.go
69
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: [<level>] <content [path]> [ 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
|
||||
}
|
||||
}
|
||||
|
29
watch.go
29
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
|
||||
}
|
||||
@ -151,6 +153,7 @@ func Start(appname string) {
|
||||
|
||||
go cmd.Run()
|
||||
started <- true
|
||||
com.ColorLog("[INFO] %s is running...\n", appname)
|
||||
}
|
||||
|
||||
// checkTMPFile returns true if the event was for TMP files.
|
||||
|
Loading…
Reference in New Issue
Block a user