forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentryAttributes.go
350 lines (310 loc) · 8.87 KB
/
entryAttributes.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package plugin
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/Benchkram/errz"
"github.com/puppetlabs/wash/munge"
)
// JSONObject is a typedef to a map[string]interface{}
// object.
type JSONObject = map[string]interface{}
// ToJSONObject serializes v to a JSON object. It will panic
// if the serialization fails.
func ToJSONObject(v interface{}) JSONObject {
if obj, ok := v.(JSONObject); ok {
return obj
}
var err error
var inrec []byte
if arr, ok := v.([]byte); ok {
inrec = arr
} else {
if inrec, err = json.Marshal(v); err != nil {
// Internal error if we can't marshal an object
panic(err)
}
}
var obj JSONObject
// Internal error if not a JSON object
errz.Fatal(json.Unmarshal(inrec, &obj))
return obj
}
// Shell describes a command shell.
type Shell int
// Defines specific Shell classes you can configure
const (
UnknownShell Shell = iota
POSIXShell
PowerShell
)
var shellNames = [3]string{"unknown", "posixshell", "powershell"}
func (sh Shell) String() string {
return shellNames[sh]
}
// OS contains information about the operating system of a compute-like entry
type OS struct {
// LoginShell describes the type of shell execution occurs in. It can be used
// by the caller to decide what type of commands to run.
LoginShell Shell
}
// ToMap converts the OS struct data to a map.
func (o OS) ToMap() map[string]interface{} {
return map[string]interface{}{
"login_shell": o.LoginShell.String(),
}
}
/*
EntryAttributes represents an entry's attributes. We use a struct
instead of a map for efficient memory allocation/deallocation,
which is needed to make Parent#List fast.
Each of the setters supports the builder pattern, which enables you
to do something like
attr := plugin.EntryAttributes{}
attr.
SetCrtime(crtime).
SetMtime(mtime)
entry.SetAttributes(attr)
*/
type EntryAttributes struct {
atime time.Time
mtime time.Time
ctime time.Time
crtime time.Time
os OS
hasOS bool // identifies that the OS struct has valid operating system information
mode os.FileMode
hasMode bool
size uint64
hasSize bool
}
// We can't just export EntryAttributes' fields because there's no way
// to determine if an arbitrary entry has e.g. a 'size' attribute from
// the size value alone (since 0-size is valid). That's why we have the
// separate has* fields, and that's why those attributes need their own
// setters. However, it's a bit weird to have setters for some fields
// and not have setters for others (e.g. we could export atime, mtime,
// ctime b/c we know that an entry has atime/mtime/ctime if their value
// isn't the zero-time). It also increases the chance that a plugin author
// could inadvertantly forget to call the `size`/`mode` attribute setter
// when creating their attributes and instead set those values in the
// constructor (via something like EntryAttributes{Ctime: time.Now(), Size: 15}).
// The latter's bad b/c Wash would think the entry didn't have a size attribute
// (since hasSize is false).
//
// Thus, although these getters/setters/Has* methods are annoying, they're
// the best way to maintain a clean and consistent interface for EntryAttributes
// while minimizing plugin author error.
// HasAtime returns true if the entry has a last access time
func (a *EntryAttributes) HasAtime() bool {
return !a.atime.IsZero()
}
// Atime returns the entry's last access time
func (a *EntryAttributes) Atime() time.Time {
return a.atime
}
// SetAtime sets the entry's last access time
func (a *EntryAttributes) SetAtime(atime time.Time) *EntryAttributes {
a.atime = atime
return a
}
// HasMtime returns true if the entry has a last modified time
func (a *EntryAttributes) HasMtime() bool {
return !a.mtime.IsZero()
}
// Mtime returns the entry's last modified time
func (a *EntryAttributes) Mtime() time.Time {
return a.mtime
}
// SetMtime sets the entry's last modified time
func (a *EntryAttributes) SetMtime(mtime time.Time) *EntryAttributes {
a.mtime = mtime
return a
}
// HasCtime returns true if the entry has a change time
func (a *EntryAttributes) HasCtime() bool {
return !a.ctime.IsZero()
}
// Ctime returns the entry's change time
func (a *EntryAttributes) Ctime() time.Time {
return a.ctime
}
// SetCtime sets the entry's change time
func (a *EntryAttributes) SetCtime(ctime time.Time) *EntryAttributes {
a.ctime = ctime
return a
}
// HasCrtime returns true if the entry has a creation time
func (a *EntryAttributes) HasCrtime() bool {
return !a.crtime.IsZero()
}
// Crtime returns the entry's creation time
func (a *EntryAttributes) Crtime() time.Time {
return a.crtime
}
// SetCrtime sets the entry's creation time
func (a *EntryAttributes) SetCrtime(crtime time.Time) *EntryAttributes {
a.crtime = crtime
return a
}
// HasOS returns true if the entry has information about its OS
func (a *EntryAttributes) HasOS() bool {
return a.hasOS
}
// OS returns the entry's operating system information
func (a *EntryAttributes) OS() OS {
return a.os
}
// SetOS sets the entry's operating system information
func (a *EntryAttributes) SetOS(os OS) *EntryAttributes {
a.os = os
a.hasOS = true
return a
}
// HasMode returns true if the entry has a mode
func (a *EntryAttributes) HasMode() bool {
return a.hasMode
}
// Mode returns the entry's mode
func (a *EntryAttributes) Mode() os.FileMode {
return a.mode
}
// SetMode sets the entry's mode
func (a *EntryAttributes) SetMode(mode os.FileMode) *EntryAttributes {
a.mode = mode
a.hasMode = true
return a
}
// HasSize returns true if the entry has a size
func (a *EntryAttributes) HasSize() bool {
return a.hasSize
}
// Size returns the entry's Size
func (a *EntryAttributes) Size() uint64 {
return a.size
}
// SetSize sets the entry's size
func (a *EntryAttributes) SetSize(size uint64) *EntryAttributes {
a.size = size
a.hasSize = true
return a
}
// ToMap converts the entry's attributes to a map, which makes it easier to write
// generic code on them.
func (a *EntryAttributes) ToMap() map[string]interface{} {
mp := make(map[string]interface{})
if a.HasAtime() {
mp["atime"] = a.Atime()
}
if a.HasMtime() {
mp["mtime"] = a.Mtime()
}
if a.HasCtime() {
mp["ctime"] = a.Ctime()
}
if a.HasCrtime() {
mp["crtime"] = a.Crtime()
}
if a.HasOS() {
mp["os"] = a.OS().ToMap()
}
if a.HasMode() {
// The mode string representation is the only portable representation. FileMode uses its own
// definitions for type bits, not those in http://man7.org/linux/man-pages/man7/inode.7.html.
mp["mode"] = a.Mode().String()
}
if a.HasSize() {
mp["size"] = a.Size()
}
return mp
}
// MarshalJSON marshals the entry's attributes to JSON. It takes
// a value receiver so that the attributes are still marshaled
// when they're referenced as interface{} objects. See
// https://stackoverflow.com/a/21394657 for more details.
func (a EntryAttributes) MarshalJSON() ([]byte, error) {
m := a.ToMap()
// Override Mode to use a re-marshalable representation.
if a.HasMode() {
m["mode"] = a.Mode()
}
return json.Marshal(m)
}
// UnmarshalJSON unmarshals the entry's attributes from JSON.
func (a *EntryAttributes) UnmarshalJSON(data []byte) error {
mp := make(map[string]interface{})
err := json.Unmarshal(data, &mp)
if err != nil {
return fmt.Errorf("plugin.EntryAttributes.UnmarshalJSON received a non-JSON object")
}
if atime, ok := mp["atime"]; ok {
t, err := munge.ToTime(atime)
if err != nil {
return attrMungeError("atime", err)
}
a.SetAtime(t)
}
if mtime, ok := mp["mtime"]; ok {
t, err := munge.ToTime(mtime)
if err != nil {
return attrMungeError("mtime", err)
}
a.SetMtime(t)
}
if ctime, ok := mp["ctime"]; ok {
t, err := munge.ToTime(ctime)
if err != nil {
return attrMungeError("ctime", err)
}
a.SetCtime(t)
}
if crtime, ok := mp["crtime"]; ok {
t, err := munge.ToTime(crtime)
if err != nil {
return attrMungeError("crtime", err)
}
a.SetCrtime(t)
}
if obj, ok := mp["os"]; ok {
os, ok := obj.(map[string]interface{})
if !ok {
return attrMungeError("os", fmt.Errorf("os must be an object"))
}
var o OS
if obj, ok := os["login_shell"]; ok {
shell, ok := obj.(string)
if !ok {
return attrMungeError("os", fmt.Errorf("login_shell must be a string"))
}
for i, name := range shellNames {
if shell == name {
o.LoginShell = Shell(i)
}
}
if o.LoginShell == UnknownShell {
errTxt := "provided unknown login shell %v, must be %v or %v"
return attrMungeError("os", fmt.Errorf(errTxt, shell, PowerShell, POSIXShell))
}
}
a.SetOS(o)
}
if mode, ok := mp["mode"]; ok {
md, err := munge.ToUintMode(mode)
if err != nil {
return attrMungeError("mode", err)
}
a.SetMode(os.FileMode(md))
}
if size, ok := mp["size"]; ok {
sz, err := munge.ToSize(size)
if err != nil {
return attrMungeError("size", err)
}
a.SetSize(sz)
}
return nil
}
func attrMungeError(name string, err error) error {
return fmt.Errorf("plugin.EntryAttributes.UnmarshalJSON: could not munge the %v attribute: %v", name, err)
}