diff --git a/.gitignore b/.gitignore index 3b03c62..3742c49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .project +.vscode build run diff --git a/Makefile b/Makefile index 13d3a03..f8dd072 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ else endif GOBIN ?= $(shell go env GOPATH)/bin -GOMAKE ?= github.com/tkrop/go-make@v0.0.24 +GOMAKE ?= github.com/tkrop/go-make@v0.0.25 TARGETS := $(shell command -v go-make >/dev/null || \ go install $(GOMAKE) && go-make targets) diff --git a/Makefile.base b/Makefile.base index 05419cc..0309730 100644 --- a/Makefile.base +++ b/Makefile.base @@ -64,7 +64,7 @@ TOOLS_GO := $(TOOLS_GO) \ github.com/icholy/gomajor \ github.com/golang/mock/mockgen \ github.com/tkrop/go-testing/cmd/mock \ - github.com/tkrop/go-make@v0.0.24 + github.com/tkrop/go-make@v0.0.25 TOOLS_SH := $(TOOLS_SH) \ github.com/anchore/syft \ github.com/anchore/grype @@ -145,10 +145,10 @@ AWS_IMAGE ?= localstack/localstack:$(AWS_VERSION) # Setup codacy integration. CODACY ?= enabled -ifdef CDP_PULL_REQUEST_NUMBER - CODACY_CONTINUE ?= true -else +ifeq ($(IMAGE_VERSION),snapshot) CODACY_CONTINUE ?= false +else + CODACY_CONTINUE ?= true endif CODACY_PROVIDER ?= ghe CODACY_USER ?= $(GITORGNAME) @@ -163,7 +163,7 @@ CODACY_STATICCHECK_VERSION ?= 3.0.12 upper = $(shell echo "$(1)" | tr '[:lower:]' '[:upper:]') -# Default target list for all and cdp builds. +# Default target list for all and pipeline builds. TARGETS_ALL ?= init test lint build image TARGETS_INIT ?= init-hooks init-go $(if $(filter $(CODACY),enabled),init-codacy,) TARGETS_CLEAN ?= clean-build @@ -977,16 +977,16 @@ image:: $(if $(filter $(IMAGE_PUSH),never),,image-push) image-build:: $(TARGETS_IMAGE_BUILD) setup-image-file = \ - FILE="$*"; IMAGE=$(IMAGE); \ - PREFIX="$$(basename "$${FILE%$(FILE_CONTAINER)*}")"; \ - SUFFIX="$$(echo $${FILE\#*$(FILE_CONTAINER)} | sed "s/^[^[:alnum:]]*//")"; \ - INFIX="$${SUFFIX:-$${PREFIX}}"; \ - if [ -n "$${INFIX}" ]; then \ - IMAGE="$${IMAGE/:/-$${INFIX}:}"; \ - fi; + FILE="$*"; IMAGE=$(IMAGE); \ + PREFIX="$$(basename "$${FILE%$(FILE_CONTAINER)*}")"; \ + SUFFIX="$$(echo $${FILE\#*$(FILE_CONTAINER)} | sed "s/^[^[:alnum:]]*//")"; \ + INFIX="$${SUFFIX:-$${PREFIX}}"; \ + if [ -n "$${INFIX}" ]; then \ + IMAGE="$${IMAGE/:/-$${INFIX}:}"; \ + fi $(TARGETS_IMAGE_BUILD):: image-build/%: build-linux - @$(call setup-image-file $*) \ + @$(call setup-image-file $*); \ if [ "$(IMAGE_PUSH)" == "never" ]; then \ echo "We never build images, aborting [$${IMAGE}]."; exit 0; \ fi; \ @@ -1004,12 +1004,12 @@ $(TARGETS_IMAGE_BUILD):: image-build/%: build-linux #@ push contianer images - if setup and allowed. image-push:: $(TARGETS_IMAGE_PUSH) $(TARGETS_IMAGE_PUSH):: image-push/%: image-build/% - @$(call setup-image-file $*) \ + @$(call setup-image-file $*); VERSION="${IMAGE##*:}; \ if [ "$(IMAGE_PUSH)" == "never" ]; then \ echo "We never push images, aborting [$${IMAGE}]."; exit 0; \ - elif [ "$(IMAGE_VERSION)" == "snapshot" ]; then \ + elif [ "$${VERSION}" == "snapshot" ]; then \ echo "We never push snapshot images, aborting [$${IMAGE}]."; exit 0; \ - elif [ -n "$(CDP_PULL_REQUEST_NUMBER)" -a "$(IMAGE_PUSH)" != "pulls" ]; then \ + elif [[ "$${VERSION}" =~ "^pr-.*$$" ]] && [ "$(IMAGE_PUSH)" != "pulls" ]; then \ echo "We never push pull request images, aborting [$${IMAGE}]."; exit 0; \ fi; \ $(IMAGE_CMD) push $${IMAGE}; \ diff --git a/VERSION b/VERSION index b056f41..2678ff8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.24 +0.0.25 diff --git a/config/Makefile b/config/Makefile index 13d3a03..f8dd072 100644 --- a/config/Makefile +++ b/config/Makefile @@ -8,7 +8,7 @@ else endif GOBIN ?= $(shell go env GOPATH)/bin -GOMAKE ?= github.com/tkrop/go-make@v0.0.24 +GOMAKE ?= github.com/tkrop/go-make@v0.0.25 TARGETS := $(shell command -v go-make >/dev/null || \ go install $(GOMAKE) && go-make targets) diff --git a/config/Makefile.base b/config/Makefile.base index 05419cc..0309730 100644 --- a/config/Makefile.base +++ b/config/Makefile.base @@ -64,7 +64,7 @@ TOOLS_GO := $(TOOLS_GO) \ github.com/icholy/gomajor \ github.com/golang/mock/mockgen \ github.com/tkrop/go-testing/cmd/mock \ - github.com/tkrop/go-make@v0.0.24 + github.com/tkrop/go-make@v0.0.25 TOOLS_SH := $(TOOLS_SH) \ github.com/anchore/syft \ github.com/anchore/grype @@ -145,10 +145,10 @@ AWS_IMAGE ?= localstack/localstack:$(AWS_VERSION) # Setup codacy integration. CODACY ?= enabled -ifdef CDP_PULL_REQUEST_NUMBER - CODACY_CONTINUE ?= true -else +ifeq ($(IMAGE_VERSION),snapshot) CODACY_CONTINUE ?= false +else + CODACY_CONTINUE ?= true endif CODACY_PROVIDER ?= ghe CODACY_USER ?= $(GITORGNAME) @@ -163,7 +163,7 @@ CODACY_STATICCHECK_VERSION ?= 3.0.12 upper = $(shell echo "$(1)" | tr '[:lower:]' '[:upper:]') -# Default target list for all and cdp builds. +# Default target list for all and pipeline builds. TARGETS_ALL ?= init test lint build image TARGETS_INIT ?= init-hooks init-go $(if $(filter $(CODACY),enabled),init-codacy,) TARGETS_CLEAN ?= clean-build @@ -977,16 +977,16 @@ image:: $(if $(filter $(IMAGE_PUSH),never),,image-push) image-build:: $(TARGETS_IMAGE_BUILD) setup-image-file = \ - FILE="$*"; IMAGE=$(IMAGE); \ - PREFIX="$$(basename "$${FILE%$(FILE_CONTAINER)*}")"; \ - SUFFIX="$$(echo $${FILE\#*$(FILE_CONTAINER)} | sed "s/^[^[:alnum:]]*//")"; \ - INFIX="$${SUFFIX:-$${PREFIX}}"; \ - if [ -n "$${INFIX}" ]; then \ - IMAGE="$${IMAGE/:/-$${INFIX}:}"; \ - fi; + FILE="$*"; IMAGE=$(IMAGE); \ + PREFIX="$$(basename "$${FILE%$(FILE_CONTAINER)*}")"; \ + SUFFIX="$$(echo $${FILE\#*$(FILE_CONTAINER)} | sed "s/^[^[:alnum:]]*//")"; \ + INFIX="$${SUFFIX:-$${PREFIX}}"; \ + if [ -n "$${INFIX}" ]; then \ + IMAGE="$${IMAGE/:/-$${INFIX}:}"; \ + fi $(TARGETS_IMAGE_BUILD):: image-build/%: build-linux - @$(call setup-image-file $*) \ + @$(call setup-image-file $*); \ if [ "$(IMAGE_PUSH)" == "never" ]; then \ echo "We never build images, aborting [$${IMAGE}]."; exit 0; \ fi; \ @@ -1004,12 +1004,12 @@ $(TARGETS_IMAGE_BUILD):: image-build/%: build-linux #@ push contianer images - if setup and allowed. image-push:: $(TARGETS_IMAGE_PUSH) $(TARGETS_IMAGE_PUSH):: image-push/%: image-build/% - @$(call setup-image-file $*) \ + @$(call setup-image-file $*); VERSION="${IMAGE##*:}; \ if [ "$(IMAGE_PUSH)" == "never" ]; then \ echo "We never push images, aborting [$${IMAGE}]."; exit 0; \ - elif [ "$(IMAGE_VERSION)" == "snapshot" ]; then \ + elif [ "$${VERSION}" == "snapshot" ]; then \ echo "We never push snapshot images, aborting [$${IMAGE}]."; exit 0; \ - elif [ -n "$(CDP_PULL_REQUEST_NUMBER)" -a "$(IMAGE_PUSH)" != "pulls" ]; then \ + elif [[ "$${VERSION}" =~ "^pr-.*$$" ]] && [ "$(IMAGE_PUSH)" != "pulls" ]; then \ echo "We never push pull request images, aborting [$${IMAGE}]."; exit 0; \ fi; \ $(IMAGE_CMD) push $${IMAGE}; \ diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 35a5ddd..eba1339 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -31,7 +31,7 @@ func (*defaultExecutor) Exec( //#nosec G204 -- caller ensures safe commands cmd := exec.Command(args[0], args[1:]...) cmd.Dir, cmd.Env = dir, os.Environ() - cmd.Env = append(cmd.Env, "MAKE=go-make") + cmd.Env = append(cmd.Env, "MAKE=make") cmd.Stdout, cmd.Stderr = stdout, stderr return cmd.Run() //nolint:wrapcheck // checked on next layer diff --git a/internal/make/make.go b/internal/make/make.go index b765108..61a7d66 100644 --- a/internal/make/make.go +++ b/internal/make/make.go @@ -95,7 +95,7 @@ var ( // Base command array for `git rev-parse HEAD` arguments. cmdGitHashHead = []string{"git", "rev-parse", "HEAD"} // Base command array for `git log --max-count=1 --format="%H"` arguments. - cmdGitHashNow = []string{"git", "log", "--max-count=1", "--format=\"%H\""} + cmdGitHashNow = []string{"git", "log", "--max-count=1", "--format=%H"} ) // CmdGitClone creates the argument array of a `git clone` command of the given @@ -145,12 +145,6 @@ func CmdMakeTargets(file string, args ...string) []string { type GoMake struct { // Info provides the build information of go-make. Info *info.Info - // The directory of the go-make command. - MakeDir string - // The actual working directory. - WorkDir string - // The path to the go-make command Makefile. - Makefile string // Executor provides the command executor. Executor cmd.Executor // Logger provides the logger. @@ -159,6 +153,13 @@ type GoMake struct { Stdout io.Writer // Stderr provides the standard error writer. Stderr io.Writer + + // The actual working directory. + WorkDir string + // The directory of the go-make command. + MakeDir string + // The path to the go-make command Makefile. + Makefile string // Trace provides the flags to trace commands. Trace bool } @@ -168,16 +169,8 @@ type GoMake struct { func NewGoMake( stdout, stderr io.Writer, info *info.Info, ) *GoMake { - //revive:disable-next-line:redefines-builtin-id // Is package name. - make, _ := os.Executable() //nolint:predeclared // Is package name. - makeDir := make + ".config" - workdir, _ := os.Getwd() - return &GoMake{ Info: info, - MakeDir: makeDir, - WorkDir: workdir, - Makefile: filepath.Join(makeDir, Makefile), Executor: cmd.NewExecutor(), Logger: log.NewLogger(), Stdout: stdout, @@ -318,20 +311,46 @@ func (gm *GoMake) exec( return nil } +func (gm *GoMake) Setup(version string) *GoMake { + if version == "" { + if gm.Info != nil && gm.Info.Version == gm.Info.Revision { + version = gm.Info.Version + // } else { + // version = "latest" + } + } + + // ---revive:disable-next-line:redefines-builtin-id // Is package name. + gm.WorkDir, _ = os.Getwd() + if version != "" { + gm.MakeDir = filepath.Join(os.Getenv("GOPATH"), + "pkg", "mod", gm.Info.Path+"@"+version, "config") + } else { + path, _ := os.Executable() + gm.MakeDir = path + ".config" + } + gm.Makefile = filepath.Join(gm.MakeDir, Makefile) + + return gm +} + // Make runs the go-make command with given arguments and return exit code. func (gm *GoMake) Make(args ...string) (int, error) { for _, arg := range args { - switch arg { - case "--trace": + switch { + case arg == "--trace": gm.Logger.Call(gm.Stderr, args...) gm.Logger.Info(gm.Stderr, gm.Info, false) gm.Trace = true - case "--version": + case strings.HasPrefix(arg, "--version="): + gm.Setup(arg[10:]) + + case arg == "--version": gm.Logger.Info(gm.Stdout, gm.Info, true) return 0, nil - case "--completion=bash": + case strings.HasPrefix(arg, "--completion"): gm.Logger.Message(gm.Stdout, BashCompletion) return 0, nil } @@ -381,7 +400,7 @@ func Make( // output that creates hard to validate output. // NewMakeFilter(stdout), NewMakeFilter(stderr), info, stdout, stderr, info, - ).Make(args[1:]...) + ).Setup("").Make(args[1:]...) return exit } diff --git a/internal/make/make_test.go b/internal/make/make_test.go index de9b883..51e3d32 100644 --- a/internal/make/make_test.go +++ b/internal/make/make_test.go @@ -60,8 +60,8 @@ var ( // infoTag with version and revision hash from git. infoTag = info.NewInfo(goMakePath, - "v1.1.1", - "v1.1.1", + "v0.0.24", + "v0.0.24", "2023-11-14T13:02:46+01:00", "2023-11-10T16:22:54+01:00", false) @@ -114,7 +114,7 @@ func GoMakeSetup( mocks.GetArg("stdout").(io.Writer), mocks.GetArg("stderr").(io.Writer), param.info, - ) + ).Setup("") if param.goMakeDir == "" { param.goMakeDir = goMakeDirExist