Skip to content

Commit 58dc88a

Browse files
authored
Add Name() to ClusterManager (#626)
Signed-off-by: Tamal Saha <[email protected]>
1 parent c75b17f commit 58dc88a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

api/v1/cluster.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/sha256"
2222
"encoding/base64"
2323
"fmt"
24+
"math/bits"
2425
"strings"
2526
)
2627

@@ -177,6 +178,18 @@ func (cm ClusterManager) Strings() []string {
177178
return out
178179
}
179180

181+
func isPowerOfTwo(n int) bool {
182+
return n > 0 && (n&(n-1)) == 0
183+
}
184+
185+
func (cm ClusterManager) Name() string {
186+
if !isPowerOfTwo(int(cm)) {
187+
return cm.String()
188+
}
189+
idx := bits.TrailingZeros(uint(cm))
190+
return _ClusterManagerNames[idx]
191+
}
192+
180193
func (cm ClusterManager) String() string {
181194
return strings.Join(cm.Strings(), ",")
182195
}

api/v1/cluster_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright AppsCode Inc. and Contributors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import "testing"
20+
21+
func TestClusterManager_Name(t *testing.T) {
22+
tests := []struct {
23+
name string
24+
cm ClusterManager
25+
want string
26+
}{
27+
{
28+
name: "ClusterManagerOpenShift",
29+
cm: ClusterManagerOpenShift,
30+
want: "OpenShift",
31+
},
32+
}
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
got := tt.cm.Name()
36+
if got != tt.want {
37+
t.Errorf("Name() = %v, want %v", got, tt.want)
38+
}
39+
})
40+
}
41+
}

0 commit comments

Comments
 (0)