Skip to content

Commit

Permalink
feat: add ldflags for version injection during artifacts build
Browse files Browse the repository at this point in the history
fix: unit test that were failing due to missing config
  • Loading branch information
grinish21 committed Sep 7, 2022
1 parent 549081d commit 32fc145
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 28 deletions.
52 changes: 30 additions & 22 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,37 @@
# permissions and limitations under the License.
#

echo "Running Unit Tests"
tmpfile=/tmp/eb-tmp.log
if SCRIPT_OUTPUT=$(local=true go test ./pkg/... > ${tmpfile}); then
cat ${tmpfile}

echo "Running Go FMT"
go fmt ./pkg/...

mkdir -p binaries

echo "Building Linux binary"
env GOOS=linux GOARCH=amd64 go build -o binaries/go-earlybird-linux

echo "Building Windows binary"
env GOOS=windows GOARCH=amd64 go build -o binaries/go-earlybird.exe
if [ -z "$1" ]
then
echo "Earlybird version is empty. Setting to default 'dev'"
version='dev'
else
echo "Earlybird version being build $1"
version="$1"
fi

echo "building MacOS binary"
env GOOS=darwin GOARCH=amd64 go build -o binaries/go-earlybird
echo "Running Unit Tests and Building binaries... version: $version"
echo "-----------------------------------------------------------------"
echo
echo "Tidy and Download modules ..."
go mod tidy && go mod download -x

echo "Cross-Compilation Complete"
else
echo "Unit Tests FAILED!"
cat ${tmpfile}
echo "Running Unit Tests..."
go test -p 10 ./pkg/** -covermode=count
testOutput=$? #get the return value of the last executed command
#exit with non-Zero to stop the build if unit tests fail
if [ $testOutput -ne 0 ]; then
echo "EXIT... Test Failed" >&2
exit $testOutput
fi

rm $tmpfile
echo "Building Linux binary"
env GOOS=linux GOARCH=amd64 go build -ldflags="-X 'github.com/americanexpress/earlybird/pkg/buildflags.Version=$version'" -o binaries/go-earlybird-linux
echo "Building Linux binary - Completed!!!"
echo "Building Windows binary"
env GOOS=windows GOARCH=amd64 go build -ldflags="-X 'github.com/americanexpress/earlybird/pkg/buildflags.Version=$version'" -o binaries/go-earlybird.exe
echo "Building Windows binary - Completed!!!"
echo "Building MacOS binary"
env GOOS=darwin GOARCH=amd64 go build -ldflags="-X 'github.com/americanexpress/earlybird/pkg/buildflags.Version=$version'" -o binaries/go-earlybird
echo "Building MacOS binary - Completed!!!"
echo "Build Completed ..."
2 changes: 1 addition & 1 deletion config/earlybird.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"display_threshold_level": 3,
"display_confidence_threshold_level": 2,
"earlybird_config_url": "https://raw.githubusercontent.com/americanexpress/earlybird/main/config/earlybird.json",
"version": "2.0.0",
"version": "3",
"name": "earlybird"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/americanexpress/earlybird

go 1.12
go 1.18

require (
code.sajari.com/docconv v1.2.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/fatih/set v0.2.1 h1:nn2CaJyknWE/6txyUDGwysr3G5QC6xWB/PtVjPBbeaA=
github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0CI=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -87,7 +86,6 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/olekukonko/tablewriter v0.0.0-20180506121414-d4647c9c7a84/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95 h1:+OLn68pqasWca0z5ryit9KGfp3sUsW4Lqg32iRMJyzs=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/gosseract/v2 v2.2.4 h1:h/PV+oJqke8q2Ccw9bjpMBWfd7N2vtGDCUcihZj3nRo=
github.com/otiai10/gosseract/v2 v2.2.4/go.mod h1:ahOp/kHojnOMGv1RaUnR0jwY5JVa6BYKhYAS8nbMLSo=
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var cfg = cfgreader.EarlybirdConfig{
MaxFileSize: 10240000,
WorkLength: 2500,
EnabledModulesMap: map[string]string{"content": "content.yaml", "password-secret": "password-secret.yaml"},
WorkerCount: 100,
}

func init() {
Expand Down
4 changes: 4 additions & 0 deletions pkg/buildflags/buildFlags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package buildflags

// Version the default value would be dev and this would be injected via ldflags
var Version = "dev"
4 changes: 3 additions & 1 deletion pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bufio"
"flag"
"fmt"
"github.com/americanexpress/earlybird/pkg/buildflags"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -164,6 +165,8 @@ func (eb *EarlybirdCfg) GetRuleModulesMap() (err error) {

//ConfigInit loads in the earlybird configuration and CLI flags
func (eb *EarlybirdCfg) ConfigInit() {
// Set the version from ldflags
eb.Config.Version = buildflags.Version
//Load CLI arguments and parse
flag.Var(&enableFlags, "enable", "Enable individual scanning modules "+utils.GetDisplayList(eb.Config.AvailableModules))
flag.Parse()
Expand Down Expand Up @@ -214,7 +217,6 @@ func (eb *EarlybirdCfg) ConfigInit() {
doUpdate(eb.Config.ConfigDir, eb.Config.RulesConfigDir, earlybirdConfigPath, cfgreader.Settings.ConfigFileURL, eb.Config.RuleModulesFilenameMap)
}

eb.Config.Version = cfgreader.Settings.Version
// Set the skip options (what not to scan) from configs
eb.Config.AnnotationsToSkipLine = cfgreader.Settings.AnnotationsToSkip
eb.Config.ExtensionsToSkipScan = cfgreader.Settings.ExtensionsToSkipTextScan
Expand Down
3 changes: 2 additions & 1 deletion pkg/scan/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var cfg = cfgReader.EarlybirdConfig{
"low": 4,
"medium": 3,
},
WorkerCount: 100,
}

func init() {
Expand Down Expand Up @@ -93,7 +94,7 @@ func TestScanFiles(t *testing.T) {
cfg *cfgReader.EarlybirdConfig
files []File
compressPaths []string
convertPaths []string
convertPaths []string
wantCode int
}
tests := []struct {
Expand Down

0 comments on commit 32fc145

Please sign in to comment.