@@ -86,6 +86,9 @@ type DriverDef struct {
86
86
// Name of the machine driver. It has to be unique.
87
87
Name string
88
88
89
+ // Alias contains a list of machine driver aliases. Each alias should also be unique.
90
+ Alias []string
91
+
89
92
// Config is a function that emits a configured driver struct
90
93
Config Configurator
91
94
@@ -109,13 +112,15 @@ func (d DriverDef) String() string {
109
112
}
110
113
111
114
type driverRegistry struct {
112
- drivers map [string ]DriverDef
113
- lock sync.RWMutex
115
+ drivers map [string ]DriverDef
116
+ driversByAlias map [string ]DriverDef
117
+ lock sync.RWMutex
114
118
}
115
119
116
120
func newRegistry () * driverRegistry {
117
121
return & driverRegistry {
118
- drivers : make (map [string ]DriverDef ),
122
+ drivers : make (map [string ]DriverDef ),
123
+ driversByAlias : make (map [string ]DriverDef ),
119
124
}
120
125
}
121
126
@@ -129,6 +134,13 @@ func (r *driverRegistry) Register(def DriverDef) error {
129
134
}
130
135
131
136
r .drivers [def .Name ] = def
137
+
138
+ for _ , alias := range def .Alias {
139
+ if _ , ok := r .driversByAlias [alias ]; ok {
140
+ return fmt .Errorf ("alias %q is already registered: %+v" , alias , def )
141
+ }
142
+ r .driversByAlias [alias ] = def
143
+ }
132
144
return nil
133
145
}
134
146
@@ -150,5 +162,12 @@ func (r *driverRegistry) List() []DriverDef {
150
162
func (r * driverRegistry ) Driver (name string ) DriverDef {
151
163
r .lock .RLock ()
152
164
defer r .lock .RUnlock ()
153
- return r .drivers [name ]
165
+
166
+ def , ok := r .drivers [name ]
167
+ if ok {
168
+ return def
169
+ }
170
+
171
+ // Check if we have driver def with name as alias
172
+ return r .driversByAlias [name ]
154
173
}
0 commit comments