From 26ddf3fa5d7f54a0e1c01a935993e370917f45aa Mon Sep 17 00:00:00 2001 From: dungnh139 Date: Tue, 22 Oct 2024 06:32:35 +0700 Subject: [PATCH] bamboo firewall cli --- .gitignore | 1 + Makefile | 36 +++++++++++++-------------- api/v1/dto/gnp.go | 4 +-- build/build.sh | 8 ++++++ build/clean.sh | 12 +++++++++ build/golang.sh | 41 +++++++++++++++++++++++++++++++ build/init.sh | 23 +++++++++++++++++ cmd/bamboofwcli/command/create.go | 2 +- cmd/bamboofwcli/command/delete.go | 2 +- cmd/bamboofwcli/command/root.go | 2 +- 10 files changed, 107 insertions(+), 24 deletions(-) create mode 100755 build/build.sh create mode 100755 build/clean.sh create mode 100755 build/golang.sh create mode 100755 build/init.sh diff --git a/.gitignore b/.gitignore index 64ae7f3..8051283 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ *.dll *.so *.dylib +_output/ # Test binary, built with `go test -c` *.test diff --git a/Makefile b/Makefile index 5b5dd6a..cd81d8e 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,24 @@ -PACKAGE_NAME = github.com/bamboo-firewall/be +SERVER_DIR = ./cmd/server +SERVER_BIN_NAME = bamboo-apiserver +CLI_DIR = ./cmd/bamboofwcli +CLI_BIN_NAME = bbfw -VERSION ?= $(shell git describe --abbrev=0 --tags) -BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD) -BUILD_TIME ?= $(shell date +%Y-%m-%dT%H:%M:%S%z) +.PHONY: all-platform +all-platform: + build/build.sh $(SERVER_DIR) $(SERVER_BIN_NAME) all + build/build.sh $(CLI_DIR) $(CLI_BIN_NAME) all -ORGANIZATION = ATAOCloud +.PHONY: all +all: build-server build-bbfw -LDFLAGS = -s -w -X $(PACKAGE_NAME)/buildinfo.Version=$(VERSION) \ - -X $(PACKAGE_NAME)/buildinfo.GitBranch=$(BRANCH) \ - -X $(PACKAGE_NAME)/buildinfo.BuildDate=$(BUILD_TIME) \ - -X $(PACKAGE_NAME)/buildinfo.Organization=$(ORGANIZATION) +.PHONY: build-server +build-server: + build/build.sh $(SERVER_DIR) $(SERVER_BIN_NAME) -GOOS ?= linux -GOARCH ?= amd64 -CGO_ENABLED ?= 0 - -BUILD := go build -buildvcs=false -a -installsuffix cgo - -.PHONY: build -build-bbfwcli: - CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) $(BUILD) -ldflags="$(LDFLAGS)" -o bbfwcli ./cmd/bamboofwcli +.PHONY: build-bbfw +build-bbfw: + build/build.sh $(CLI_DIR) $(CLI_BIN_NAME) .PHONY: clean clean: - rm -f bbfwcli \ No newline at end of file + build/clean.sh diff --git a/api/v1/dto/gnp.go b/api/v1/dto/gnp.go index 8e83f33..3cd48f4 100644 --- a/api/v1/dto/gnp.go +++ b/api/v1/dto/gnp.go @@ -9,8 +9,8 @@ type GlobalNetworkPolicy struct { Metadata GNPMetadata `json:"metadata,omitempty" yaml:"metadata,omitempty"` Spec GNPSpec `json:"spec" yaml:"spec"` Description string `json:"description,omitempty" yaml:"description"` - CreatedAt time.Time `json:"createdAt" yaml:"createdAt" yaml:"createdAt"` - UpdatedAt time.Time `json:"updatedAt" yaml:"updatedAt" yaml:"updatedAt"` + CreatedAt time.Time `json:"createdAt" yaml:"createdAt"` + UpdatedAt time.Time `json:"updatedAt" yaml:"updatedAt"` } type GNPMetadata struct { diff --git a/build/build.sh b/build/build.sh new file mode 100755 index 0000000..d25f8f5 --- /dev/null +++ b/build/build.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +ROOT_PATH="$(cd "$(dirname "$0")/.." && pwd -P)" +BUILD_PATH="${ROOT_PATH}/build" + +source "${BUILD_PATH}/init.sh" + +golang::build_binaries "$@" diff --git a/build/clean.sh b/build/clean.sh new file mode 100755 index 0000000..7fd61f3 --- /dev/null +++ b/build/clean.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +ROOT_PATH="$(cd "$(dirname "$0")/.." && pwd -P)" +BUILD_PATH="${ROOT_PATH}/build" + +source "${BUILD_PATH}/init.sh" + +cleanup() { + rm -r ${BUILD_OUTPUT_PATH} +} + +cleanup diff --git a/build/golang.sh b/build/golang.sh new file mode 100755 index 0000000..2b736db --- /dev/null +++ b/build/golang.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +readonly SUPPORTED_PLATFORMS=( + linux/amd64 + linux/arm64 + darwin/amd64 + darwin/arm64 + windows/amd64 + windows/arm64 +) + +golang::build_binaries() { + local -a platforms + if [[ "$3" == "all" ]]; then + platforms=("${SUPPORTED_PLATFORMS[@]}") + else + local host_platform + host_platform=$(golang::host_platform) + platforms+=("${host_platform}") + fi + + for platform in "${platforms[@]}"; do + golang::build_binary_for_platform ${platform} $1 $2 + done +} + +golang::build_binary_for_platform() { + local platform="$1" + local build_dir="$2" + local bin_name="$3" + + GOOS=${platform%%/*} + GOARCH=${platform##*/} + output="${BUILD_CMD_PATH}/${GOOS}/${GOARCH}/${bin_name}" + + CGO_ENABLED=${CGO_ENABLED} GOOS=${GOOS} GOARCH=${GOARCH} ${GO_BUILD} -ldflags="${LDFLAGS}" -o ${output} ${build_dir} +} + +golang::host_platform() { + echo "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" +} diff --git a/build/init.sh b/build/init.sh new file mode 100755 index 0000000..cd5dea6 --- /dev/null +++ b/build/init.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +BUILD_OUTPUT_PATH="${ROOT_PATH}/_output" +BUILD_CMD_PATH="${BUILD_OUTPUT_PATH}/bin" + +PACKAGE_NAME="github.com/bamboo-firewall/be" + +VERSION="$(git describe --abbrev=0 --tags)" +BRANCH="$(git rev-parse --abbrev-ref HEAD)" +BUILD_TIME="$(date +%Y-%m-%dT%H:%M:%S%z)" + +ORGANIZATION="ATAOCloud" + +LDFLAGS="-s -w -X ${PACKAGE_NAME}/buildinfo.Version=${VERSION} \ + -X ${PACKAGE_NAME}/buildinfo.GitBranch=${BRANCH} \ + -X ${PACKAGE_NAME}/buildinfo.BuildDate=${BUILD_TIME} \ + -X ${PACKAGE_NAME}/buildinfo.Organization=${ORGANIZATION}" + +CGO_ENABLED=0 + +GO_BUILD="go build -buildvcs=false -a -installsuffix cgo" + +source "${BUILD_PATH}/golang.sh" diff --git a/cmd/bamboofwcli/command/create.go b/cmd/bamboofwcli/command/create.go index 5374fbc..02868a1 100644 --- a/cmd/bamboofwcli/command/create.go +++ b/cmd/bamboofwcli/command/create.go @@ -77,6 +77,6 @@ func create(cmd *cobra.Command, args []string) error { } } - fmt.Printf("Total: %d resources. Success: %d. Fail: %d.", len(resources), numHandled, len(resources)-numHandled) + fmt.Printf("Total: %d resources. Success: %d. Fail: %d.\n", len(resources), numHandled, len(resources)-numHandled) return nil } diff --git a/cmd/bamboofwcli/command/delete.go b/cmd/bamboofwcli/command/delete.go index e10dd03..92ab660 100644 --- a/cmd/bamboofwcli/command/delete.go +++ b/cmd/bamboofwcli/command/delete.go @@ -125,6 +125,6 @@ func deleteResources(cmd *cobra.Command, args []string) error { } } - fmt.Printf("Total: %d resources. Success: %d. Fail: %d", len(resources), numHandled, len(resources)-numHandled) + fmt.Printf("Total: %d resources. Success: %d. Fail: %d.\n", len(resources), numHandled, len(resources)-numHandled) return nil } diff --git a/cmd/bamboofwcli/command/root.go b/cmd/bamboofwcli/command/root.go index 308551b..1ef336d 100644 --- a/cmd/bamboofwcli/command/root.go +++ b/cmd/bamboofwcli/command/root.go @@ -8,7 +8,7 @@ import ( ) const ( - binaryName = "bbfwcli" + binaryName = "bbfw" ) var rootCMD = &cobra.Command{