From 687ec01518d2f25a092d0c09cefb0f9bf03a4a0a Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong(cubxxw)" <3293172751nss@gmail.com> Date: Wed, 22 Mar 2023 14:40:37 +0800 Subject: [PATCH] style(pkg/common): improve code readability and maintainability Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> --- Makefile | 4 ++-- pkg/client/common.go | 10 +++------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 829337ddd..f3533d2f5 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ ifeq ($(OS),Windows_NT) SHELL:=cmd.exe # Need BLANK due to makefile parsing of `\` -# (see: ) +# (see: https://stackoverflow.com/questions/54733231/how-to-escape-a-backslash-in-the-end-to-mean-literal-backslash-in-makefile/54733416#54733416) BLANK:= # Define variable named `/` to represent OS path separator (usable as `$/` in this file) @@ -87,7 +87,7 @@ lint: install-golangci-lint ## test: Run unit and acceptance tests test: unit acceptance -## unit: Run unit tests +## unit: Append coverage arguments ifeq ($(TEST_COVERAGE), 1) unit: GOTESTFLAGS:=$(GOTESTFLAGS) -coverprofile=./out/tests/coverage-unit.txt -covermode=atomic endif diff --git a/pkg/client/common.go b/pkg/client/common.go index b6e0e8ec2..b526edd95 100644 --- a/pkg/client/common.go +++ b/pkg/client/common.go @@ -18,7 +18,7 @@ func (c *Client) parseTagReference(imageName string) (name.Reference, error) { return nil, errors.New("image is a required parameter") } if _, err := name.ParseReference(imageName, name.WeakValidation); err != nil { - return nil, err + return nil, fmt.Errorf("'%s' is not a valid tag reference: %s", imageName, err) } ref, err := name.NewTag(imageName, name.WeakValidation) if err != nil { @@ -108,17 +108,13 @@ func contains(slc []string, v string) bool { } func getBestRunMirror(registry string, runImage string, mirrors []string, preferredMirrors []string) string { - var runImageList []string - runImageList = append(runImageList, preferredMirrors...) - runImageList = append(runImageList, runImage) - runImageList = append(runImageList, mirrors...) - + runImageList := append(append(append([]string{}, preferredMirrors...), runImage), mirrors...) for _, img := range runImageList { ref, err := name.ParseReference(img, name.WeakValidation) if err != nil { continue } - if ref.Context().RegistryStr() == registry { + if reg := ref.Context().RegistryStr(); reg == registry { return img } }