From 16375c4d6f9493ffabcd3aa9c02f42fef8ce4f6a Mon Sep 17 00:00:00 2001 From: Heathcliff Date: Mon, 23 Dec 2024 15:22:09 +0100 Subject: [PATCH] Prepare for building multiple output binaries Signed-off-by: Heathcliff --- Dockerfile | 2 +- Makefile | 9 ++++++--- cmd/fleetlock/main.go | 7 +++++++ cmd/main.go | 7 ------- hack/build.sh | 11 +++++++++-- pkg/{cmd => fleetlock}/root.go | 2 +- pkg/{cmd => fleetlock}/root_test.go | 2 +- 7 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 cmd/fleetlock/main.go delete mode 100644 cmd/main.go rename pkg/{cmd => fleetlock}/root.go (99%) rename pkg/{cmd => fleetlock}/root_test.go (90%) diff --git a/Dockerfile b/Dockerfile index ba6d0f0e..c5cc9190 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,7 +15,7 @@ COPY cmd ./cmd COPY pkg ./pkg COPY hack ./hack -RUN --mount=type=bind,target=/app/.git,source=.git GOOS=linux GOARCH="${TARGETARCH}" hack/build.sh +RUN --mount=type=bind,target=/app/.git,source=.git GOOS=linux GOARCH="${TARGETARCH}" hack/build.sh fleetlock # # END build-stage diff --git a/Makefile b/Makefile index 1e680df3..571f5a3e 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,13 @@ REPOSITORY ?= localhost CONTAINER_NAME ?= fleetlock TAG ?= latest -default: build +build: build-fleetctl build-fleetlock -build: - hack/build.sh +build-fleetctl: + hack/build.sh fleetctl + +build-fleetlock: + hack/build.sh fleetlock image: podman build -t $(REPOSITORY)/$(CONTAINER_NAME):$(TAG) . diff --git a/cmd/fleetlock/main.go b/cmd/fleetlock/main.go new file mode 100644 index 00000000..6c0094a4 --- /dev/null +++ b/cmd/fleetlock/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/heathcliff26/fleetlock/pkg/fleetlock" + +func main() { + fleetlock.Execute() +} diff --git a/cmd/main.go b/cmd/main.go deleted file mode 100644 index 778acac4..00000000 --- a/cmd/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "github.com/heathcliff26/fleetlock/pkg/cmd" - -func main() { - cmd.Execute() -} diff --git a/hack/build.sh b/hack/build.sh index 6828deb9..86725283 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -6,6 +6,8 @@ base_dir="$(dirname "${BASH_SOURCE[0]}" | xargs realpath)/.." bin_dir="${base_dir}/bin" +target="${1}" + GOOS="${GOOS:-$(go env GOOS)}" GOARCH="${GOARCH:-$(go env GOARCH)}" @@ -16,7 +18,12 @@ if [ "${RELEASE_VERSION}" != "" ]; then GO_LD_FLAGS+=" -X github.com/heathcliff26/fleetlock/pkg/version.version=${RELEASE_VERSION}" fi -output_name="${bin_dir}/fleetlock" +output_name="${bin_dir}/${target}" + +if [ "${2}" != "" ]; then + output_name="${bin_dir}/${2}" +fi + if [ "${GOOS}" == "windows" ]; then output_name="${output_name}.exe" fi @@ -24,4 +31,4 @@ fi pushd "${base_dir}" >/dev/null echo "Building $(basename "${output_name}")" -GOOS="${GOOS}" GOARCH="${GOARCH}" CGO_ENABLED=0 go build -ldflags="${GO_LD_FLAGS}" -o "${output_name}" ./cmd/... +GOOS="${GOOS}" GOARCH="${GOARCH}" CGO_ENABLED=0 go build -ldflags="${GO_LD_FLAGS}" -o "${output_name}" "./cmd/${target}" diff --git a/pkg/cmd/root.go b/pkg/fleetlock/root.go similarity index 99% rename from pkg/cmd/root.go rename to pkg/fleetlock/root.go index b9a614c8..7ff5b2c9 100644 --- a/pkg/cmd/root.go +++ b/pkg/fleetlock/root.go @@ -1,4 +1,4 @@ -package cmd +package fleetlock import ( "fmt" diff --git a/pkg/cmd/root_test.go b/pkg/fleetlock/root_test.go similarity index 90% rename from pkg/cmd/root_test.go rename to pkg/fleetlock/root_test.go index 9f6c0043..3e9bc3e5 100644 --- a/pkg/cmd/root_test.go +++ b/pkg/fleetlock/root_test.go @@ -1,4 +1,4 @@ -package cmd +package fleetlock import ( "testing"