Skip to content

Commit a37471d

Browse files
committed
Preset: Add presetMap variable
It remove some of the duplication we are having with switch case and also allow us to use it for another helper which can provide us list of all preset ( next commit)
1 parent cdc618e commit a37471d

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

pkg/crc/preset/preset.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ const (
1515
Microshift Preset = "microshift"
1616
)
1717

18+
var presetMap = map[Preset]string{
19+
Podman: string(Podman),
20+
OpenShift: string(OpenShift),
21+
OKD: string(OKD),
22+
Microshift: string(Microshift),
23+
}
24+
1825
func (preset Preset) String() string {
19-
switch preset {
20-
case Podman:
21-
return string(Podman)
22-
case OpenShift:
23-
return string(OpenShift)
24-
case OKD:
25-
return string(OKD)
26-
case Microshift:
27-
return string(Microshift)
26+
presetStr, presetExists := presetMap[preset]
27+
if !presetExists {
28+
return "invalid"
2829
}
29-
return "invalid"
30+
return presetStr
3031
}
3132

3233
func (preset Preset) ForDisplay() string {
@@ -44,18 +45,13 @@ func (preset Preset) ForDisplay() string {
4445
}
4546

4647
func ParsePresetE(input string) (Preset, error) {
47-
switch input {
48-
case Podman.String():
49-
return Podman, nil
50-
case OpenShift.String():
51-
return OpenShift, nil
52-
case OKD.String():
53-
return OKD, nil
54-
case Microshift.String():
55-
return Microshift, nil
56-
default:
57-
return OpenShift, fmt.Errorf("Cannot parse preset '%s'", input)
48+
for pSet, pString := range presetMap {
49+
if pString == input {
50+
return pSet, nil
51+
}
5852
}
53+
return OpenShift, fmt.Errorf("Cannot parse preset '%s'", input)
54+
5955
}
6056
func ParsePreset(input string) Preset {
6157
preset, err := ParsePresetE(input)

0 commit comments

Comments
 (0)