mirror of
https://github.com/beego/bee.git
synced 2024-11-22 05:00: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"
|
"os"
|
||||||
path "path/filepath"
|
path "path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Unknwon/com"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmdNew = &Command{
|
var cmdNew = &Command{
|
||||||
@ -40,15 +42,15 @@ func init() {
|
|||||||
func createApp(cmd *Command, args []string) {
|
func createApp(cmd *Command, args []string) {
|
||||||
curpath, _ := os.Getwd()
|
curpath, _ := os.Getwd()
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
colorLog("[ERRO] Argument [appname] is missing\n")
|
com.ColorLog("[ERRO] Argument [appname] is missing\n")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
gopath := os.Getenv("GOPATH")
|
gopath := os.Getenv("GOPATH")
|
||||||
Debugf("gopath:%s", gopath)
|
Debugf("gopath:%s", gopath)
|
||||||
if gopath == "" {
|
if gopath == "" {
|
||||||
colorLog("[ERRO] $GOPATH not found\n")
|
com.ColorLog("[ERRO] $GOPATH not found\n")
|
||||||
colorLog("[HINT] Set $GOPATH in your environment vairables\n")
|
com.ColorLog("[HINT] Set $GOPATH in your environment vairables\n")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
haspath := false
|
haspath := false
|
||||||
@ -66,8 +68,8 @@ func createApp(cmd *Command, args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !haspath {
|
if !haspath {
|
||||||
colorLog("[ERRO] Unable to create an application outside of $GOPATH(%s)\n", gopath)
|
com.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("[HINT] Change your work directory by `cd ($GOPATH%ssrc)`\n", string(path.Separator))
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +112,7 @@ func createApp(cmd *Command, args []string) {
|
|||||||
fmt.Println(path.Join(apppath, "main.go"))
|
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))
|
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}}
|
var appconf = `appname = {{.Appname}}
|
||||||
|
8
run.go
8
run.go
@ -5,6 +5,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
path "path/filepath"
|
path "path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/Unknwon/com"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmdRun = &Command{
|
var cmdRun = &Command{
|
||||||
@ -68,7 +70,7 @@ var conf struct {
|
|||||||
func runApp(cmd *Command, args []string) {
|
func runApp(cmd *Command, args []string) {
|
||||||
exit := make(chan bool)
|
exit := make(chan bool)
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
colorLog("[ERRO] Cannot start running[ %s ]\n",
|
com.ColorLog("[ERRO] Cannot start running[ %s ]\n",
|
||||||
"argument 'appname' is missing")
|
"argument 'appname' is missing")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
@ -77,7 +79,7 @@ func runApp(cmd *Command, args []string) {
|
|||||||
|
|
||||||
err := loadConfig()
|
err := loadConfig()
|
||||||
if err != nil {
|
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
|
var paths []string
|
||||||
paths = append(paths,
|
paths = append(paths,
|
||||||
@ -111,7 +113,7 @@ func loadConfig() error {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
colorLog("[INFO] Detected bee.json\n")
|
com.ColorLog("[INFO] Detected bee.json\n")
|
||||||
d := json.NewDecoder(f)
|
d := json.NewDecoder(f)
|
||||||
err = d.Decode(&conf)
|
err = d.Decode(&conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
42
test.go
42
test.go
@ -1,28 +1,30 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
path "path/filepath"
|
|
||||||
"os/exec"
|
|
||||||
"time"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
path "path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Unknwon/com"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmdTest = &Command{
|
var cmdTest = &Command{
|
||||||
UsageLine: "test [appname]",
|
UsageLine: "test [appname]",
|
||||||
Short: "test the app",
|
Short: "test the app",
|
||||||
Long: ``,
|
Long: ``,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdTest.Run = testApp
|
cmdTest.Run = testApp
|
||||||
}
|
}
|
||||||
|
|
||||||
var started= make(chan bool)
|
var started = make(chan bool)
|
||||||
|
|
||||||
func testApp(cmd *Command, args []string) {
|
func testApp(cmd *Command, args []string) {
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
colorLog("[ERRO] Cannot start running[ %s ]\n",
|
com.ColorLog("[ERRO] Cannot start running[ %s ]\n",
|
||||||
"argument 'appname' is missing")
|
"argument 'appname' is missing")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
@ -31,7 +33,7 @@ func testApp(cmd *Command, args []string) {
|
|||||||
|
|
||||||
err := loadConfig()
|
err := loadConfig()
|
||||||
if err != nil {
|
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
|
var paths []string
|
||||||
paths = append(paths,
|
paths = append(paths,
|
||||||
@ -56,27 +58,27 @@ func testApp(cmd *Command, args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runTest(){
|
func runTest() {
|
||||||
colorLog("[INFO] Start testing...\n")
|
com.ColorLog("[INFO] Start testing...\n")
|
||||||
time.Sleep(time.Second*5)
|
time.Sleep(time.Second * 5)
|
||||||
path, _ := os.Getwd()
|
path, _ := os.Getwd()
|
||||||
os.Chdir(path+"/tests")
|
os.Chdir(path + "/tests")
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
icmd := exec.Command("go", "test")
|
icmd := exec.Command("go", "test")
|
||||||
var out,errbuffer bytes.Buffer
|
var out, errbuffer bytes.Buffer
|
||||||
icmd.Stdout = &out
|
icmd.Stdout = &out
|
||||||
icmd.Stderr = &errbuffer
|
icmd.Stderr = &errbuffer
|
||||||
colorLog("[INFO] ============== Test Begin ===================\n")
|
com.ColorLog("[INFO] ============== Test Begin ===================\n")
|
||||||
err = icmd.Run()
|
err = icmd.Run()
|
||||||
colorLog(out.String())
|
com.ColorLog(out.String())
|
||||||
colorLog(errbuffer.String())
|
com.ColorLog(errbuffer.String())
|
||||||
colorLog("[INFO] ============== Test End ===================\n")
|
com.ColorLog("[INFO] ============== Test End ===================\n")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
colorLog("[ERRO] ============== Test failed ===================\n")
|
com.ColorLog("[ERRO] ============== Test failed ===================\n")
|
||||||
colorLog("[ERRO] " ,err)
|
com.ColorLog("[ERRO] ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
colorLog("[SUCC] Test finish\n")
|
com.ColorLog("[SUCC] Test finish\n")
|
||||||
}
|
}
|
||||||
|
69
util.go
69
util.go
@ -5,7 +5,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Go is a basic promise implementation: it wraps calls a function in a goroutine
|
// 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...)
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
31
watch.go
31
watch.go
@ -2,13 +2,15 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/howeyc/fsnotify"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Unknwon/com"
|
||||||
|
"github.com/howeyc/fsnotify"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -20,7 +22,7 @@ var (
|
|||||||
func NewWatcher(paths []string) {
|
func NewWatcher(paths []string) {
|
||||||
watcher, err := fsnotify.NewWatcher()
|
watcher, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
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)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,14 +42,14 @@ func NewWatcher(paths []string) {
|
|||||||
|
|
||||||
mt := getFileModTime(e.Name)
|
mt := getFileModTime(e.Name)
|
||||||
if t := eventTime[e.Name]; mt == t {
|
if t := eventTime[e.Name]; mt == t {
|
||||||
colorLog("[SKIP] # %s #\n", e.String())
|
com.ColorLog("[SKIP] # %s #\n", e.String())
|
||||||
isbuild = false
|
isbuild = false
|
||||||
}
|
}
|
||||||
|
|
||||||
eventTime[e.Name] = mt
|
eventTime[e.Name] = mt
|
||||||
|
|
||||||
if isbuild {
|
if isbuild {
|
||||||
colorLog("[EVEN] %s\n", e)
|
com.ColorLog("[EVEN] %s\n", e)
|
||||||
go Autobuild()
|
go Autobuild()
|
||||||
}
|
}
|
||||||
case err := <-watcher.Error:
|
case err := <-watcher.Error:
|
||||||
@ -58,12 +60,12 @@ func NewWatcher(paths []string) {
|
|||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
colorLog("[INFO] Initializing watcher...\n")
|
com.ColorLog("[INFO] Initializing watcher...\n")
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
colorLog("[TRAC] Directory( %s )\n", path)
|
com.ColorLog("[TRAC] Directory( %s )\n", path)
|
||||||
err = watcher.Watch(path)
|
err = watcher.Watch(path)
|
||||||
if err != nil {
|
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)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,14 +77,14 @@ func getFileModTime(path string) int64 {
|
|||||||
path = strings.Replace(path, "\\", "/", -1)
|
path = strings.Replace(path, "\\", "/", -1)
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
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()
|
return time.Now().Unix()
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
fi, err := f.Stat()
|
fi, err := f.Stat()
|
||||||
if err != nil {
|
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()
|
return time.Now().Unix()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,7 +95,7 @@ func Autobuild() {
|
|||||||
state.Lock()
|
state.Lock()
|
||||||
defer state.Unlock()
|
defer state.Unlock()
|
||||||
|
|
||||||
colorLog("[INFO] Start building...\n")
|
com.ColorLog("[INFO] Start building...\n")
|
||||||
path, _ := os.Getwd()
|
path, _ := os.Getwd()
|
||||||
os.Chdir(path)
|
os.Chdir(path)
|
||||||
|
|
||||||
@ -115,10 +117,10 @@ func Autobuild() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
colorLog("[ERRO] ============== Build failed ===================\n")
|
com.ColorLog("[ERRO] ============== Build failed ===================\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
colorLog("[SUCC] Build was successful\n")
|
com.ColorLog("[SUCC] Build was successful\n")
|
||||||
Restart(appname)
|
Restart(appname)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +142,7 @@ func Restart(appname string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Start(appname string) {
|
func Start(appname string) {
|
||||||
colorLog("[INFO] Restarting %s ...\n", appname)
|
com.ColorLog("[INFO] Restarting %s ...\n", appname)
|
||||||
if strings.Index(appname, "./") == -1 {
|
if strings.Index(appname, "./") == -1 {
|
||||||
appname = "./" + appname
|
appname = "./" + appname
|
||||||
}
|
}
|
||||||
@ -150,7 +152,8 @@ func Start(appname string) {
|
|||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
go cmd.Run()
|
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.
|
// checkTMPFile returns true if the event was for TMP files.
|
||||||
|
Loading…
Reference in New Issue
Block a user