Skip to content

Commit f5db165

Browse files
authored
Build binaries for all platforms; add version command (#7)
1 parent 51143ce commit f5db165

File tree

9 files changed

+205
-12
lines changed

9 files changed

+205
-12
lines changed

.circleci/config.yml

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ executors:
33
docker:
44
- image: cimg/go:1.15
55

6+
runOnAllTags: &runOnAllTags
7+
filters:
8+
tags:
9+
only: /.*/
10+
611
version: 2.1
712

813
jobs:
@@ -31,15 +36,32 @@ jobs:
3136
command: |
3237
make test
3338
39+
build:
40+
executor: custom
41+
steps:
42+
- checkout
43+
44+
- run:
45+
name: Build binaries
46+
command: |
47+
make build
48+
49+
- run:
50+
name: Ensure `kube-linter version` returns the expected value.
51+
command: |
52+
version="$(.gobin/kube-linter version)"
53+
expected_version="$(./get-tag)"
54+
echo "Version from kube-linter: ${version}. Expected version: ${expected_version}"
55+
[[ "${version}" == "${expected_version}" ]]
56+
57+
3458
workflows:
3559
version: 2
3660
build:
3761
jobs:
3862
- lint:
39-
filters:
40-
tags:
41-
only: /.*/
63+
<<: *runOnAllTags
4264
- test:
43-
filters:
44-
tags:
45-
only: /.*/
65+
<<: *runOnAllTags
66+
- build:
67+
<<: *runOnAllTags

Makefile

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ deps: go.mod
1010
@go mod verify
1111
@touch deps
1212

13+
UNAME_S := $(shell uname -s)
14+
HOST_OS := linux
15+
ifeq ($(UNAME_S),Darwin)
16+
HOST_OS := darwin
17+
endif
1318

14-
#####################################################################
15-
###### Binaries we depend on ############
16-
#####################################################################
1719

1820
GOBIN := $(CURDIR)/.gobin
1921
PATH := $(GOBIN):$(PATH)
@@ -23,6 +25,10 @@ PATH := $(GOBIN):$(PATH)
2325
# See https://stackoverflow.com/a/36226784/3690207
2426
SHELL := env PATH=$(PATH) /bin/bash
2527

28+
########################################
29+
###### Binaries we depend on ###########
30+
########################################
31+
2632
GOLANGCILINT_BIN := $(GOBIN)/golangci-lint
2733
$(GOLANGCILINT_BIN): deps
2834
@echo "+ $@"
@@ -66,8 +72,8 @@ lint: golangci-lint staticcheck
6672

6773
.PHONY: generated-docs
6874
generated-docs: build
69-
./bin/kube-linter templates list --format markdown > docs/generated/templates.md
70-
./bin/kube-linter checks list --format markdown > docs/generated/checks.md
75+
kube-linter templates list --format markdown > docs/generated/templates.md
76+
kube-linter checks list --format markdown > docs/generated/checks.md
7177

7278
.PHONY: packr
7379
packr: $(PACKR_BIN)
@@ -80,7 +86,12 @@ packr: $(PACKR_BIN)
8086

8187
.PHONY: build
8288
build: packr
83-
go build -o ./bin/kube-linter ./cmd/kube-linter
89+
@CGO_ENABLED=0 GOOS=darwin scripts/go-build.sh ./cmd/kube-linter
90+
@CGO_ENABLED=0 GOOS=linux scripts/go-build.sh ./cmd/kube-linter
91+
@CGO_ENABLED=0 GOOS=windows scripts/go-build.sh ./cmd/kube-linter
92+
@mkdir -p "$(GOBIN)"
93+
@cp "bin/$(HOST_OS)/kube-linter" "$(GOBIN)/kube-linter"
94+
@chmod u+w "$(GOBIN)/kube-linter"
8495

8596
##########
8697
## Test ##

get-tag

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
if [ -n "${CIRCLE_TAG}" ]; then
4+
echo "${CIRCLE_TAG}"
5+
else
6+
git describe --long --tags --abbrev=10 --dirty
7+
fi

internal/command/root/command.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"golang.stackrox.io/kube-linter/internal/command/checks"
88
"golang.stackrox.io/kube-linter/internal/command/lint"
99
"golang.stackrox.io/kube-linter/internal/command/templates"
10+
"golang.stackrox.io/kube-linter/internal/command/version"
1011
)
1112

1213
// Command is the root command.
@@ -20,6 +21,7 @@ func Command() *cobra.Command {
2021
checks.Command(),
2122
lint.Command(),
2223
templates.Command(),
24+
version.Command(),
2325
)
2426
return c
2527
}

internal/command/version/command.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
"golang.stackrox.io/kube-linter/internal/version"
8+
)
9+
10+
// Command defines the version command
11+
func Command() *cobra.Command {
12+
c := &cobra.Command{
13+
Use: "version",
14+
Short: "print version and exit",
15+
Args: cobra.NoArgs,
16+
Run: func(cmd *cobra.Command, _ []string) {
17+
fmt.Println(version.Get())
18+
},
19+
}
20+
return c
21+
}

