forked from openshift/ops-sop-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
207 lines (181 loc) · 4.99 KB
/
parser.go
File metadata and controls
207 lines (181 loc) · 4.99 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package sopsearch
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
// MDFile is the struct for a markdown file
type MDFile struct {
Path string
Name string
Content string
Author []string
CreationDate time.Time
LastUpdated time.Time
Tags []string
Link string
}
// ADFile is the struct for a asciidoc file
type ADFile struct {
Path string
Name string
Content string
Author []string
CreationDate time.Time
LastUpdated time.Time
Tags []string
Link string
}
// ScanForFiles scans the ops-sop directory and finds files ending in .md or .asciidoc
//and then it will take all of those files and put them inside of a MDFile or ADFile object
//respectively. It also performs a GitLog to find authors and dates for files, and will grab other
//data from the files to put into the object.
func ScanForFiles(path string, config Config) ([]MDFile, []ADFile, error) {
var mfiles []MDFile
var afiles []ADFile
mdmatches, err := FindFiles(path, "*.md")
if err != nil {
return []MDFile{}, []ADFile{}, err
}
admatches, err := FindFiles(path, "*.asciidoc")
if err != nil {
return []MDFile{}, []ADFile{}, err
}
for _, f := range mdmatches {
var linkBuild strings.Builder
var link string
linkBuild.WriteString(config.RepoURL)
path := f
name := strings.TrimSuffix(filepath.Base(f), filepath.Ext(filepath.Base(f)))
content, err := ioutil.ReadFile(f)
if err != nil {
return []MDFile{}, []ADFile{}, err
}
cont := string(content)
git, err := GitLog(f)
if err != nil {
return []MDFile{}, []ADFile{}, err
}
auth, dat, err := GetAuthorsAndDates(git)
if err != nil {
return []MDFile{}, []ADFile{}, err
}
tags := strings.Split(filepath.Dir(f), "/")
if tags[len(tags)-1] == "ops-sop" {
linkBuild.WriteString(filepath.Base(f))
link = linkBuild.String()
} else {
sop := indexOf("ops-sop", tags)
for i := sop + 1; i < len(tags); i++ {
linkBuild.WriteString(tags[i])
}
linkBuild.WriteString(filepath.Base(f))
link = linkBuild.String()
}
tags = append(tags, "markdown")
tags = tags[indexOf("ops-sop", tags):]
tags = append(tags, link)
mfiles = append(mfiles, MDFile{path, name, cont, auth, dat.Oldest, dat.Newest, tags, link})
}
for _, f := range admatches {
var linkBuild strings.Builder
var link string
linkBuild.WriteString(config.RepoURL)
path := f
name := strings.TrimSuffix(filepath.Base(f), filepath.Ext(filepath.Base(f)))
content, err := ioutil.ReadFile(f)
if err != nil {
return []MDFile{}, []ADFile{}, err
}
cont := string(content)
git, err := GitLog(f)
if err != nil {
return []MDFile{}, []ADFile{}, err
}
auth, dat, err := GetAuthorsAndDates(git)
if err != nil {
return []MDFile{}, []ADFile{}, err
}
tags := strings.Split(filepath.Dir(f), "/")
if tags[len(tags)-1] == "ops-sop" {
linkBuild.WriteString(filepath.Base(f))
link = linkBuild.String()
} else {
sop := indexOf("ops-sop", tags)
for i := sop + 1; i < len(tags); i++ {
linkBuild.WriteString(tags[i])
linkBuild.WriteString("/")
}
linkBuild.WriteString(filepath.Base(f))
link = linkBuild.String()
}
tags = append(tags, "asciidoc")
tags = tags[indexOf("ops-sop", tags):]
tags = append(tags, link)
afiles = append(afiles, ADFile{path, name, cont, auth, dat.Oldest, dat.Newest, tags, link})
}
return mfiles, afiles, nil
}
// FindFiles takes a path to a directory and a pattern (in this case, the type of file) and
//looks through the entire directory and adds all the paths for the files that match the pattern
//into a slice of strings which it returns.
func FindFiles(root, pattern string) ([]string, error) {
var matches []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
return err
} else if matched {
matches = append(matches, path)
}
return nil
})
if err != nil {
return nil, err
}
return matches, nil
}
// ToBulkSOP takes a slice of MDFile objects and a slice of ADFile objects and turns both
//into SOP objects and then returns a slice containing all of them as SOP objects.
func ToBulkSOP(mf []MDFile, af []ADFile) ([]Sop, error) {
var sop Sop
var slice []Sop
for _, x := range mf {
sop.Path = x.Path
sop.Name = x.Name
sop.Content = x.Content
sop.Author = x.Author
sop.CreationDate = x.CreationDate
sop.LastUpdated = x.LastUpdated
sop.Tags = x.Tags
sop.Link = x.Link
slice = append(slice, sop)
}
for _, y := range af {
sop.Path = y.Path
sop.Name = y.Name
sop.Content = y.Content
sop.Author = y.Author
sop.CreationDate = y.CreationDate
sop.LastUpdated = y.LastUpdated
sop.Tags = y.Tags
sop.Link = y.Link
slice = append(slice, sop)
}
return slice, nil
}
func indexOf(element string, data []string) int {
for k, v := range data {
if element == v {
return k
}
}
return -1 //not found.
}