-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathuploadfile.go
213 lines (180 loc) · 5.04 KB
/
uploadfile.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
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
package cluster
import (
"fmt"
"os"
"path"
"strconv"
"strings"
"github.com/bmatcuk/doublestar/v4"
"github.com/jellydator/validation"
log "github.com/sirupsen/logrus"
)
type LocalFile struct {
Path string
PermMode string
}
// UploadFile describes a file to be uploaded for the host
type UploadFile struct {
Name string `yaml:"name,omitempty"`
Source string `yaml:"src"`
DestinationDir string `yaml:"dstDir"`
DestinationFile string `yaml:"dst"`
PermMode interface{} `yaml:"perm"`
DirPermMode interface{} `yaml:"dirPerm"`
User string `yaml:"user"`
Group string `yaml:"group"`
PermString string `yaml:"-"`
DirPermString string `yaml:"-"`
Sources []*LocalFile `yaml:"-"`
Base string `yaml:"-"`
}
func (u UploadFile) Validate() error {
return validation.ValidateStruct(&u,
validation.Field(&u.Source, validation.Required),
validation.Field(&u.DestinationFile, validation.Required.When(u.DestinationDir == "").Error("dst or dstdir required")),
validation.Field(&u.DestinationDir, validation.Required.When(u.DestinationFile == "").Error("dst or dstdir required")),
)
}
// converts string or integer value to octal string for chmod
func permToString(val interface{}) (string, error) {
var s string
switch t := val.(type) {
case int, float64:
var num int
if n, ok := t.(float64); ok {
num = int(n)
} else {
num = t.(int)
}
if num < 0 {
return s, fmt.Errorf("invalid permission: %d: must be a positive value", num)
}
if num == 0 {
return s, fmt.Errorf("invalid nil permission")
}
s = fmt.Sprintf("%#o", num)
case string:
s = t
default:
return "", nil
}
for i, c := range s {
n, err := strconv.Atoi(string(c))
if err != nil {
return s, fmt.Errorf("failed to parse permission %s: %w", s, err)
}
// These could catch some weird octal conversion mistakes
if i == 1 && n < 4 {
return s, fmt.Errorf("invalid permission %s: owner would have unconventional access", s)
}
if n > 7 {
return s, fmt.Errorf("invalid permission %s: octal value can't have numbers over 7", s)
}
}
return s, nil
}
// UnmarshalYAML sets in some sane defaults when unmarshaling the data from yaml
func (u *UploadFile) UnmarshalYAML(unmarshal func(interface{}) error) error {
type uploadFile UploadFile
yu := (*uploadFile)(u)
if err := unmarshal(yu); err != nil {
return err
}
fp, err := permToString(u.PermMode)
if err != nil {
return err
}
u.PermString = fp
dp, err := permToString(u.DirPermMode)
if err != nil {
return err
}
u.DirPermString = dp
return u.resolve()
}
// String returns the file bundle name or if it is empty, the source.
func (u *UploadFile) String() string {
if u.Name == "" {
return u.Source
}
return u.Name
}
// Owner returns a chown compatible user:group string from User and Group, or empty when neither are set.
func (u *UploadFile) Owner() string {
return strings.TrimSuffix(fmt.Sprintf("%s:%s", u.User, u.Group), ":")
}
// returns true if the string contains any glob characters
func isGlob(s string) bool {
return strings.ContainsAny(s, "*%?[]{}")
}
// sets the destination and resolves any globs/local paths into u.Sources
func (u *UploadFile) resolve() error {
if u.IsURL() {
if u.DestinationFile == "" {
if u.DestinationDir != "" {
u.DestinationFile = path.Join(u.DestinationDir, path.Base(u.Source))
} else {
u.DestinationFile = path.Base(u.Source)
}
}
return nil
}
if isGlob(u.Source) {
return u.glob(u.Source)
}
stat, err := os.Stat(u.Source)
if err != nil {
return fmt.Errorf("failed to stat local path for %s: %w", u, err)
}
if stat.IsDir() {
log.Tracef("source %s is a directory, assuming %s/**/*", u.Source, u.Source)
return u.glob(path.Join(u.Source, "**/*"))
}
perm := u.PermString
if perm == "" {
perm = fmt.Sprintf("%o", stat.Mode())
}
u.Base = path.Dir(u.Source)
u.Sources = []*LocalFile{
{Path: path.Base(u.Source), PermMode: perm},
}
return nil
}
// finds files based on a glob pattern
func (u *UploadFile) glob(src string) error {
base, pattern := doublestar.SplitPattern(src)
u.Base = base
fsys := os.DirFS(base)
sources, err := doublestar.Glob(fsys, pattern)
if err != nil {
return err
}
for _, s := range sources {
abs := path.Join(base, s)
log.Tracef("glob %s found: %s", abs, s)
stat, err := os.Stat(abs)
if err != nil {
return fmt.Errorf("failed to stat file %s: %w", u, err)
}
if stat.IsDir() {
log.Tracef("%s is a directory", abs)
continue
}
perm := u.PermString
if perm == "" {
perm = fmt.Sprintf("%o", stat.Mode())
}
u.Sources = append(u.Sources, &LocalFile{Path: s, PermMode: perm})
}
if len(u.Sources) == 0 {
return fmt.Errorf("no files found for %s", u)
}
if u.DestinationFile != "" && len(u.Sources) > 1 {
return fmt.Errorf("found multiple files for %s but single file dst %s defined", u, u.DestinationFile)
}
return nil
}
// IsURL returns true if the source is a URL
func (u *UploadFile) IsURL() bool {
return strings.Contains(u.Source, "://")
}