forked from docker-archive/markdownlint
/
main.go
125 lines (111 loc) · 2.94 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/SvenDowideit/markdownlint/checkers"
"github.com/SvenDowideit/markdownlint/data"
"github.com/SvenDowideit/markdownlint/linereader"
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
printUsage()
os.Exit(-1)
}
dir := args[0]
filter := ""
if len(args) >= 2 {
filter = args[1]
}
data.AllFiles = make(map[string]*data.FileDetails)
fmt.Println("Finding files")
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return err
}
data.VerboseLog("FOUND: %s\n", path)
if info.IsDir() {
return nil
}
file, err := filepath.Rel(dir, path)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return err
}
// verboseLog("\t walked to %s\n", file)
data.AddFile(file, path)
return nil
})
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(-1)
}
for file, details := range data.AllFiles {
if !strings.HasPrefix(file, filter) {
data.VerboseLog("FILTERED: %s\n", file)
continue
}
if !strings.HasSuffix(file, ".md") {
data.VerboseLog("SKIPPING: %s\n", file)
continue
}
fmt.Printf("opening: %s\n", file)
reader, err := linereader.OpenReader(details.FullPath)
if err != nil {
fmt.Printf("ERROR opening: %s\n", err)
data.AllFiles[file].FormatErrorCount++
}
err = checkers.CheckHugoFrontmatter(reader, file)
if err != nil {
fmt.Printf("ERROR (%s) frontmatter: %s\n", file, err)
}
err = checkers.CheckMarkdownLinks(reader, file)
if err != nil {
// this only errors if there is a fatal issue
fmt.Printf("ERROR (%s) links: %s\n", file, err)
data.AllFiles[file].FormatErrorCount++
}
reader.Close()
}
checkers.TestLinks()
// TODO (JIRA: DOCS-181): Title, unique across products if not, file should include an {identifier}
summaryFileName := "markdownlint.summary.txt"
f, err := os.Create(summaryFileName)
if err == nil {
fmt.Printf("Also writing summary to %s :\n\n", summaryFileName)
defer f.Close()
}
if filter != "" {
Printf(f, "# Filtered (%s) Summary:\n\n", filter)
} else {
Printf(f, "# Summary:\n\n")
}
errorCount, errorString := checkers.FrontSummary(filter)
Printf(f, errorString)
count, errorString := checkers.LinkSummary(filter)
errorCount += count
Printf(f, errorString)
Printf(f, "\n\tFound: %d files\n", len(data.AllFiles))
Printf(f, "\tFound: %d errors\n", errorCount)
// return the number of 404's to show that there are things to be fixed
os.Exit(errorCount)
}
func Printf(f *os.File, format string, a ...interface{}) {
str := fmt.Sprintf(format, a...)
fmt.Print(str)
if f != nil {
// Don't reall want to know we can't write..
f.WriteString(str)
}
}
func printUsage() {
fmt.Println("Please specify a directory to check")
fmt.Println("\tfor example: markdownlint . [filter]")
fmt.Println("\t [filter] can be any string prefix inside the dir specified")
flag.PrintDefaults()
}