-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
117 lines (87 loc) · 1.71 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
const modu = "/go.mod"
type Rename struct {
name string
dirFiles []string
modName, newName, dirRoot string
}
func (r *Rename) filePath() {
err := filepath.Walk(r.dirRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dirs := strings.Split(path, "/n")
for i := 0; i < len(dirs); i++ {
if strings.Contains(dirs[i], ".go") {
r.dirFiles = append(r.dirFiles, dirs[i])
}
}
return nil
})
if err != nil {
log.Println(err)
return
}
r.refacFile()
}
func (r Rename) refacFile() {
for i := 0; i < len(r.dirFiles); i++ {
file, err := os.ReadFile(r.dirFiles[i])
if err != nil {
return
}
err = r.doRefac(file, r.dirFiles[i])
if err != nil {
log.Println(err)
}
}
fmt.Println("Succesfuly")
}
func (r Rename) doRefac(file []byte, dir string) error {
newconten := strings.ReplaceAll(string(file), r.modName, r.newName)
err := os.WriteFile(dir, []byte(newconten), os.FileMode(0755))
if err != nil {
return err
}
return nil
}
func (r *Rename) renameMod() {
dirModu := r.dirRoot + modu
file, err := os.ReadFile(dirModu)
if err != nil {
log.Println(err)
return
}
lines := strings.Split(string(file), "\n")
r.modName = (lines[0])[len("module")+1:]
err = r.doRefac(file, dirModu)
if err != nil {
log.Println(err)
return
}
r.filePath()
}
func main() {
runApp()
}
func runApp() {
var (
err error
rename = &Rename{}
)
flag.StringVar(&rename.newName, "rename", " ", "contain new name for gomod")
flag.Parse()
rename.dirRoot, err = os.Getwd()
if err != nil {
log.Fatal(err)
}
rename.renameMod()
}