Skip to content

Commit

Permalink
style(pkg/common): improve code readability and maintainability
Browse files Browse the repository at this point in the history
fix(makefile): Remove.DEFAULT_GOAL and.PHONY

fix(makefile): Refine the Makefile file so that it can do the basic work

Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com>
  • Loading branch information
cubxxw committed Mar 22, 2023
1 parent b7f1563 commit 192ed61
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
24 changes: 19 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ifeq ($(OS),Windows_NT)
SHELL:=cmd.exe

# Need BLANK due to makefile parsing of `\\`
# (see: <https://stackoverflow.com/questions/54733231/how-to-escape-a-backslash-in-the-end-to-mean-literal-backslash-in-makefile/54733416#54733416>)
# Need BLANK due to makefile parsing of `\`
# (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)
Expand Down Expand Up @@ -37,6 +37,14 @@ NO_DOCKER?=
clean_build := $(strip ${PACK_BUILD})
clean_sha := $(strip ${PACK_GITSHA1})

# append coverage arguments
ifeq ($(TEST_COVERAGE), 1)
unit: GOTESTFLAGS:=$(GOTESTFLAGS) -coverprofile=./out/tests/coverage-unit.txt -covermode=atomic
endif
ifeq ($(NO_DOCKER),)
unit: GOTESTFLAGS:=$(GOTESTFLAGS) --tags=example
endif

# Append build number and git sha to version, if not-empty
ifneq ($(and $(clean_build),$(clean_sha)),)
PACK_VERSION:=${PACK_VERSION}+git-${clean_sha}.build-${clean_build}
Expand All @@ -54,7 +62,9 @@ BINDIR:=/usr/bin/
.DEFAULT_GOAL := build
.PHONY: clean build format imports lint test unit acceptance prepare-for-pr verify verify-format benchmark

## bUild: Build the program
# this target must be listed first in order for it to be a default target,
# so that ubuntu_ppa's may be constructed using default build tools.
## build: Build the program
build: out
@echo "=====> Building..."
$(GOCMD) build -ldflags "-s -w -X 'github.com/buildpacks/pack.Version=${PACK_VERSION}' -extldflags ${LDFLAGS}" -trimpath -o ./out/$(PACK_BIN) -a ./cmd/pack
Expand Down Expand Up @@ -126,15 +136,19 @@ verify-format: install-goimports
benchmark: out
@echo "=====> Running benchmarks"
$(GOCMD) test -run=^$ -bench=. -benchtime=1s -benchmem -memprofile=./out/bench_mem.out -cpuprofile=./out/bench_cpu.out -tags=benchmarks ./benchmarks/ -v
# NOTE: You can analyze the results, using go tool pprof. For instance, you can start a server to see a graph of the cpu usage by running
# go tool pprof -http=":8082" out/bench_cpu.out. Alternatively, you can run go tool pprof, and in the ensuing cli, run
# commands like top10 or web to dig down into the cpu and memory usage
# For more, see https://blog.golang.org/pprof

## package: Package the program
package: out
tar czf .$/out$/$(ARCHIVE_NAME).tgz -C .$/out$/ $(PACK_BIN)

## install: Install the program to the system
install:
mkdir -p ${DESTDIR}${BINDIR}
cp ./out/$(PACK_BIN) ${DESTDIR}${BINDIR}/
@mkdir -p ${DESTDIR}${BINDIR}
@cp ./out/$(PACK_BIN) ${DESTDIR}${BINDIR}/

## install-mockgen: Used only by apt-get install when installing ubuntu ppa
install-mockgen:
Expand Down
9 changes: 3 additions & 6 deletions pkg/client/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -108,17 +108,14 @@ 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
}
}
Expand Down

0 comments on commit 192ed61

Please sign in to comment.