Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build binaries for all platforms; add version command (#7)
- Loading branch information
1 parent
51143ce
commit f5db165
Showing
9 changed files
with
205 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/sh | ||
|
||
if [ -n "${CIRCLE_TAG}" ]; then | ||
echo "${CIRCLE_TAG}" | ||
else | ||
git describe --long --tags --abbrev=10 --dirty | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.stackrox.io/kube-linter/internal/version" | ||
) | ||
|
||
// Command defines the version command | ||
func Command() *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "version", | ||
Short: "print version and exit", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, _ []string) { | ||
fmt.Println(version.Get()) | ||
}, | ||
} | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package version | ||
|
||
import ( | ||
"golang.stackrox.io/kube-linter/internal/stringutils" | ||
) | ||
|
||
var ( | ||
version string //XDef:VERSION | ||
) | ||
|
||
// Get returns the version. | ||
func Get() string { | ||
return stringutils.OrDefault(version, "development") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env bash | ||
|
||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" | ||
source "${SCRIPT_DIR}/utils.sh" | ||
|
||
set -eo pipefail | ||
|
||
main_srcdir="$1" | ||
[[ -n "${main_srcdir}" ]] || die "Usage: $0 <directory with main file>" | ||
|
||
x_defs=() | ||
x_def_errors=() | ||
|
||
while read -r line || [[ -n "$line" ]]; do | ||
if [[ "$line" =~ ^[[:space:]]*$ ]]; then | ||
continue | ||
elif [[ "$line" =~ ^([^[:space:]]+)[[:space:]]+(.*)[[:space:]]*$ ]]; then | ||
var="${BASH_REMATCH[1]}" | ||
def="${BASH_REMATCH[2]}" | ||
eval "stamp_${var}=$(printf '%q' "$def")" | ||
else | ||
die "Malformed variable_stamps.sh output line ${line}" | ||
fi | ||
done < <("${SCRIPT_DIR}/variable_stamps.sh") | ||
|
||
while read -r line || [[ -n "$line" ]]; do | ||
if [[ "$line" =~ ^[[:space:]]*$ ]]; then | ||
continue | ||
elif [[ "$line" =~ ^([^:]+):([[:digit:]]+):[[:space:]]*(var[[:space:]]+)?([^[:space:]]+)[[:space:]].*//XDef:([^[:space:]]+)[[:space:]]*$ ]]; then | ||
go_file="${BASH_REMATCH[1]}" | ||
go_line="${BASH_REMATCH[2]}" | ||
go_var="${BASH_REMATCH[4]}" | ||
stamp_var="${BASH_REMATCH[5]}" | ||
|
||
varname="stamp_${stamp_var}" | ||
[[ -n "${!varname}" ]] || x_def_errors+=( | ||
"Variable ${go_var} defined in ${go_file}:${go_line} references status var ${stamp_var} that is not part of the variable_stamps.sh output" | ||
) | ||
go_package="$(cd "${SCRIPT_DIR}/.."; go list -e "./$(dirname "$go_file")")" | ||
|
||
x_defs+=(-X "\"${go_package}.${go_var}=${!varname}\"") | ||
fi | ||
done < <(git -C "${SCRIPT_DIR}/.." grep -n '//XDef:' -- '*.go') | ||
if [[ "${#x_def_errors[@]}" -gt 0 ]]; then | ||
printf >&2 "%s\n" "${x_def_errors[@]}" | ||
exit 1 | ||
fi | ||
|
||
ldflags=(-s -w "${x_defs[@]}") | ||
|
||
[[ -n "${GOOS}" ]] || die "GOOS must be set" | ||
bin_name="$(basename "$main_srcdir")" | ||
output_file="bin/${GOOS}/${bin_name}" | ||
if [[ "$GOOS" == "windows" ]]; then | ||
output_file="${output_file}.exe" | ||
fi | ||
mkdir -p "$(dirname "$output_file")" | ||
echo >&2 "Compiling Go source in ${main_srcdir} to ${output_file}" | ||
go build -ldflags="${ldflags[*]}" -o "${output_file}" "${main_srcdir}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env bash | ||
|
||
function safe_tput() { | ||
tput "$@" 2>/dev/null || true | ||
} | ||
|
||
bold="$(safe_tput bold)" | ||
reset="$(safe_tput sgr0)" | ||
green="$(safe_tput setaf 2)" | ||
yellow="$(safe_tput setaf 3)" | ||
red="$(safe_tput setaf 1)" | ||
black="$(safe_tput setaf 0; safe_tput setab 7)" | ||
|
||
function eecho() { | ||
echo >&2 "$@" | ||
} | ||
|
||
function einfo() { | ||
eecho -en "${bold}${green}[INFO]${black} " | ||
eecho -n "$@" | ||
eecho -e "$reset" | ||
} | ||
|
||
function ewarn() { | ||
eecho -en "${bold}${yellow}[WARN]${black} " | ||
eecho -n "$@" | ||
eecho -e "$reset" | ||
} | ||
|
||
function eerror() { | ||
eecho -en "${bold}${red}[ERROR]${black} " | ||
eecho -n "$@" | ||
eecho -e "$reset" | ||
} | ||
|
||
function efatal() { | ||
eecho -en "${bold}${red}[FATAL]${black} " | ||
eecho -n "$@" | ||
eecho -e "$reset" | ||
} | ||
|
||
function die() { | ||
efatal "$@" | ||
exit 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
|
||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" | ||
source "${SCRIPT_DIR}/utils.sh" | ||
|
||
set -euo pipefail | ||
|
||
gitroot="$(git rev-parse --show-toplevel)" | ||
|
||
[[ -n "${gitroot}" ]] || die "Could not determine git root" | ||
|
||
echo "VERSION $("${gitroot}/get-tag")" |