/
main.go
74 lines (58 loc) · 1.51 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
package main
import (
"flag"
"log"
"os"
"path/filepath"
"github.com/CheHsuan/markdown-tree/pkg/mdtree"
)
var (
inputDir = flag.String("input-dir", "", "input directory path")
outputFile = flag.String("output-file", "", "output file path")
withLink = flag.Bool("with-link", false, "enable adding relative file path as link")
baseURL = flag.String("base-url", "", "set the base URL for --with-link option")
)
func main() {
flag.Parse()
if *inputDir == "" || *outputFile == "" {
log.Panic("missing input directory path or output file path")
}
opts := []func(*mdtree.Config){}
if *withLink {
if *baseURL == "" {
log.Panic("missing baseURL for with-link option")
}
opts = append(opts, mdtree.WithLink(*baseURL))
}
tree := mdtree.NewMarkdownTree(filepath.Base(*inputDir), opts...)
wd, err := os.Getwd()
if err != nil {
log.Panic(err.Error())
}
// change working dir to target dir
if err := os.Chdir(*inputDir); err != nil {
log.Panic(err.Error())
}
// walk through the directory
if err := filepath.Walk(".",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && info.Name() == ".git" {
return filepath.SkipDir
}
tree.InsertNode(path, info.IsDir())
return nil
}); err != nil {
log.Panic(err.Error())
}
// change working dir back
if err := os.Chdir(wd); err != nil {
log.Panic(err.Error())
}
if err := tree.GenerateMarkdown(*outputFile); err != nil {
log.Panic(err.Error())
}
log.Printf("done")
}