-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathmain.go
More file actions
91 lines (82 loc) · 2.41 KB
/
main.go
File metadata and controls
91 lines (82 loc) · 2.41 KB
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/url"
"path"
"sort"
"strings"
)
func main() {
var buf bytes.Buffer
buf.WriteString(strings.TrimLeft(authInfo, "\n"))
buf.WriteString("\n# 目录\n")
readDir(".", root, &buf)
buf.WriteString(footer)
err := ioutil.WriteFile("README.md", buf.Bytes(), 0644)
checkErr(err)
}
func readDir(dirname string, level int, buf *bytes.Buffer) {
fileList, err := ioutil.ReadDir(dirname)
checkErr(err)
sort.Slice(fileList, func(i, j int) bool {
if fileList[i].IsDir() == fileList[j].IsDir() {
return strings.ToLower(fileList[i].Name()) < strings.ToLower(fileList[j].Name())
}
return fileList[j].IsDir()
})
for _, fi := range fileList {
if validName(fi.Name()) {
if level == root {
buf.WriteString("\n")
if fi.IsDir() {
buf.WriteString("## ")
}
} else {
buf.WriteString(strings.Repeat(" ", level))
buf.WriteString("- ")
}
buf.WriteString(fmt.Sprintf("[%s](%s)\n",
fi.Name(),
url.PathEscape(path.Join(dirname, fi.Name())),
))
if fi.IsDir() {
readDir(path.Join(dirname, fi.Name()), level+1, buf)
}
}
}
}
func validName(name string) bool {
name = strings.ToLower(name)
excludeFile := map[string]bool{
"license": true,
"go.mod": true,
"go.sum": true,
}
return !(excludeFile[name] ||
strings.HasPrefix(name, ".") ||
strings.HasSuffix(name, ".md") ||
strings.HasSuffix(name, ".go"))
}
func checkErr(err error) {
if err != nil {
log.Fatalln(err)
}
}
const root = 0
const authInfo = `
<!--|This file generated by command; DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/books |-->
<!--+----------------------------------------------------------------------+-->
`
const footer = `
## ©2018 Shuo. All rights reserved.
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fopenset%2Fbooks?ref=badge_shield)
## License
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fopenset%2Fbooks?ref=badge_large)
`