forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
content.go
116 lines (105 loc) · 2.73 KB
/
content.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
package fake
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"github.com/jenkins-x/go-scm/scm"
"github.com/pkg/errors"
)
const (
// DefaultFileWritePermissions default permissions when creating a file
DefaultFileWritePermissions = 0644
)
type contentService struct {
client *wrapper
data *Data
}
func (c contentService) Find(_ context.Context, repo, path, ref string) (*scm.Content, *scm.Response, error) {
f, err := c.path(repo, path, ref)
if err != nil {
return nil, nil, err
}
_, err = os.Stat(f)
if os.IsNotExist(err) {
return nil, &scm.Response{
Status: 404,
}, errors.Wrapf(err, "file %s does not exist", f)
}
data, err := ioutil.ReadFile(f)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to read file %s", f)
}
return &scm.Content{
Path: path,
Data: data,
Sha: ref,
}, nil, nil
}
func (c contentService) List(_ context.Context, repo, path, ref string) ([]*scm.FileEntry, *scm.Response, error) {
dir, err := c.path(repo, path, ref)
if err != nil {
return nil, nil, err
}
fileNames, err := ioutil.ReadDir(dir)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to list files in directory %s", dir)
}
var answer []*scm.FileEntry
for _, f := range fileNames {
name := f.Name()
t := "file"
if f.IsDir() {
t = "dir"
}
path := filepath.Join(dir, name)
answer = append(answer, &scm.FileEntry{
Name: name,
Path: path,
Type: t,
Size: int(f.Size()),
Sha: ref,
Link: "file://" + path,
})
}
return answer, nil, nil
}
func (c contentService) Create(_ context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
f, err := c.path(repo, path, "")
if err != nil {
return nil, err
}
err = ioutil.WriteFile(f, params.Data, DefaultFileWritePermissions)
if err != nil {
return nil, errors.Wrapf(err, "failed to write file %s", f)
}
return nil, nil
}
func (c contentService) Update(_ context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
f, err := c.path(repo, path, "")
if err != nil {
return nil, err
}
err = ioutil.WriteFile(f, params.Data, DefaultFileWritePermissions)
if err != nil {
return nil, errors.Wrapf(err, "failed to write file %s", f)
}
return nil, nil
}
func (c contentService) Delete(_ context.Context, repo, path, ref string) (*scm.Response, error) {
f, err := c.path(repo, path, ref)
if err != nil {
return nil, err
}
err = os.Remove(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to delete file %s", f)
}
return nil, nil
}
func (c contentService) path(repo string, path string, ref string) (string, error) {
if c.data.ContentDir == "" {
return "", errors.Errorf("no data.ContentDir configured")
}
return filepath.Join(c.data.ContentDir, repo, path), nil
}