Skip to content

Commit

Permalink
修复监听函数bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hongbao liu authored and hongbao liu committed Nov 24, 2018
1 parent aa061b4 commit 588fb86
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
27 changes: 14 additions & 13 deletions dev.go
@@ -1,19 +1,20 @@
package main

import (
"github.com/urfave/cli"
"bufio"
"fmt"
"os"
"github.com/fsnotify/fsnotify"
"path/filepath"
"log"
"github.com/urfave/cli"
"github.com/xcltapestry/xclpkg/clcolor"
"time"
"os/exec"
"sync"
"bufio"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
)

const title = `
Expand Down Expand Up @@ -41,13 +42,13 @@ func Dev(c *cli.Context) error {
// make dev
// 定义一个函数,用来执行make dev
// 定义一个函数,用来监听当前目录
go watchDir()
makeDev()
a.Wait()
return nil
}

func makeDev() {
go watchDir()
cmd := exec.Command("make", "run")
cmd.Stdout = os.Stdout // 控制台输出命令
cmd.Stderr = os.Stdout // 如果有错误,也使用控制台进行输出
Expand Down Expand Up @@ -124,7 +125,6 @@ func watchDir() {
r := bufio.NewReader(file)
for {
line, err := r.ReadString('\n') //以'\n'为结束符读入一行

if err != nil || io.EOF == err {
break
}
Expand Down Expand Up @@ -199,7 +199,8 @@ func timer() {
// 删除
// 向程序发送退出信号,并重新执行make dev
if command != nil {
command.Process.Kill()
//command.Process.Kill()
syscall.Kill(command.Process.Pid, syscall.SIGTERM)
makeDev()
}
})
Expand All @@ -213,10 +214,10 @@ func timer() {
func removeWatcher(dir string) {
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
//removeWatcher(path)
p, _ := filepath.Abs(path)
p := filepath.Dir(path)
watcher.Remove(p)
} else {
fmt.Println(path)
watcher.Remove(path)
}
return nil
Expand Down
52 changes: 52 additions & 0 deletions middleware.go
@@ -0,0 +1,52 @@
package main

import (
"fmt"
"github.com/urfave/cli"
"io"
"io/ioutil"
"log"
"os"
"strings"
)

func makeMiddleware(c *cli.Context) error {
if c.Args().Get(0) == "" {
log.Fatal("Need a argument: middleware's name. e.g. bingo sword make:middleware example_middleware")
}
tmpDir := getTempDir() + "/middleware.gtpl"
if IfFileExist(tmpDir) {
// 文件存在
f, err := os.Open(tmpDir)
defer f.Close()
if err != nil {
log.Fatal(err)
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
var str string
str = strings.Replace(string(bytes), "${name}", c.Args().Get(0), -1)

// 得到要创建的env文件
p, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
dstDir := p + "/" + new(Environment).GetWithDefault("MIDDLEWARE_DIR", "http/middlewares") + "/" + c.Args().Get(0) + ".go"

df, err := CreateFile(dstDir, 0755)
defer df.Close()
if err != nil {
log.Fatal(err)
}
io.WriteString(df, str)
fmt.Printf("\n%c[0;48;32m%s%c[0m\n", 0x1B, "Middleware created successfully :"+dstDir, 0x1B)


} else {
log.Fatal("No such file named middleware.gtpl in " + tmpDir)
}
return nil
}
5 changes: 5 additions & 0 deletions sword.go
Expand Up @@ -21,5 +21,10 @@ func sword() []cli.Command {
Usage: "Create a command file from the bingo template.",
Action: makeCommand,
},
{
Name: "make:middleware",
Usage: "Create a middleware file from the bingo template.",
Action: makeMiddleware,
},
}
}
10 changes: 10 additions & 0 deletions templates/middleware.gtpl
@@ -0,0 +1,10 @@
package middlewares

import (
"github.com/silsuer/bingo-router"
)


func ${name}(c *bingo_router.Context, next func(c *bingo_router.Context)) {
next(c)
}

0 comments on commit 588fb86

Please sign in to comment.