-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmake_command.go
53 lines (46 loc) · 1.12 KB
/
make_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"github.com/urfave/cli"
"io"
"io/ioutil"
"log"
"os"
"strings"
)
func makeCommand(c *cli.Context) error {
if c.Args().Get(0) == "" {
log.Fatal("Need a argument: command's name. e.g. bingo sword make:command example_command")
}
// tmpl文件路径
tmplDir := getTempDir() + "/command.gtpl"
if IfFileExist(tmplDir) { // 文件存在
f, err := os.Open(tmplDir)
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("COMMAND_DIR", "console/commands") + "/" + 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, "Command created successfully :"+dstDir, 0x1B)
} else {
log.Fatal("No such file named command.gtpl in " + tmplDir)
}
return nil
}