Repository navigation
Expand file tree
/
Copy pathaddon.go
More file actions
171 lines (131 loc) · 4.86 KB
/
Copy pathaddon.go
File metadata and controls
171 lines (131 loc) · 4.86 KB
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
/*
Copyright (C) 2017 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package addon
import (
"fmt"
"github.com/minishift/minishift/pkg/minishift/addon/command"
)
// AddOn is the internal representation of an AddOn including the addon metadata and the actual commands
// the Addon consists off.
type AddOn interface {
// MetaData returns the meta data of this addon.
MetaData() AddOnMeta
// MetaDataForAddonRemove returns the meta data for this addon when remove subcommand used.
MetaDataForAddonRemove() AddOnMeta
// Commands returns a Command slice of the commands this addon consists of.
Commands() []command.Command
// RemoveCommands returns a slice of the commands which are used to remove this addon.
RemoveCommands() []command.Command
// InstallPath returns the path under which the addon is installed.
InstallPath() string
// IsEnabled returns true, if this addon is currently enabled and getting applied as part of OpenShift
// provisioning. Returns false, if this addon is not enabled.
IsEnabled() bool
// SetEnabled sets this addon as en- or disabled.
SetEnabled(enabled bool)
// GetPriority returns the priority of this add on in terms of the order in which it gets applied
GetPriority() int
// SetPriority sets the priority of this addon in terms of its installation order. Addons with lower priority
// gets installed first
SetPriority(priority int)
}
// DefaultAddOn provides the default implementation of the AddOn interface
type DefaultAddOn struct {
AddOn
metaData AddOnMeta
metaDataForAddonRemove AddOnMeta
commands []command.Command
removeCommands []command.Command
enabled bool
path string
priority int
}
func NewAddOn(meta AddOnMeta, metaDataForAddonRemove AddOnMeta, commands []command.Command, removeCommands []command.Command, path string) AddOn {
addOn := DefaultAddOn{
metaData: meta,
metaDataForAddonRemove: metaDataForAddonRemove,
commands: commands,
removeCommands: removeCommands,
path: path,
}
return &addOn
}
func (a *DefaultAddOn) Commands() []command.Command {
return a.commands
}
func (a *DefaultAddOn) RemoveCommands() []command.Command {
return a.removeCommands
}
func (a *DefaultAddOn) MetaData() AddOnMeta {
return a.metaData
}
func (a *DefaultAddOn) MetaDataForAddonRemove() AddOnMeta {
return a.metaDataForAddonRemove
}
func (a *DefaultAddOn) IsEnabled() bool {
return a.enabled
}
func (a *DefaultAddOn) SetEnabled(enabled bool) {
a.enabled = enabled
}
func (a *DefaultAddOn) SetPriority(priority int) {
a.priority = priority
}
func (a *DefaultAddOn) GetPriority() int {
return a.priority
}
func (a *DefaultAddOn) InstallPath() string {
return a.path
}
func (meta *DefaultAddOn) String() string {
return fmt.Sprintf("%#v", meta)
}
// ByPriority implements sort.Interface for []AddOn based on the Priority field.
type ByPriority []AddOn
func (a ByPriority) Len() int { return len(a) }
func (a ByPriority) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByPriority) Less(i, j int) bool { return a[i].GetPriority() < a[j].GetPriority() }
// ByStatusThenName implements sort.Interface for []AddOn based primarily on the addon enable state and secondarily
// on the addon name.
type ByStatusThenName []AddOn
func (a ByStatusThenName) Len() int { return len(a) }
func (a ByStatusThenName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByStatusThenName) Less(i, j int) bool {
if a[i].IsEnabled() && !a[j].IsEnabled() {
return true
}
if !a[i].IsEnabled() && a[j].IsEnabled() {
return false
}
return a[i].MetaData().Name() < a[j].MetaData().Name()
}
// ByStatusThenPriority implements sort.Interface for []AddOn based primarily on the addon enable state and secondarily
// on the addon priority. Lastly the name is considered
type ByStatusThenPriorityThenName []AddOn
func (a ByStatusThenPriorityThenName) Len() int { return len(a) }
func (a ByStatusThenPriorityThenName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByStatusThenPriorityThenName) Less(i, j int) bool {
if a[i].IsEnabled() && !a[j].IsEnabled() {
return true
}
if !a[i].IsEnabled() && a[j].IsEnabled() {
return false
}
if a[i].GetPriority() < a[j].GetPriority() {
return true
}
if a[i].GetPriority() < a[j].GetPriority() {
return false
}
return a[i].MetaData().Name() < a[j].MetaData().Name()
}