From 9d7b65446fe6f93cbc0bc5c832f1d34853261352 Mon Sep 17 00:00:00 2001 From: jregan Date: Tue, 24 Sep 2019 20:29:03 -0700 Subject: [PATCH] Make pre-commit more portable and less tricky. --- .travis.yml | 9 +-- go.mod | 7 +++ go.sum | 16 +++++ travis/pre-commit.sh | 147 ++++++++++++++++++++++++++++--------------- 4 files changed, 121 insertions(+), 58 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1e0f6776a..4892257dc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,19 +26,12 @@ go_import_path: sigs.k8s.io/kustomize before_install: - source ./travis/consider-early-travis-exit.sh - - curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(go env GOPATH)/bin v1.17.1 - - GO111MODULE=on go get sigs.k8s.io/kustomize/pluginator@v1 - - go get -u github.com/monopole/mdrip - # The following would install Helm if needed for some reason. - # - wget https://storage.googleapis.com/kubernetes-helm/helm-v2.13.1-linux-amd64.tar.gz - # - tar -xvzf helm-v2.13.1-linux-amd64.tar.gz - # - sudo mv linux-amd64/helm /usr/local/bin/helm # Skip the install process; let pre-commit.sh do it. install: true script: - - env GO111MODULE=on ./travis/pre-commit.sh + - ./travis/pre-commit.sh # TBD. Suppressing for now. notifications: diff --git a/go.mod b/go.mod index c404db817..0cb1a7554 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,14 @@ require ( github.com/evanphx/json-patch v4.5.0+incompatible github.com/go-openapi/spec v0.19.2 github.com/gogo/protobuf v1.3.0 // indirect + github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect + github.com/gorilla/mux v1.7.3 // indirect + github.com/gorilla/sessions v1.2.0 // indirect + github.com/gorilla/websocket v1.4.1 // indirect + github.com/monopole/mdrip v0.2.48 // indirect github.com/pkg/errors v0.8.1 + github.com/russross/blackfriday v2.0.0+incompatible // indirect + github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect github.com/spf13/cobra v0.0.2 github.com/spf13/pflag v1.0.3 gopkg.in/yaml.v2 v2.2.2 diff --git a/go.sum b/go.sum index 60dd0424e..c03c92eb6 100644 --- a/go.sum +++ b/go.sum @@ -34,6 +34,8 @@ github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/gogo/protobuf v1.3.0 h1:G8O7TerXerS4F6sx9OV7/nRfJdnXgHZu/S/7F2SN+UE= github.com/gogo/protobuf v1.3.0/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -45,6 +47,14 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/ github.com/googleapis/gnostic v0.0.0-20170426233943-68f4ded48ba9/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= github.com/googleapis/gnostic v0.3.0 h1:CcQijm0XKekKjP/YCz28LXVSpgguuB+nCxaSjCe09y0= github.com/googleapis/gnostic v0.3.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= +github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= +github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= +github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= +github.com/gorilla/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ= +github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= +github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= +github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= @@ -72,6 +82,8 @@ github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da h1:ZQGIPjr1iTtU github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/monopole/mdrip v0.2.48 h1:LVXlMzEyJOPouL5MYN9z61rMHwatLV1JZZSN+mmI6zI= +github.com/monopole/mdrip v0.2.48/go.mod h1:rzORfdNQ63T/tS95GOFHB+I3OrT+Bjlk8krOc/QiL/8= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -85,6 +97,10 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday v2.0.0+incompatible h1:cBXrhZNUf9C+La9/YpS+UHpUT8YD6Td9ZMSU9APFcsk= +github.com/russross/blackfriday v2.0.0+incompatible/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/spf13/cobra v0.0.2 h1:NfkwRbgViGoyjBKsLI0QMDcuMnhM+SBg3T0cGfpvKDE= github.com/spf13/cobra v0.0.2/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= diff --git a/travis/pre-commit.sh b/travis/pre-commit.sh index 195db86ec..2f86467af 100755 --- a/travis/pre-commit.sh +++ b/travis/pre-commit.sh @@ -1,15 +1,38 @@ #!/bin/bash set -e -# Make sure, we run in the root of the repo and -# therefore run the tests on all packages -base_dir="$( cd "$(dirname "$0")/.." && pwd )" -cd "$base_dir" || { - echo "Cannot cd to '$base_dir'. Aborting." >&2 - exit 1 +# Tracks success or failure of various operations. +# 0==success, any other value is a failure. +rcAccumulator=0 + +# Not used, and not cross platform, +# but kept because I don't want to have to +# look it up again. +function installHelm { + wget https://storage.googleapis.com/kubernetes-helm/helm-v2.13.1-linux-amd64.tar.gz + tar -xvzf helm-v2.13.1-linux-amd64.tar.gz + sudo mv linux-amd64/helm /usr/local/bin/helm } -rc=0 +function doInstall { + local tool=$1 + shift + if hash $tool 2>/dev/null; then + echo "$tool available." + else + echo "Installing ${tool}..." + eval $@ + fi +} + +function installTools { + doInstall mdrip \ + 'go get -u github.com/monopole/mdrip' + doInstall golangci-lint \ + 'curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.17.1' + doInstall pluginator \ + 'go install sigs.k8s.io/kustomize/pluginator' +} function runFunc { local name=$1 @@ -17,7 +40,7 @@ function runFunc { printf "============== begin %s\n" "$name" $name local code=$? - rc=$((rc || $code)) + rcAccumulator=$((rcAccumulator || $code)) if [ $code -ne 0 ]; then result="FAILURE" fi @@ -35,7 +58,7 @@ function testGoTests { echo " " echo Not on travis, so running the notravis Go tests echo " " - + # Requires helm. # At the moment not asking travis to install it. go test -v sigs.k8s.io/kustomize/v3/pkg/target \ @@ -53,92 +76,116 @@ function testGoTests { } function testExamplesAgainstLatestRelease { - /bin/rm -f $HOME/go/bin/kustomize + /bin/rm -f $(go env GOPATH)/bin/kustomize # Install latest release. go get sigs.k8s.io/kustomize/v3/cmd/kustomize - PATH=$HOME/go/bin:$PATH \ - mdrip --mode test --label testAgainstLatestRelease ./examples + + mdrip --mode test --label testAgainstLatestRelease ./examples if [ -z ${TRAVIS+x} ]; then echo " " echo Not on travis, so running the notravis example tests echo " " - # Requires helm. At the moment not asking travis to install it. - PATH=$HOME/go/bin:$PATH \ - mdrip --mode test --label helmtest README.md ./examples/chart.md + # The following requires helm. + # At the moment not asking travis to install it. + mdrip --mode test --label helmtest README.md ./examples/chart.md fi } function testExamplesAgainstHead { - /bin/rm -f $HOME/go/bin/kustomize + /bin/rm -f $(go env GOPATH)/bin/kustomize # Install from head. go install sigs.k8s.io/kustomize/v3/cmd/kustomize # To test examples of unreleased features, add # examples with code blocks annotated with some # label _other than_ @testAgainstLatestRelease. - PATH=$HOME/go/bin:$PATH \ - mdrip --mode test --label testAgainstLatestRelease ./examples + mdrip --mode test --label testAgainstLatestRelease ./examples } function generateCode { - ./plugin/generateBuiltins.sh $oldGoPath + ./plugin/generateBuiltins.sh $preferredGoPath } -# Use of GOPATH is optional if go modules are -# used. This script tries to work for people who -# don't have GOPATH set, and work for travis. -# -# Upon entry, travis has GOPATH set, and used it -# to install mdrip and the like. +# This script tries to work for both travis +# and contributors who have or do not have +# GOPATH set. # # Use GOPATH to define XDG_CONFIG_HOME, then unset # GOPATH so that go.mod is unambiguously honored. -echo "GOPATH=$GOPATH" -if [ -z ${GOPATH+x} ]; then - echo GOPATH is unset - tmp=$HOME/gopath - if [ -d "$tmp" ]; then - oldGoPath=$tmp - else - tmp=$HOME/go +function setPreferredGoPathAndUnsetGoPath { + preferredGoPath=$GOPATH + if [ -z ${GOPATH+x} ]; then + # GOPATH is unset + local tmp=$HOME/gopath if [ -d "$tmp" ]; then - oldGoPath=$tmp + preferredGoPath=$tmp + else + # this works even if GOPATH undefined. + preferredGoPath=$(go env GOPATH) fi + else + unset GOPATH fi -else - oldGoPath=$GOPATH - unset GOPATH -fi -echo "oldGoPath=$oldGoPath" -export XDG_CONFIG_HOME=$oldGoPath/src/sigs.k8s.io -echo "XDG_CONFIG_HOME=$XDG_CONFIG_HOME" -if [ ! -d "$XDG_CONFIG_HOME" ]; then - echo "$XDG_CONFIG_HOME is not a directory." - exit 1 -fi + + if [ -z ${GOPATH+x} ]; then + echo GOPATH is unset + else + echo "GOPATH=$GOPATH, but should be unset at this point." + exit 1 + fi + echo "preferredGoPath=$preferredGoPath" +} # Until go v1.13, set this explicitly. export GO111MODULE=on +# We don't want GOPATH to be defined, as it +# has too much baggage. +setPreferredGoPathAndUnsetGoPath + +# This is needed for plugins. +export XDG_CONFIG_HOME=$preferredGoPath/src/sigs.k8s.io +echo "XDG_CONFIG_HOME=$XDG_CONFIG_HOME" +if [ ! -d "$XDG_CONFIG_HOME" ]; then + echo "$XDG_CONFIG_HOME is not a directory." + echo "Unable to compile or otherwise work with kustomize plugins." + exit 1 +fi + +# With GOPATH now undefined, this most +# likely this puts $HOME/go/bin on the path. +# Regardless, subsequent go tool installs will +# be placing binaries in this location. +PATH=$(go env GOPATH)/bin:$PATH + +# Make sure we run in the root of the repo and +# therefore run the tests on all packages +base_dir="$( cd "$(dirname "$0")/.." && pwd )" +cd "$base_dir" || { + echo "Cannot cd to '$base_dir'. Aborting." >&2 + exit 1 +} + echo "HOME=$HOME" -echo "GOPATH=$GOPATH" -echo "GO111MODULE=$GO111MODULE" +echo "PATH=$PATH" echo pwd=`pwd` echo " " echo "Working..." +runFunc installTools + runFunc generateCode runFunc testGoLangCILint runFunc testGoTests runFunc testExamplesAgainstLatestRelease runFunc testExamplesAgainstHead -if [ $rc -eq 0 ]; then +if [ $rcAccumulator -eq 0 ]; then echo "SUCCESS!" else - echo "FAILURE; exit code $rc" + echo "FAILURE; exit code $rcAccumulator" fi -exit $rc +exit $rcAccumulator