forked from carabiner-dev/vcslocator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget.go
More file actions
253 lines (222 loc) · 5.82 KB
/
Copy pathget.go
File metadata and controls
253 lines (222 loc) · 5.82 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-FileCopyrightText: Copyright 2025 Carabiner Systems, Inc
// SPDX-License-Identifier: Apache-2.0
package vcslocator
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"sync"
"github.com/nozzle/throttler"
)
//nolint:errname // This is not an Error type
type ErrorList struct {
Errors []error
}
func (el *ErrorList) Error() string {
if err := errors.Join(el.Errors...); err != nil {
return err.Error()
}
return ""
}
type copyPlan struct {
Locator Locator
FS fs.FS
Components *Components
Files map[int]string
}
// GetGroup gets the data of several vcs locators in an efficient manner
func GetGroup[T ~string](locators []T) ([][]byte, error) {
buffers := make([]io.Writer, len(locators))
for i := range locators {
var b bytes.Buffer
buffers[i] = &b
}
if err := CopyFileGroup(locators, buffers); err != nil {
return nil, err
}
ret := [][]byte{}
for i, w := range buffers {
if b, ok := w.(*bytes.Buffer); ok {
ret = append(ret, b.Bytes())
} else {
return nil, fmt.Errorf("lost buffer #%d", i)
}
}
return ret, nil
}
// CopyFileGroup copies a group of locators to the specified writers
func CopyFileGroup[T ~string](locators []T, writers []io.Writer, funcs ...fnOpt) error {
if len(locators) != len(writers) {
return fmt.Errorf("number of writers does not match the number of VCS locators")
}
// First, create the clone plan
cloneList := map[string]*copyPlan{}
for i, l := range locators {
// Parse the locator
components, err := Locator(l).Parse()
if err != nil {
return fmt.Errorf("error parsing locator %d", i)
}
repostring := fmt.Sprintf("%s:%s", components.RepoURL(), components.RefString)
if _, ok := cloneList[repostring]; !ok {
cloneList[repostring] = ©Plan{
Locator: Locator(l),
Components: components,
Files: map[int]string{},
}
}
cloneList[repostring].Files[i] = components.SubPath
}
// Clone them repos
var mutex sync.Mutex
t := throttler.New(4, len(cloneList))
for repostring, copyplan := range cloneList {
go func(repostring string, copyplan *copyPlan) {
fsobj, err := CloneRepository(copyplan.Locator)
mutex.Lock()
cloneList[repostring].FS = fsobj
mutex.Unlock()
if err != nil {
err = fmt.Errorf("reading %q: %w", copyplan.Locator, err)
}
t.Done(err)
}(repostring, copyplan)
t.Throttle()
}
if err := t.Err(); err != nil {
return fmt.Errorf("error cloning repositories: %w", err)
}
// Now copy the files in parallel
errs := map[int]error{}
t2 := throttler.New(4, len(locators))
var emtx sync.Mutex
for _, copyplan := range cloneList {
for i, path := range copyplan.Files {
go func(i int, path string, copyplan *copyPlan) {
f, err := copyplan.FS.Open(path)
if err != nil {
emtx.Lock()
errs[i] = fmt.Errorf("opening path %d (%q): %w", i, path, err)
emtx.Unlock()
t2.Done(nil)
return
}
defer f.Close() //nolint:errcheck
if _, err := io.Copy(writers[i], f); err != nil {
emtx.Lock()
errs[i] = fmt.Errorf("copying data stream %d: %w", i, err)
emtx.Unlock()
t2.Done(nil)
return
}
t2.Done(nil)
}(i, path, copyplan)
t2.Throttle()
}
}
if len(errs) != 0 {
ret := []error{}
for i := range locators {
if err, ok := errs[i]; ok {
ret = append(ret, err)
} else {
ret = append(ret, nil)
}
}
return &ErrorList{
Errors: ret,
}
}
return nil
}
// CopyFile downloads a file specified by the VCS locator and copies it
// to an io.Writer.
func CopyFile[T ~string](locator T, w io.Writer, funcs ...fnOpt) error {
opts := defaultOptions
for _, fn := range funcs {
if err := fn(&opts); err != nil {
return err
}
}
l := Locator(locator)
components, err := l.Parse(funcs...)
if err != nil {
return fmt.Errorf("parsing locator: %w", err)
}
if components.SubPath == "" {
return errors.New("locator has no subpath defined")
}
fsobj, err := CloneRepository(locator, funcs...)
if err != nil {
return fmt.Errorf("cloning repository: %w", err)
}
f, err := fsobj.Open(components.SubPath)
if err != nil {
return fmt.Errorf("opening file: %w", err)
}
if _, err := io.Copy(w, f); err != nil {
return fmt.Errorf("copying data stream: %w", err)
}
return nil
}
// Download copies data from the git repository to the specified directory
func Download[T ~string](locator T, localDir string, funcs ...fnOpt) error {
opts := defaultOptions
for _, fn := range funcs {
if err := fn(&opts); err != nil {
return err
}
}
l := Locator(locator)
components, err := l.Parse(funcs...)
if err != nil {
return fmt.Errorf("parsing locator: %w", err)
}
if components.SubPath == "" {
return errors.New("locator has no subpath defined")
}
fsys, err := CloneRepository(locator, funcs...)
if err != nil {
return fmt.Errorf("cloning repository: %w", err)
}
// Walk the filesystem to fetch all we need
if err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.Fatal(err)
}
if d.IsDir() {
return nil
}
if !strings.HasPrefix(path, strings.TrimPrefix(components.SubPath, "/")) {
return nil
}
// We know all paths are files here, so we create the dir and copy
src, err := fsys.Open(path)
if err != nil {
return fmt.Errorf("opening file from source: %w", err)
}
defer src.Close() //nolint:errcheck
destDir := filepath.Join(localDir, filepath.Dir(path))
if err := os.MkdirAll(destDir, os.FileMode(0o755)); err != nil {
return fmt.Errorf("creating destination dir: %w", err)
}
dst, err := os.Create(filepath.Join(localDir, path))
if err != nil {
return fmt.Errorf("opening destination file: %w", err)
}
defer dst.Close() //nolint:errcheck
if _, err := io.Copy(dst, src); err != nil {
return fmt.Errorf("copying data stream: %w", err)
}
return nil
}); err != nil {
return err
}
return nil
}