forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.go
105 lines (90 loc) · 2.72 KB
/
features.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
/*
Copyright 2016 The Kubernetes Authors.
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 features
import (
"fmt"
"k8s.io/contrib/mungegithub/github"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
// Features are all features the code know about. Care should be taken
// not to try to use a feature which isn't 'active'
type Features struct {
Aliases *Aliases
Repos *RepoInfo
GCSInfo *GCSInfo
TestOptions *TestOptions
active []feature
}
type feature interface {
Name() string
AddFlags(cmd *cobra.Command)
Initialize(config *github.Config) error
EachLoop() error
}
var featureMap = map[string]feature{}
// GetActive returns all features requested by a munger
func (f *Features) GetActive() []feature {
return f.active
}
// Initialize should be called with the set of all features needed by all (active) mungers
func (f *Features) Initialize(config *github.Config, requestedFeatures []string) error {
for _, name := range requestedFeatures {
glog.Infof("Initializing feature: %v", name)
feat, found := featureMap[name]
if !found {
return fmt.Errorf("Could not find a feature named: %s", name)
}
f.active = append(f.active, featureMap[name])
if err := feat.Initialize(config); err != nil {
return err
}
switch name {
case RepoFeatureName:
f.Repos = feat.(*RepoInfo)
case GCSFeature:
f.GCSInfo = feat.(*GCSInfo)
case TestOptionsFeature:
f.TestOptions = feat.(*TestOptions)
case AliasesFeature:
f.Aliases = feat.(*Aliases)
}
}
return nil
}
// EachLoop allows active features to update every loop
func (f *Features) EachLoop() error {
for _, feat := range f.GetActive() {
if err := feat.EachLoop(); err != nil {
return err
}
}
return nil
}
// AddFlags allow every feature to add flags to the command
func (f *Features) AddFlags(cmd *cobra.Command) error {
for _, feat := range featureMap {
feat.AddFlags(cmd)
}
return nil
}
// RegisterFeature should be called in `init()` by each feature to make itself
// available by name
func RegisterFeature(feat feature) error {
if _, found := featureMap[feat.Name()]; found {
glog.Fatalf("a feature with the name (%s) already exists", feat.Name())
}
featureMap[feat.Name()] = feat
glog.Infof("Registered %#v at %s", feat, feat.Name())
return nil
}