From b4e5a74e4c5d3d833f1fa0f57c7b0a9df8f70f18 Mon Sep 17 00:00:00 2001 From: Mark Johnston Date: Fri, 8 Mar 2019 12:45:26 -0500 Subject: pkg/build: reduce cyclomatic complexity of getBuilder() --- pkg/build/build.go | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/pkg/build/build.go b/pkg/build/build.go index f8a20aabe..620ac5e07 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -63,22 +63,30 @@ type builder interface { } func getBuilder(targetOS, targetArch, vmType string) (builder, error) { - switch { - case targetOS == "linux" && targetArch == "amd64" && vmType == "gvisor": - return gvisor{}, nil - case targetOS == "linux" && targetArch == "amd64" && (vmType == "qemu" || vmType == "gce"): - return linux{}, nil - case targetOS == "fuchsia" && (targetArch == "amd64" || targetArch == "arm64") && vmType == "qemu": - return fuchsia{}, nil - case targetOS == "akaros" && targetArch == "amd64" && vmType == "qemu": - return akaros{}, nil - case targetOS == "openbsd" && targetArch == "amd64" && (vmType == "gce" || vmType == "vmm"): - return openbsd{}, nil - case targetOS == "netbsd" && targetArch == "amd64" && (vmType == "qemu" || vmType == "gce"): - return netbsd{}, nil - default: - return nil, fmt.Errorf("unsupported image type %v/%v/%v", targetOS, targetArch, vmType) + var supported = []struct { + OS string + arch string + vms []string + b builder + }{ + {"linux", "amd64", []string{"gvisor"}, gvisor{}}, + {"linux", "amd64", []string{"gce", "qemu"}, linux{}}, + {"fuchsia", "amd64", []string{"gce"}, fuchsia{}}, + {"fuchsia", "arm64", []string{"gce"}, fuchsia{}}, + {"akaros", "amd64", []string{"qemu"}, akaros{}}, + {"openbsd", "amd64", []string{"gce", "vmm"}, openbsd{}}, + {"netbsd", "amd64", []string{"gce", "qemu"}, netbsd{}}, } + for _, s := range supported { + if targetOS == s.OS && targetArch == s.arch { + for _, vm := range s.vms { + if vmType == vm { + return s.b, nil + } + } + } + } + return nil, fmt.Errorf("unsupported image type %v/%v/%v", targetOS, targetArch, vmType) } func CompilerIdentity(compiler string) (string, error) { -- cgit mrf-deployment