-
Notifications
You must be signed in to change notification settings - Fork 202
/
install.go
112 lines (90 loc) · 3.23 KB
/
install.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
package plugins
import (
"fmt"
"sort"
"strings"
"get.porter.sh/porter/pkg/cnab"
"get.porter.sh/porter/pkg/pkgmgmt"
"get.porter.sh/porter/pkg/portercontext"
)
// SchemaTypePlugins is the default schemaType value for InstallPluginsSpec resources
const SchemaTypePlugins = "Plugins"
// InstallPluginsSchemaVersion represents the version associated with the schema
// plugins configuration documents.
var InstallPluginsSchemaVersion = cnab.SchemaVersion("1.0.0")
type InstallOptions struct {
pkgmgmt.InstallOptions
File string
}
func (o *InstallOptions) Validate(args []string, cxt *portercontext.Context) error {
o.PackageType = "plugin"
if o.File != "" {
if len(args) > 0 {
return fmt.Errorf("plugin name should not be specified when --file is provided")
}
if o.URL != "" {
return fmt.Errorf("plugin URL should not be specified when --file is provided")
}
// version should not be set to anything other than the default value
if o.Version != "" && o.Version != "latest" {
return fmt.Errorf("plugin version %s should not be specified when --file is provided", o.Version)
}
if _, err := cxt.FileSystem.Stat(o.File); err != nil {
return fmt.Errorf("unable to access --file %s: %w", o.File, err)
}
return nil
}
return o.InstallOptions.Validate(args)
}
// InstallPluginsSpec represents the user-defined configuration for plugins installation.
type InstallPluginsSpec struct {
SchemaType string `yaml:"schemaType"`
SchemaVersion string `yaml:"schemaVersion"`
Plugins InstallPluginsConfig `yaml:"plugins"`
}
func (spec InstallPluginsSpec) Validate() error {
if spec.SchemaType != "" && !strings.EqualFold(spec.SchemaType, SchemaTypePlugins) {
return fmt.Errorf("invalid schemaType %s, expected %s", spec.SchemaType, SchemaTypePlugins)
}
if InstallPluginsSchemaVersion != cnab.SchemaVersion(spec.SchemaVersion) {
if spec.SchemaVersion == "" {
spec.SchemaVersion = "(none)"
}
return fmt.Errorf("invalid schemaVersion provided: %s. This version of Porter is compatible with %s.", spec.SchemaVersion, InstallPluginsSchemaVersion)
}
return nil
}
// InstallPluginsConfig is the go representation of plugin installation file format.
type InstallPluginsConfig map[string]pkgmgmt.InstallOptions
// InstallPluginsConfigList is a sorted list of InstallationFileOption in alphabetical order.
type InstallPluginsConfigList struct {
data InstallPluginsConfig
keys []string
}
// NewInstallPluginConfigs returns a new instance of InstallPluginConfigs with plugins sorted in alphabetical order
// using their names.
func NewInstallPluginConfigs(opt InstallPluginsConfig) InstallPluginsConfigList {
keys := make([]string, 0, len(opt))
data := make(InstallPluginsConfig, len(opt))
for k, v := range opt {
keys = append(keys, k)
v.Name = k
v.PackageType = "plugin"
data[k] = v
}
sort.SliceStable(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
return InstallPluginsConfigList{
data: data,
keys: keys,
}
}
// Values returns InstallOptions list in alphabetical order.
func (pc InstallPluginsConfigList) Values() []pkgmgmt.InstallOptions {
value := make([]pkgmgmt.InstallOptions, 0, len(pc.keys))
for _, k := range pc.keys {
value = append(value, pc.data[k])
}
return value
}