This repository has been archived by the owner on Nov 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc.go
135 lines (115 loc) · 2.99 KB
/
doc.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
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"net/url"
"path"
"strings"
"sync"
"github.com/blevesearch/bleve"
"github.com/xanzy/go-gitlab"
)
// Flavor is the markdown flavor used for parsing the markdown files.
type Flavor int
const (
None Flavor = iota
Mmark
)
type Doc struct {
projects map[string]*GitLab // Basename(URL) -> Project + potential metadata
i bleve.Index
rw sync.RWMutex // protects Projects and Index
}
// New returns a new and initialized pointer to a Doc. Note the Bleve index is not set.
func New() *Doc {
return &Doc{projects: make(map[string]*GitLab), rw: sync.RWMutex{}}
}
type GitLab struct {
*gitlab.Project
Options
Files map[string][]byte
}
func (g *GitLab) String() string {
// strings.Builder ?
s := fmt.Sprintf("** %s: %d files\n", g.WebURL, len(g.Files))
for k, v := range g.Files {
s += "\t" + k + fmt.Sprintf(", %d bytes\n", len(v))
}
return s
}
func (d *Doc) String() string {
d.rw.RLock()
defer d.rw.RUnlock()
s := fmt.Sprintf("%d Projects\n", len(d.projects))
for _, v := range d.projects {
s += v.String()
}
return s
}
// Insert inserts a new project into d.
func (d *Doc) Insert(p *gitlab.Project, opts Options) {
d.rw.Lock()
defer d.rw.Unlock()
urlp := ProjectToPath(p)
d.projects[urlp] = &GitLab{Project: p, Options: opts, Files: make(map[string][]byte)}
}
// Fetch will return the project belonging to path. Will return nil if not found.
func (d *Doc) Fetch(path string) *GitLab {
d.rw.RLock()
defer d.rw.RUnlock()
return d.projects[path]
}
func (d *Doc) InsertFile(p *gitlab.Project, pathname string, buf []byte) {
urlp := ProjectToPath(p)
gl := d.Fetch(urlp)
if gl == nil {
return
}
d.rw.Lock()
defer d.rw.Unlock()
stripped := RemoveFirstPathElement(pathname)
full := path.Join(urlp, stripped)
gl.Files[full] = buf
}
// FetchFile return the file for p associated with pathname. Pathname must be without the doc dir.
func (d *Doc) FetchFile(p *gitlab.Project, pathname string) []byte {
urlp := ProjectToPath(p)
gl := d.Fetch(urlp)
if gl == nil {
return nil
}
d.rw.RLock()
defer d.rw.RUnlock()
full := path.Join(urlp, pathname)
return gl.Files[full]
}
// SetIndex sets the Bleve index in doc.
func (d *Doc) SetIndex(i bleve.Index) {
d.rw.Lock()
defer d.rw.Unlock()
d.i = i
}
// Index returns the Bleve index from doc.
func (d *Doc) Index() bleve.Index {
d.rw.RLock()
defer d.rw.RUnlock()
return d.i
}
// ProjectToPath converts a gitlab project to a path that can be used in Fetch.
func ProjectToPath(p *gitlab.Project) string {
url, _ := url.Parse(p.WebURL)
return url.Path
}
// PathToProject joins the elements and create a project string
func PathToProject(elem ...string) string {
elem = append([]string{"/"}, elem...)
return path.Join(elem...)
}
// RemoveFirstPathElement removes the first element from the path p. This is need to remove the Docs dir from
// the files downloaded from GitLab.
func RemoveFirstPathElement(p string) string {
el := strings.Split(p, "/")
if len(el) == 0 {
return ""
}
return path.Join(el[1:]...)
}