-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
199 lines (182 loc) · 3.88 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/fs"
"os"
"os/exec"
"runtime"
"strings"
)
type Config struct {
Style string `json:"style"`
Filter []string `json:"filter"`
Dirs []string `json:"dirs"`
}
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
const configExample = `{
"style": "WebKit",
"filter": [
"*.c",
"*.cc",
"*.cpp",
"*.h",
"*.hh",
"*.hpp"
],
"dirs": [
"./"
]
}`
func main() {
var config Config
var configPath string
flag.StringVar(&configPath, "c", "", "format config path. if the param is empty, would create a example config.")
flag.Parse()
exist, err := PathExists("./format.json")
if err != nil {
panic(err)
}
if exist {
configPath = "format.json"
}
if configPath == "" {
fmt.Println("param config is empty, create a example config...")
f, err := os.Create("./format.json")
if err != nil {
fmt.Println("create example config fail!", err)
return
}
defer f.Close()
writer := bufio.NewWriter(f)
defer writer.Flush()
writer.WriteString(configExample)
fmt.Println("example config is created!")
return
} else {
f, err := os.Open(configPath)
if err != nil {
fmt.Println("open config file fail!", err)
return
}
defer f.Close()
reader := bufio.NewReader(f)
decoder := json.NewDecoder(reader)
err = decoder.Decode(&config)
if err != nil {
fmt.Println("decode json fail!", err)
return
}
}
if len(config.Filter) == 0 {
config.Filter = append(config.Filter, "*")
} else if len(config.Filter) == 1 && config.Filter[0] == "" {
config.Filter[0] = "*"
}
dirs := GetDirsList(config.Dirs)
dirs = filterConfigDirs(dirs, config.Filter)
files := make([]string, 0)
for _, dir := range dirs {
for _, filter := range config.Filter {
path := fmt.Sprintf("%s/%s", dir, filter)
files = append(files, path)
fmt.Println("format file: ", path)
}
}
err = ClangFormat(config.Style, files)
if err != nil {
fmt.Println(err)
}
}
func filterConfigDirs(dirs []string, s []string) []string {
res := []string{}
for _, d := range dirs {
var count = 0
for _, f := range s {
var matches, err = (fs.Glob(os.DirFS("./"+d), "./"+f))
if err != nil {
panic(err)
}
count += len(matches)
}
if count > 0 {
res = append(res, d)
}
}
return res
}
func GetDirList(path string) []string {
dirs := make([]string, 0)
rd, err := os.ReadDir(path)
if err != nil {
fmt.Println("read dir error!", err)
return dirs
}
for _, fi := range rd {
if fi.IsDir() {
subDirs := GetDirList(path + "/" + fi.Name())
dirs = append(dirs, subDirs...)
}
}
dirs = append(dirs, path)
return dirs
}
func GetDirsList(paths []string) []string {
dirs := make([]string, 0)
for _, path := range paths {
dirs = append(dirs, GetDirList(path)...)
}
return dirs
}
func ClangFormat(style string, paths []string) error {
files := strings.Join(paths, " ")
command := fmt.Sprintf("clang-format -i --style=%s %s", style, files) + " --verbose"
fmt.Println("command:", command)
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
fmt.Println("runtime os is windows, use powershell")
cmd = exec.Command("powershell", "-command", command)
//cmd = exec.Command("cmd.exe", "/c", "start"+command)
} else {
fmt.Println("runtime os is linux, use bash")
cmd = exec.Command("/bin/bash", "-c", command)
}
cmd.Stderr = os.Stderr
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("create stdout pipe fail!")
return err
}
err = cmd.Start()
if err != nil {
return err
}
for {
output, _, err := bufio.NewReader(stdout).ReadLine()
if err != nil {
if err.Error() != "EOF" {
fmt.Println("read output fail!")
return err
} else {
break
}
}
fmt.Println(output)
}
err = cmd.Wait()
if err != nil {
return err
}
return nil
}