-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirList.go
120 lines (114 loc) · 2.89 KB
/
dirList.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
package main
import (
"flag"
"fmt"
// "github.com/monochromegane/go-gitignore"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
func MkStr(path string, name string) string {
var MDStr string
//path=strings.ReplaceAll(path, "_", "\\_") //Escape links
name = strings.ReplaceAll(name, "_", " ") // strip _ off
name = strings.TrimSuffix(name, ".md")
var Depth = strings.Count(path, "/") // Every / means, we're one subdir higher
for i := 0; i != Depth; i++ {
MDStr += " "
}
MDStr += " * [" + name + "](" + path + ")\n"
return MDStr
}
func MkStrEscapedStr(path string, name string) string {
var MDStr string
path = strings.ReplaceAll(path, "_", "\\_") //Escape links
MDStr = MkStr(path, name)
return MDStr
}
func main() {
var includeDot bool
var OutputFile string
var AppendFile string
var Dir string
//var IgnoreFile string
var DirCounter int
var FileCounter int
var Stats bool
var Escape bool
FileCounter = 0
DirCounter = 0
var MDStr string
flag.BoolVar(&includeDot, "dot", false, "True: Display display dotfiles. Default is false,")
flag.BoolVar(&Stats, "stats", false, "True: Append Stats to output")
flag.BoolVar(&Escape, "escape", true, "False: Dont escape pathes with _")
flag.StringVar(&OutputFile, "o", "", "Output to given file")
flag.StringVar(&AppendFile, "A", "", "Append to given file")
flag.StringVar(&Dir, "d", "", "run in specified directory")
//flag.StringVar(&IgnoreFile, "i", "./.gitignore", "use specified gitignore")
flag.Parse()
if Dir == "" {
Dir = "."
}
/*if IgnoreFile == "" {
IgnoreFile = "."
}
Gitignore, err := gitignore.NewGitIgnore(IgnoreFile)
if err != nil {
fmt.Println("Not using any gitignore")
}*/
err := filepath.Walk(Dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// when dotfiles are excluded
if !includeDot {
if strings.HasPrefix(path, ".") != true{
if info.IsDir() {
DirCounter++
MDStr += MkStrEscapedStr(path, info.Name()+"/")
} else {
MDStr += MkStrEscapedStr(path, info.Name())
FileCounter++
}
}
} else { // in case when dotfiles are not excluded
if info.IsDir() {
DirCounter++
MDStr += MkStrEscapedStr(path, info.Name()+"/")
} else {
MDStr += MkStrEscapedStr(path, info.Name())
FileCounter++
}
}
return nil
})
if err != nil {
log.Println(err)
}
if Stats {
statstr := "\n\nFiles: " + strconv.Itoa(FileCounter) + ", Directories: " + strconv.Itoa(DirCounter) + "\n"
MDStr += statstr
}
if OutputFile != "" {
err := ioutil.WriteFile(OutputFile, []byte(MDStr), 0644)
if err != nil {
log.Fatal(err)
}
} else if AppendFile != "" {
f, err := os.OpenFile(AppendFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = f.WriteString(MDStr)
if err != nil {
log.Fatal(err)
}
} else {
fmt.Println(MDStr)
}
}