-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
88 lines (80 loc) · 1.54 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
package main
import (
"go/format"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
func main() {
path := os.Args[1]
if !Exists(path) {
log.Println("path is not exists")
return
}
if IsDir(path) {
err := filepath.Walk(path,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, ".pb.go") {
log.Println(path)
commentFile(path)
}
return nil
})
if err != nil {
log.Println(err)
}
} else if IsFile(path) {
log.Println(path)
if strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, ".pb.go") {
commentFile(path)
}
}
}
func commentFile(path string) {
textBytes, err := ioutil.ReadFile(path)
if err != nil {
log.Println(err)
return
}
text := string(textBytes)
text = AddCommentToText(text)
ret := wrapLine(text)
fmtRet, err := format.Source([]byte(ret))
if err != nil {
log.Printf("wrap line error:%v", err)
return
}
err = ioutil.WriteFile(path, fmtRet, 0666)
if err != nil {
log.Println(err)
return
}
}
// 判断所给路径文件/文件夹是否存在
func Exists(path string) bool {
_, err := os.Stat(path) // os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
// 判断所给路径是否为文件夹
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
// 判断所给路径是否为文件
func IsFile(path string) bool {
return !IsDir(path)
}