internal/version/version.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package version
2+
3+
import (
4+
"golang.stackrox.io/kube-linter/internal/stringutils"
5+
)
6+
7+
var (
8+
version string //XDef:VERSION
9+
)
10+
11+
// Get returns the version.
12+
func Get() string {
13+
return stringutils.OrDefault(version, "development")
14+
}

scripts/go-build.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
3+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
4+
source "${SCRIPT_DIR}/utils.sh"
5+
6+
set -eo pipefail
7+
8+
main_srcdir="$1"
9+
[[ -n "${main_srcdir}" ]] || die "Usage: $0 <directory with main file>"
10+
11+
x_defs=()
12+
x_def_errors=()
13+
14+
while read -r line || [[ -n "$line" ]]; do
15+
if [[ "$line" =~ ^[[:space:]]*$ ]]; then
16+
continue
17+
elif [[ "$line" =~ ^([^[:space:]]+)[[:space:]]+(.*)[[:space:]]*$ ]]; then
18+
var="${BASH_REMATCH[1]}"
19+
def="${BASH_REMATCH[2]}"
20+
eval "stamp_${var}=$(printf '%q' "$def")"
21+
else
22+
die "Malformed variable_stamps.sh output line ${line}"
23+
fi
24+
done < <("${SCRIPT_DIR}/variable_stamps.sh")
25+
26+
while read -r line || [[ -n "$line" ]]; do
27+
if [[ "$line" =~ ^[[:space:]]*$ ]]; then
28+
continue
29+
elif [[ "$line" =~ ^([^:]+):([[:digit:]]+):[[:space:]]*(var[[:space:]]+)?([^[:space:]]+)[[:space:]].*//XDef:([^[:space:]]+)[[:space:]]*$ ]]; then
30+
go_file="${BASH_REMATCH[1]}"
31+
go_line="${BASH_REMATCH[2]}"
32+
go_var="${BASH_REMATCH[4]}"
33+
stamp_var="${BASH_REMATCH[5]}"
34+
35+
varname="stamp_${stamp_var}"
36+
[[ -n "${!varname}" ]] || x_def_errors+=(
37+
"Variable ${go_var} defined in ${go_file}:${go_line} references status var ${stamp_var} that is not part of the variable_stamps.sh output"
38+
)
39+
go_package="$(cd "${SCRIPT_DIR}/.."; go list -e "./$(dirname "$go_file")")"
40+
41+
x_defs+=(-X "\"${go_package}.${go_var}=${!varname}\"")
42+
fi
43+
done < <(git -C "${SCRIPT_DIR}/.." grep -n '//XDef:' -- '*.go')
44+
if [[ "${#x_def_errors[@]}" -gt 0 ]]; then
45+
printf >&2 "%s\n" "${x_def_errors[@]}"
46+
exit 1
47+
fi
48+
49+
ldflags=(-s -w "${x_defs[@]}")
50+
51+
[[ -n "${GOOS}" ]] || die "GOOS must be set"
52+
bin_name="$(basename "$main_srcdir")"
53+
output_file="bin/${GOOS}/${bin_name}"
54+
if [[ "$GOOS" == "windows" ]]; then
55+
output_file="${output_file}.exe"
56+
fi
57+
mkdir -p "$(dirname "$output_file")"
58+
echo >&2 "Compiling Go source in ${main_srcdir} to ${output_file}"
59+
go build -ldflags="${ldflags[*]}" -o "${output_file}" "${main_srcdir}"

scripts/utils.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
function safe_tput() {
4+
tput "$@" 2>/dev/null || true
5+
}
6+
7+
bold="$(safe_tput bold)"
8+
reset="$(safe_tput sgr0)"
9+
green="$(safe_tput setaf 2)"
10+
yellow="$(safe_tput setaf 3)"
11+
red="$(safe_tput setaf 1)"
12+
black="$(safe_tput setaf 0; safe_tput setab 7)"
13+
14+
function eecho() {
15+
echo >&2 "$@"
16+
}
17+
18+
function einfo() {
19+
eecho -en "${bold}${green}[INFO]${black} "
20+
eecho -n "$@"
21+
eecho -e "$reset"
22+
}
23+
24+
function ewarn() {
25+
eecho -en "${bold}${yellow}[WARN]${black} "
26+
eecho -n "$@"
27+
eecho -e "$reset"
28+
}
29+
30+
function eerror() {
31+
eecho -en "${bold}${red}[ERROR]${black} "
32+
eecho -n "$@"
33+
eecho -e "$reset"
34+
}
35+
36+
function efatal() {
37+
eecho -en "${bold}${red}[FATAL]${black} "
38+
eecho -n "$@"
39+
eecho -e "$reset"
40+
}
41+
42+
function die() {
43+
efatal "$@"
44+
exit 1
45+
}

scripts/variable_stamps.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
4+
source "${SCRIPT_DIR}/utils.sh"
5+
6+
set -euo pipefail
7+
8+
gitroot="$(git rev-parse --show-toplevel)"
9+
10+
[[ -n "${gitroot}" ]] || die "Could not determine git root"
11+
12+
echo "VERSION $("${gitroot}/get-tag")"

0 commit comments

Comments
 (0)