This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mock_archive.go
79 lines (70 loc) · 1.59 KB
/
mock_archive.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
// Copyright 2016 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
package archivist
import (
"bytes"
"strings"
"io"
"io/ioutil"
"errors"
"sync"
)
type MockArchiveBackend struct {
mutex sync.Mutex
files map[string][]byte
}
func (b *MockArchiveBackend) Exists(pth string) bool {
b.mutex.Lock()
defer b.mutex.Unlock()
_, ok := b.files[pth]
return ok
}
func (b *MockArchiveBackend) GetFile(pth string) (io.ReadCloser, error) {
b.mutex.Lock()
defer b.mutex.Unlock()
var buf []byte
buf, ok := b.files[pth]
if !ok {
return nil, errors.New("no such file: " + pth)
}
return ioutil.NopCloser(bytes.NewReader(buf)), nil
}
func (b *MockArchiveBackend) PutFile(pth string, in io.ReadCloser) error {
b.mutex.Lock()
defer b.mutex.Unlock()
buf, e := ioutil.ReadAll(in)
if e != nil {
return e
}
b.files[pth] = buf
return nil
}
func (b *MockArchiveBackend) ListFiles(pth string) (chan string, chan error) {
b.mutex.Lock()
defer b.mutex.Unlock()
ch := make(chan string)
errs := make(chan error)
files := make([]string, 0, len(b.files))
for k, _ := range b.files {
files = append(files, k)
}
go func() {
for _, f := range files {
if strings.HasPrefix(f, pth) {
ch <- f
}
}
close(ch)
close(errs)
}()
return ch, errs
}
func (b *MockArchiveBackend) CanListFiles() bool {
return true
}
func MakeMockBackend(opts *ConnectOptions) ArchiveBackend {
b := new(MockArchiveBackend)
b.files = make(map[string][]byte)
return b
}