Skip to content

Commit

Permalink
bamboo firewall cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dungnh139 committed Oct 21, 2024
1 parent 5078ad5 commit 26ddf3f
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
_output/

# Test binary, built with `go test -c`
*.test
Expand Down
36 changes: 17 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
build/clean.sh
4 changes: 2 additions & 2 deletions api/v1/dto/gnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
12 changes: 12 additions & 0 deletions build/clean.sh
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions build/golang.sh
Original file line number Diff line number Diff line change
@@ -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)"
}
23 changes: 23 additions & 0 deletions build/init.sh
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion cmd/bamboofwcli/command/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cmd/bamboofwcli/command/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cmd/bamboofwcli/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

const (
binaryName = "bbfwcli"
binaryName = "bbfw"
)

var rootCMD = &cobra.Command{
Expand Down

0 comments on commit 26ddf3f

Please sign in to comment.