-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (74 loc) · 1.82 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// starter 项目支持工具, 修改包名和引用名
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
var clone = "https://github.com/medivh-jay/starter.git"
func main() {
var module string
var help bool
flag.StringVar(&module, "m", "", "type your custom module name")
flag.BoolVar(&help, "h", false, "get help")
flag.Parse()
if help || module == "" {
flag.Usage()
return
}
fmt.Println(module)
cmd := exec.Command("git", "clone", clone, module)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run()
path, _ := filepath.Abs(fmt.Sprintf("%s/%s/", "./", module))
var confirm string
fmt.Printf("the directory will be traversed: %s, and all files ending in .go will be modified, Confirm?[Y/N]: ", path)
_, _ = fmt.Scanln(&confirm)
if strings.ToUpper(confirm) != "Y" {
return
}
git, _ := filepath.Abs(path + "/.git")
_ = os.RemoveAll(git)
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".go") {
buf, err := ioutil.ReadFile(path)
if err != nil {
return err
}
buf = bytes.ReplaceAll(buf, []byte(`"starter/`), []byte(fmt.Sprintf(`"%s/`, module)))
err = ioutil.WriteFile(path, buf, 0755)
if err != nil {
return err
}
_ = exec.Command("go", "fmt", path).Run()
fmt.Println("gocreator: ", "complete->", path)
}
if info.Name() == "go.mod" {
buf, err := ioutil.ReadFile(path)
if err != nil {
return err
}
content := string(buf)
content = strings.ReplaceAll(content, `starter`, fmt.Sprintf(`%s`, module))
err = ioutil.WriteFile(path, []byte(content), 0755)
if err != nil {
return err
}
}
return nil
})
if err != nil {
log.Println(err)
}
}