Skip to content

Commit c322721

Browse files
committed
Refactor to use switch instead of if-else chain
Signed-off-by: Oleksandr Redko <[email protected]>
1 parent ce73395 commit c322721

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

.golangci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,13 @@ linters-settings:
9696
- rangeValCopy
9797
- hugeParam
9898
- importShadow
99-
- ifElseChain
10099
- sprintfQuotedString
101100
- builtinShadow
102101
- filepathJoin
102+
settings:
103+
ifElseChain:
104+
# Min number of if-else blocks that makes the warning trigger.
105+
minThreshold: 3
103106
errorlint:
104107
asserts: false
105108
revive:

cmd/limactl/start.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
128128

129129
const yBytesLimit = 4 * 1024 * 1024 // 4MiB
130130

131-
if ok, u := guessarg.SeemsTemplateURL(arg); ok {
131+
isTemplateURL, templateURL := guessarg.SeemsTemplateURL(arg)
132+
switch {
133+
case isTemplateURL:
132134
// No need to use SecureJoin here. https://github.com/lima-vm/lima/pull/805#discussion_r853411702
133-
templateName := filepath.Join(u.Host, u.Path)
135+
templateName := filepath.Join(templateURL.Host, templateURL.Path)
134136
logrus.Debugf("interpreting argument %q as a template name %q", arg, templateName)
135137
if st.instName == "" {
136138
// e.g., templateName = "deprecated/centos-7" , st.instName = "centos-7"
@@ -140,7 +142,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
140142
if err != nil {
141143
return nil, err
142144
}
143-
} else if guessarg.SeemsHTTPURL(arg) {
145+
case guessarg.SeemsHTTPURL(arg):
144146
if st.instName == "" {
145147
st.instName, err = guessarg.InstNameFromURL(arg)
146148
if err != nil {
@@ -161,7 +163,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
161163
if err != nil {
162164
return nil, err
163165
}
164-
} else if guessarg.SeemsFileURL(arg) {
166+
case guessarg.SeemsFileURL(arg):
165167
if st.instName == "" {
166168
st.instName, err = guessarg.InstNameFromURL(arg)
167169
if err != nil {
@@ -178,7 +180,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
178180
if err != nil {
179181
return nil, err
180182
}
181-
} else if guessarg.SeemsYAMLPath(arg) {
183+
case guessarg.SeemsYAMLPath(arg):
182184
if st.instName == "" {
183185
st.instName, err = guessarg.InstNameFromYAMLPath(arg)
184186
if err != nil {
@@ -195,7 +197,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
195197
if err != nil {
196198
return nil, err
197199
}
198-
} else if arg == "-" {
200+
case arg == "-":
199201
if st.instName == "" {
200202
return nil, errors.New("must pass instance name with --name when reading template from stdin")
201203
}
@@ -209,7 +211,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
209211
return nil, errors.New("cannot use --tty=true and read template from stdin together")
210212
}
211213
tty = false
212-
} else {
214+
default:
213215
if arg == "" {
214216
if st.instName == "" {
215217
st.instName = DefaultInstanceName

pkg/cidata/cidata.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,18 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
270270
return err
271271
}
272272

273-
if len(y.DNS) > 0 {
273+
switch {
274+
case len(y.DNS) > 0:
274275
for _, addr := range y.DNS {
275276
args.DNSAddresses = append(args.DNSAddresses, addr.String())
276277
}
277-
} else if firstUsernetIndex != -1 || *y.VMType == limayaml.VZ {
278+
case firstUsernetIndex != -1 || *y.VMType == limayaml.VZ:
278279
args.DNSAddresses = append(args.DNSAddresses, args.SlirpDNS)
279-
} else if *y.HostResolver.Enabled {
280+
case *y.HostResolver.Enabled:
280281
args.UDPDNSLocalPort = udpDNSLocalPort
281282
args.TCPDNSLocalPort = tcpDNSLocalPort
282283
args.DNSAddresses = append(args.DNSAddresses, args.SlirpDNS)
283-
} else {
284+
default:
284285
args.DNSAddresses, err = osutil.DNSAddresses()
285286
if err != nil {
286287
return err

pkg/limayaml/validate.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ func validateNetwork(y *LimaYAML) error {
314314
interfaceName := make(map[string]int)
315315
for i, nw := range y.Networks {
316316
field := fmt.Sprintf("networks[%d]", i)
317-
if nw.Lima != "" {
317+
switch {
318+
case nw.Lima != "":
318319
config, err := networks.Config()
319320
if err != nil {
320321
return err
@@ -335,7 +336,7 @@ func validateNetwork(y *LimaYAML) error {
335336
if nw.VZNAT != nil && *nw.VZNAT {
336337
return fmt.Errorf("field `%s.lima` and field `%s.vzNAT` are mutually exclusive", field, field)
337338
}
338-
} else if nw.Socket != "" {
339+
case nw.Socket != "":
339340
if nw.VZNAT != nil && *nw.VZNAT {
340341
return fmt.Errorf("field `%s.socket` and field `%s.vzNAT` are mutually exclusive", field, field)
341342
}
@@ -344,7 +345,7 @@ func validateNetwork(y *LimaYAML) error {
344345
} else if err == nil && fi.Mode()&os.ModeSocket == 0 {
345346
return fmt.Errorf("field `%s.socket` %q points to a non-socket file", field, nw.Socket)
346347
}
347-
} else if nw.VZNAT != nil && *nw.VZNAT {
348+
case nw.VZNAT != nil && *nw.VZNAT:
348349
if y.VMType == nil || *y.VMType != VZ {
349350
return fmt.Errorf("field `%s.vzNAT` requires `vmType` to be %q", field, VZ)
350351
}
@@ -354,7 +355,7 @@ func validateNetwork(y *LimaYAML) error {
354355
if nw.Socket != "" {
355356
return fmt.Errorf("field `%s.vzNAT` and field `%s.socket` are mutually exclusive", field, field)
356357
}
357-
} else {
358+
default:
358359
return fmt.Errorf("field `%s.lima` or field `%s.socket must be set", field, field)
359360
}
360361
if nw.MACAddress != "" {

pkg/store/instance.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,15 @@ func inspectStatusWithPIDFiles(instDir string, inst *Instance, y *limayaml.LimaY
192192
}
193193

194194
if inst.Status == StatusUnknown {
195-
if inst.HostAgentPID > 0 && inst.DriverPID > 0 {
195+
switch {
196+
case inst.HostAgentPID > 0 && inst.DriverPID > 0:
196197
inst.Status = StatusRunning
197-
} else if inst.HostAgentPID == 0 && inst.DriverPID == 0 {
198+
case inst.HostAgentPID == 0 && inst.DriverPID == 0:
198199
inst.Status = StatusStopped
199-
} else if inst.HostAgentPID > 0 && inst.DriverPID == 0 {
200+
case inst.HostAgentPID > 0 && inst.DriverPID == 0:
200201
inst.Errors = append(inst.Errors, errors.New("host agent is running but driver is not"))
201202
inst.Status = StatusBroken
202-
} else {
203+
default:
203204
inst.Errors = append(inst.Errors, fmt.Errorf("%s driver is running but host agent is not", inst.VMType))
204205
inst.Status = StatusBroken
205206
}

0 commit comments

Comments
 (0)