-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathdirectory_parameter.go
104 lines (90 loc) · 3.73 KB
/
directory_parameter.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
package cnab
import (
"encoding/json"
"github.com/cnabio/cnab-go/bundle/definition"
"github.com/docker/docker/api/types/mount"
"github.com/pkg/errors"
)
const (
DirectoryExtensionShortHand = "directory-parameter"
DirectoryParameterExtensionKey = PorterExtensionsPrefix + DirectoryExtensionShortHand
)
// DirectoryParameterDefinition represents those parameter options
// That apply exclusively to the directory parameter type
type DirectoryParameterDefinition struct {
Writeable bool `yaml:"writeable,omitempty"`
// UID and GID should be ints, however 0 is the default value for int type
// But is also a realistic value for UID/GID thus we need to make the type interface
// To detect the case that the values weren't set
GID interface{} `yaml:"gid,omitempty" json:"gid,omitempty"`
UID interface{} `yaml:"uid,omitempty" json:"uid,omitempty"`
}
// MountParameterSource represents a parameter using a docker mount
// As a its source with the provided options
type MountParameterSourceDefn struct {
mount.Mount `yaml:",inline"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
}
// DirectorySources represents the sources available to the directory parameter type
// Currently only mount has been specified, but this could change in the future
type DirectorySources struct {
Mount MountParameterSourceDefn `yaml:"mount,omitempty" json:"mount,omitempty"`
}
type DirectoryDetails struct {
DirectorySources
DirectoryParameterDefinition
Kind string `json:"kind,omitempty"`
}
// DirectoryParameterExtension indicates that Directory support is required
var DirectoryParameterExtension = RequiredExtension{
Shorthand: DirectoryExtensionShortHand,
Key: DirectoryParameterExtensionKey,
Reader: DirectoryParameterReader,
}
// SupportsDirectoryParameters returns true if the bundle supports the
// Directory parameter extension
func (b ExtendedBundle) SupportsDirectoryParameters() bool {
return b.SupportsExtension(DirectoryParameterExtensionKey)
}
// IsDirType determines if the parameter/credential is of type "directory".
func (b ExtendedBundle) IsDirType(def *definition.Schema) bool {
return b.SupportsDirectoryParameters() && def.Type == "string" && def.Comment == DirectoryParameterExtensionKey
}
// DirectoryParameterReader is a Reader for the DirectoryParameterExtension.
// The extension maintains the list of directory parameters in the bundle
func DirectoryParameterReader(b ExtendedBundle) (interface{}, error) {
return b.DirectoryParameterReader()
}
// DirectoryParameterReader is a Reader for the DirectoryParameterExtension.
// This method generates the list of directory parameter names in the bundle.
// The Directory Parameter extension maintains the list of directory parameters in the bundle
func (b ExtendedBundle) DirectoryParameterReader() (interface{}, error) {
bytes, err := json.Marshal(b.Custom[DirectoryParameterExtensionKey])
if err != nil {
return nil, errors.Wrapf(err, "Failed to marshal custom extension %s", DirectoryParameterExtensionKey)
}
var dd map[string]DirectoryDetails
if err = errors.Wrapf(json.Unmarshal(bytes, &dd), "Failed to unmarshal custom extension %s %s", DirectoryParameterExtensionKey, string(bytes)); err != nil {
return nil, err
}
dirs := make([]DirectoryDetails, len(dd))
i := 0
for _, dir := range dd {
dirs[i] = dir
i++
}
return dirs, nil
}
// DirectoryParameterSupport checks if the Directory parameter extension is present
func (e ProcessedExtensions) DirectoryParameterSupport() bool {
_, extensionRequired := e[DirectoryParameterExtensionKey]
return extensionRequired
}
// IDToInt converts an interface to an integer. If the id is coercable to an int, returns the value
// Otherwise returns -1
func IDToInt(id interface{}) int {
if i, ok := id.(int); ok {
return i
}
return -1
}