-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathdatastore.go
114 lines (88 loc) · 2.79 KB
/
datastore.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
package config
import "github.com/pkg/errors"
// Data is the data stored in PORTER_HOME/porter.toml|yaml|json.
// Use the accessor functions to ensure default values are handled properly.
type Data struct {
// Only define fields here that you need to access from code
// Values are dynamically applied to flags and don't need to be defined
// DefaultStoragePlugin is the storage plugin to use when no named storage is specified.
DefaultStoragePlugin string `mapstructure:"default-storage-plugin"`
// DefaultStorage to use when a named storage is not specified by a flag.
DefaultStorage string `mapstructure:"default-storage"`
// CrudStores defined in the configuration file.
CrudStores []CrudStore `mapstructure:"storage"`
// DefaultSecretsPlugin is the plugin to use when no plugin is specified.
DefaultSecretsPlugin string `mapstructure:"default-secrets-plugin"`
// DefaultSecrets to use when one is not specified by a flag.
DefaultSecrets string `mapstructure:"default-secrets"`
// SecretSources defined in the configuration file.
SecretSources []SecretSource `mapstructure:"secrets"`
}
// SecretSource is the plugin stanza for secrets.
type SecretSource struct {
PluginConfig `mapstructure:",squash"`
}
// CrudStore is the plugin stanza for storage.
type CrudStore struct {
PluginConfig `mapstructure:",squash"`
}
func (d *Data) GetDefaultStoragePlugin() string {
if d == nil || d.DefaultStoragePlugin == "" {
return "filesystem"
}
return d.DefaultStoragePlugin
}
func (d *Data) GetDefaultStorage() string {
if d == nil {
return ""
}
return d.DefaultStorage
}
func (d *Data) GetStorage(name string) (CrudStore, error) {
if d != nil {
for _, is := range d.CrudStores {
if is.Name == name {
return is, nil
}
}
}
return CrudStore{}, errors.New("store %q not defined")
}
func (d *Data) GetDefaultSecretsPlugin() string {
if d == nil || d.DefaultSecretsPlugin == "" {
return "host"
}
return d.DefaultSecretsPlugin
}
func (d *Data) GetDefaultSecretSource() string {
if d == nil {
return ""
}
return d.DefaultSecrets
}
func (d *Data) GetSecretSource(name string) (SecretSource, error) {
if d != nil {
for _, cs := range d.SecretSources {
if cs.Name == name {
return cs, nil
}
}
}
return SecretSource{}, errors.New("secrets %q not defined")
}
// PluginConfig is a standardized config stanza that defines which plugin to
// use and its custom configuration.
type PluginConfig struct {
Name string `mapstructure:"name"`
PluginSubKey string `mapstructure:"plugin"`
Config map[string]interface{} `mapstructure:"config"`
}
func (p PluginConfig) GetName() string {
return p.Name
}
func (p PluginConfig) GetPluginSubKey() string {
return p.PluginSubKey
}
func (p PluginConfig) GetConfig() interface{} {
return p.Config
}