github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/build-support/functions/10-util.sh (about)

     1  function err {
     2     if test "${COLORIZE}" -eq 1
     3     then
     4        tput bold
     5        tput setaf 1
     6     fi
     7  
     8     echo "$@" 1>&2
     9  
    10     if test "${COLORIZE}" -eq 1
    11     then
    12        tput sgr0
    13     fi
    14  }
    15  
    16  function status {
    17     if test "${COLORIZE}" -eq 1
    18     then
    19        tput bold
    20        tput setaf 4
    21     fi
    22  
    23     echo "$@"
    24  
    25     if test "${COLORIZE}" -eq 1
    26     then
    27        tput sgr0
    28     fi
    29  }
    30  
    31  function status_stage {
    32     if test "${COLORIZE}" -eq 1
    33     then
    34        tput bold
    35        tput setaf 2
    36     fi
    37  
    38     echo "$@"
    39  
    40     if test "${COLORIZE}" -eq 1
    41     then
    42        tput sgr0
    43     fi
    44  }
    45  
    46  function debug {
    47     if is_set "${BUILD_DEBUG}"
    48     then
    49        if test "${COLORIZE}" -eq 1
    50        then
    51           tput setaf 6
    52        fi
    53        echo "$@"
    54        if test "${COLORIZE}" -eq 1
    55        then
    56           tput sgr0
    57        fi
    58     fi
    59  }
    60  
    61  function sed_i {
    62     if test "$(uname)" == "Darwin"
    63     then
    64        sed -i '' "$@"
    65        return $?
    66     else
    67        sed -i "$@"
    68        return $?
    69     fi
    70  }
    71  
    72  function is_set {
    73     # Arguments:
    74     #   $1 - string value to check its truthiness
    75     #
    76     # Return:
    77     #   0 - is truthy (backwards I know but allows syntax like `if is_set <var>` to work)
    78     #   1 - is not truthy
    79  
    80     local val=$(tr '[:upper:]' '[:lower:]' <<< "$1")
    81     case $val in
    82        1 | t | true | y | yes)
    83           return 0
    84           ;;
    85        *)
    86           return 1
    87           ;;
    88     esac
    89  }
    90  
    91  function have_gpg_key {
    92     # Arguments:
    93     #   $1 - GPG Key id to check if we have installed
    94     #
    95     # Return:
    96     #   0 - success (we can use this key for signing)
    97     #   * - failure (key cannot be used)
    98  
    99     gpg --list-secret-keys $1 > /dev/null 2>&1
   100     return $?
   101  }
   102  
   103  function parse_version {
   104     # Arguments:
   105     #   $1 - Path to the top level Consul source
   106     #   $2 - boolean value for whether the release version should be parsed from the source
   107     #   $3 - boolean whether to use GIT_DESCRIBE and GIT_COMMIT environment variables
   108     #   $4 - boolean whether to omit the version part of the version string. (optional)
   109     #
   110     # Return:
   111     #   0 - success (will write the version to stdout)
   112     #   * - error   (no version output)
   113     #
   114     # Notes:
   115     #   If the GOTAGS environment variable is present then it is used to determine which
   116     #   version file to use for parsing.
   117  
   118     local vfile="${1}/version/version.go"
   119  
   120     # ensure the version file exists
   121     if ! test -f "${vfile}"
   122     then
   123        err "Error - File not found: ${vfile}"
   124        return 1
   125     fi
   126  
   127     local include_release="$2"
   128     local use_git_env="$3"
   129     local omit_version="$4"
   130  
   131     local git_version=""
   132     local git_commit=""
   133  
   134     if test -z "${include_release}"
   135     then
   136        include_release=true
   137     fi
   138  
   139     if test -z "${use_git_env}"
   140     then
   141        use_git_env=true
   142     fi
   143  
   144     if is_set "${use_git_env}"
   145     then
   146        git_version="${GIT_DESCRIBE}"
   147        git_commit="${GIT_COMMIT}"
   148     fi
   149  
   150     # Get the main version out of the source file
   151     version_main=$(awk '$1 == "Version" && $2 == "=" { gsub(/"/, "", $3); print $3 }' < ${vfile})
   152     release_main=$(awk '$1 == "VersionPrerelease" && $2 == "=" { gsub(/"/, "", $3); print $3 }' < ${vfile})
   153  
   154  
   155     # try to determine the version if we have build tags
   156     for tag in "$GOTAGS"
   157     do
   158        for vfile in $(find "${1}/version" -name "version_*.go" 2> /dev/null| sort)
   159        do
   160           if grep -q "// +build $tag" "${vfile}"
   161           then
   162              version_main=$(awk '$1 == "Version" && $2 == "=" { gsub(/"/, "", $3); print $3 }' < ${vfile})
   163              release_main=$(awk '$1 == "VersionPrerelease" && $2 == "=" { gsub(/"/, "", $3); print $3 }' < ${vfile})
   164           fi
   165        done
   166     done
   167  
   168     local version="${version_main}"
   169     # override the version from source with the value of the GIT_DESCRIBE env var if present
   170     if test -n "${git_version}"
   171     then
   172        version="${git_version}"
   173     fi
   174  
   175     local rel_ver=""
   176     if is_set "${include_release}"
   177     then
   178        # Default to pre-release from the source
   179        rel_ver="${release_main}"
   180  
   181        # When no GIT_DESCRIBE env var is present and no release is in the source then we
   182        # are definitely in dev mode
   183        if test -z "${git_version}" -a -z "${rel_ver}" && is_set "${use_git_env}"
   184        then
   185           rel_ver="dev"
   186        fi
   187  
   188        # Add the release to the version
   189        if test -n "${rel_ver}" -a -n "${git_commit}"
   190        then
   191           rel_ver="${rel_ver} (${git_commit})"
   192        fi
   193     fi
   194  
   195     if test -n "${rel_ver}"
   196     then
   197        if is_set "${omit_version}"
   198        then
   199           echo "${rel_ver}" | tr -d "'"
   200        else
   201           echo "${version}-${rel_ver}" | tr -d "'"
   202        fi
   203        return 0
   204     elif ! is_set "${omit_version}"
   205     then
   206        echo "${version}" | tr -d "'"
   207        return 0
   208     else
   209        return 1
   210     fi
   211  }
   212  
   213  function get_version {
   214     # Arguments:
   215     #   $1 - Path to the top level Consul source
   216     #   $2 - Whether the release version should be parsed from source (optional)
   217     #   $3 - Whether to use GIT_DESCRIBE and GIT_COMMIT environment variables
   218     #
   219     # Returns:
   220     #   0 - success (the version is also echoed to stdout)
   221     #   1 - error
   222     #
   223     # Notes:
   224     #   If a VERSION environment variable is present it will override any parsing of the version from the source
   225     #   In addition to processing the main version.go, version_*.go files will be processed if they have
   226     #   a Go build tag that matches the one in the GOTAGS environment variable. This tag processing is
   227     #   primitive though and will not match complex build tags in the files with negation etc.
   228  
   229     local vers="$VERSION"
   230     if test -z "$vers"
   231     then
   232        # parse the OSS version from version.go
   233        vers="$(parse_version ${1} ${2} ${3})"
   234     fi
   235  
   236     if test -z "$vers"
   237     then
   238        return 1
   239     else
   240        echo $vers
   241        return 0
   242     fi
   243  }
   244  
   245  function git_branch {
   246     # Arguments:
   247     #   $1 - Path to the git repo (optional - assumes pwd is git repo otherwise)
   248     #
   249     # Returns:
   250     #   0 - success
   251     #   * - failure
   252     #
   253     # Notes:
   254     #   Echos the current branch to stdout when successful
   255  
   256     local gdir="$(pwd)"
   257     if test -d "$1"
   258     then
   259        gdir="$1"
   260     fi
   261  
   262     pushd "${gdir}" > /dev/null
   263  
   264     local ret=0
   265     local head="$(git status -b --porcelain=v2 | awk '{if ($1 == "#" && $2 =="branch.head") { print $3 }}')" || ret=1
   266  
   267     popd > /dev/null
   268  
   269     test ${ret} -eq 0 && echo "$head"
   270     return ${ret}
   271  }
   272  
   273  function git_upstream {
   274     # Arguments:
   275     #   $1 - Path to the git repo (optional - assumes pwd is git repo otherwise)
   276     #
   277     # Returns:
   278     #   0 - success
   279     #   * - failure
   280     #
   281     # Notes:
   282     #   Echos the current upstream branch to stdout when successful
   283  
   284     local gdir="$(pwd)"
   285     if test -d "$1"
   286     then
   287        gdir="$1"
   288     fi
   289  
   290     pushd "${gdir}" > /dev/null
   291  
   292     local ret=0
   293     local head="$(git status -b --porcelain=v2 | awk '{if ($1 == "#" && $2 =="branch.upstream") { print $3 }}')" || ret=1
   294  
   295     popd > /dev/null
   296  
   297     test ${ret} -eq 0 && echo "$head"
   298     return ${ret}
   299  }
   300  
   301  function git_log_summary {
   302     # Arguments:
   303     #   $1 - Path to the git repo (optional - assumes pwd is git repo otherwise)
   304     #
   305     # Returns:
   306     #   0 - success
   307     #   * - failure
   308     #
   309  
   310     local gdir="$(pwd)"
   311     if test -d "$1"
   312     then
   313        gdir="$1"
   314     fi
   315  
   316     pushd "${gdir}" > /dev/null
   317  
   318     local ret=0
   319  
   320     local head=$(git_branch) || ret=1
   321     local upstream=$(git_upstream) || ret=1
   322     local rev_range="${head}...${upstream}"
   323  
   324     if test ${ret} -eq 0
   325     then
   326        status "Git Changes:"
   327        git log --pretty=oneline ${rev_range} || ret=1
   328  
   329     fi
   330     return $ret
   331  }
   332  
   333  function git_diff {
   334     # Arguments:
   335     #   $1 - Path to the git repo (optional - assumes pwd is git repo otherwise)
   336     #   $2 .. $N - Optional path specification
   337     #
   338     # Returns:
   339     #   0 - success
   340     #   * - failure
   341     #
   342  
   343     local gdir="$(pwd)"
   344     if test -d "$1"
   345     then
   346        gdir="$1"
   347     fi
   348  
   349     shift
   350  
   351     pushd "${gdir}" > /dev/null
   352  
   353     local ret=0
   354  
   355     local head=$(git_branch) || ret=1
   356     local upstream=$(git_upstream) || ret=1
   357  
   358     if test ${ret} -eq 0
   359     then
   360        status "Git Diff - Paths: $@"
   361        git diff ${HEAD} ${upstream} -- "$@" || ret=1
   362     fi
   363     return $ret
   364  }
   365  
   366  function normalize_git_url {
   367     url="${1#https://}"
   368     url="${url#git@}"
   369     url="${url%.git}"
   370     url="$(sed ${SED_EXT} -e 's/([^\/:]*)[:\/](.*)/\1:\2/' <<< "${url}")"
   371     echo "$url"
   372     return 0
   373  }
   374  
   375  function git_remote_url {
   376     # Arguments:
   377     #   $1 - Path to the top level Consul source
   378     #   $2 - Remote name
   379     #
   380     # Returns:
   381     #   0 - success
   382     #   * - error
   383     #
   384     # Note:
   385     #   The push url for the git remote will be echoed to stdout
   386  
   387     if ! test -d "$1"
   388     then
   389        err "ERROR: '$1' is not a directory. git_remote_url must be called with the path to the top level source as the first argument'"
   390        return 1
   391     fi
   392  
   393     if test -z "$2"
   394     then
   395        err "ERROR: git_remote_url must be called with a second argument that is the name of the remote"
   396        return 1
   397     fi
   398  
   399     local ret=0
   400  
   401     pushd "$1" > /dev/null
   402  
   403     local url=$(git remote get-url --push $2 2>&1) || ret=1
   404  
   405     popd > /dev/null
   406  
   407     if test "${ret}" -eq 0
   408     then
   409        echo "${url}"
   410        return 0
   411     fi
   412  }
   413  
   414  function find_git_remote {
   415     # Arguments:
   416     #   $1 - Path to the top level Consul source
   417     #
   418     # Returns:
   419     #   0 - success
   420     #   * - error
   421     #
   422     # Note:
   423     #   The remote name to use for publishing will be echoed to stdout upon success
   424  
   425     if ! test -d "$1"
   426     then
   427        err "ERROR: '$1' is not a directory. find_git_remote must be called with the path to the top level source as the first argument'"
   428        return 1
   429     fi
   430  
   431     need_url=$(normalize_git_url "${PUBLISH_GIT_HOST}:${PUBLISH_GIT_REPO}")
   432     debug "Required normalized remote: ${need_url}"
   433  
   434     pushd "$1" > /dev/null
   435  
   436     local ret=1
   437     for remote in $(git remote)
   438     do
   439        url=$(git remote get-url --push ${remote}) || continue
   440        url=$(normalize_git_url "${url}")
   441  
   442        debug "Testing Remote: ${remote}: ${url}"
   443        if test "${url}" == "${need_url}"
   444        then
   445           echo "${remote}"
   446           ret=0
   447           break
   448        fi
   449     done
   450  
   451     popd > /dev/null
   452     return ${ret}
   453  }
   454  
   455  function git_remote_not_blacklisted {
   456     # Arguments:
   457     #   $1 - path to the repo
   458     #   $2 - the remote name
   459     #
   460     # Returns:
   461     #   0 - not blacklisted
   462     #   * - blacklisted
   463     return 0
   464  }
   465  
   466  function is_git_clean {
   467     # Arguments:
   468     #   $1 - Path to git repo
   469     #   $2 - boolean whether the git status should be output when not clean
   470     #
   471     # Returns:
   472     #   0 - success
   473     #   * - error
   474     #
   475  
   476     if ! test -d "$1"
   477     then
   478        err "ERROR: '$1' is not a directory. is_git_clean must be called with the path to a git repo as the first argument'"
   479        return 1
   480     fi
   481  
   482     local output_status="$2"
   483  
   484     pushd "${1}" > /dev/null
   485  
   486     local ret=0
   487     test -z "$(git status --porcelain=v2 2> /dev/null)" || ret=1
   488  
   489     if is_set "${output_status}" && test "$ret" -ne 0
   490     then
   491        err "Git repo is not clean"
   492        # --porcelain=v1 is the same as --short except uncolorized
   493        git status --porcelain=v1
   494     fi
   495     popd > /dev/null
   496     return ${ret}
   497  }
   498  
   499  function update_git_env {
   500     # Arguments:
   501     #   $1 - Path to git repo
   502     #
   503     # Returns:
   504     #   0 - success
   505     #   * - error
   506     #
   507  
   508     if ! test -d "$1"
   509     then
   510        err "ERROR: '$1' is not a directory. is_git_clean must be called with the path to a git repo as the first argument'"
   511        return 1
   512     fi
   513  
   514     export GIT_COMMIT=$(git rev-parse --short HEAD)
   515     export GIT_DIRTY=$(test -n "$(git status --porcelain)" && echo "+CHANGES")
   516     export GIT_DESCRIBE=$(git describe --tags --always)
   517     export GIT_IMPORT=github.com/hashicorp/consul/version
   518     export GOLDFLAGS="-X ${GIT_IMPORT}.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X ${GIT_IMPORT}.GitDescribe=${GIT_DESCRIBE}"
   519     return 0
   520  }
   521  
   522  function git_push_ref {
   523     # Arguments:
   524     #   $1 - Path to the top level Consul source
   525     #   $2 - Git ref (optional)
   526     #   $3 - remote (optional - if not specified we will try to determine it)
   527     #
   528     # Returns:
   529     #   0 - success
   530     #   * - error
   531  
   532     if ! test -d "$1"
   533     then
   534        err "ERROR: '$1' is not a directory. push_git_release must be called with the path to the top level source as the first argument'"
   535        return 1
   536     fi
   537  
   538     local sdir="$1"
   539     local ret=0
   540     local remote="$3"
   541  
   542     # find the correct remote corresponding to the desired repo (basically prevent pushing enterprise to oss or oss to enterprise)
   543     if test -z "${remote}"
   544     then
   545        local remote=$(find_git_remote "${sdir}") || return 1
   546        status "Using git remote: ${remote}"
   547     fi
   548  
   549     local ref=""
   550  
   551     pushd "${sdir}" > /dev/null
   552  
   553     if test -z "$2"
   554     then
   555        # If no git ref was provided we lookup the current local branch and its tracking branch
   556        # It must have a tracking upstream and it must be tracking the sanctioned git remote
   557        local head=$(git_branch "${sdir}") || return 1
   558        local upstream=$(git_upstream "${sdir}") || return 1
   559  
   560        # upstream branch for this branch does not track the remote we need to push to
   561        # basically this checks that the upstream (could be something like origin/master) references the correct remote
   562        # if it doesn't then the string modification wont apply and the var will reamin unchanged and equal to itself.
   563        if test "${upstream#${remote}/}" == "${upstream}"
   564        then
   565           err "ERROR: Upstream branch '${upstream}' does not track the correct remote '${remote}' - cannot push"
   566           ret=1
   567        fi
   568        ref="refs/heads/${head}"
   569     else
   570        # A git ref was provided - get the full ref and make sure it isn't ambiguous and also to
   571        # be able to determine whether its a branch or tag we are pushing
   572        ref_out=$(git rev-parse --symbolic-full-name "$2" --)
   573  
   574        # -ne 2 because it should have the ref on one line followed by a line with '--'
   575        if test "$(wc -l <<< "${ref_out}")" -ne 2
   576        then
   577           err "ERROR: Git ref '$2' is ambiguous"
   578           debug "${ref_out}"
   579           ret=1
   580        else
   581           ref=$(head -n 1 <<< "${ref_out}")
   582        fi
   583     fi
   584  
   585     if test ${ret} -eq 0
   586     then
   587        case "${ref}" in
   588           refs/tags/*)
   589              status "Pushing tag ${ref#refs/tags/} to ${remote}"
   590              ;;
   591           refs/heads/*)
   592              status "Pushing local branch ${ref#refs/tags/} to ${remote}"
   593              ;;
   594           *)
   595              err "ERROR: git_push_ref func is refusing to push ref that isn't a branch or tag"
   596              return 1
   597        esac
   598  
   599        if ! git push "${remote}" "${ref}"
   600        then
   601           err "ERROR: Failed to push ${ref} to remote: ${remote}"
   602           ret=1
   603        fi
   604     fi
   605  
   606     popd > /dev/null
   607  
   608     return $ret
   609  }
   610  
   611  function update_version {
   612     # Arguments:
   613     #   $1 - Path to the version file
   614     #   $2 - Version string
   615     #   $3 - PreRelease version (if unset will become an empty string)
   616     #
   617     # Returns:
   618     #   0 - success
   619     #   * - error
   620  
   621     if ! test -f "$1"
   622     then
   623        err "ERROR: '$1' is not a regular file. update_version must be called with the path to a go version file"
   624        return 1
   625     fi
   626  
   627     if test -z "$2"
   628     then
   629        err "ERROR: The version specified was empty"
   630        return 1
   631     fi
   632  
   633     local vfile="$1"
   634     local version="$2"
   635     local prerelease="$3"
   636  
   637     sed_i ${SED_EXT} -e "s/(Version[[:space:]]*=[[:space:]]*)\"[^\"]*\"/\1\"${version}\"/g" -e "s/(VersionPrerelease[[:space:]]*=[[:space:]]*)\"[^\"]*\"/\1\"${prerelease}\"/g" "${vfile}"
   638     return $?
   639  }
   640  
   641  function set_changelog_version {
   642     # Arguments:
   643     #   $1 - Path to top level Consul source
   644     #   $2 - Version to put into the Changelog
   645     #   $3 - Release Date
   646     #
   647     # Returns:
   648     #   0 - success
   649     #   * - error
   650  
   651     local changelog="${1}/CHANGELOG.md"
   652     local version="$2"
   653     local rel_date="$3"
   654  
   655     if ! test -f "${changelog}"
   656     then
   657        err "ERROR: File not found: ${changelog}"
   658        return 1
   659     fi
   660  
   661     if test -z "${version}"
   662     then
   663        err "ERROR: Must specify a version to put into the changelog"
   664        return 1
   665     fi
   666  
   667     if test -z "${rel_date}"
   668     then
   669        rel_date=$(date +"%B %d, %Y")
   670     fi
   671  
   672     sed_i ${SED_EXT} -e "s/## UNRELEASED/## ${version} (${rel_date})/" "${changelog}"
   673     return $?
   674  }
   675  
   676  function set_website_version {
   677     # Arguments:
   678     #   $1 - Path to top level Consul source
   679     #   $2 - Version to put into the website
   680     #
   681     # Returns:
   682     #   0 - success
   683     #   * - error
   684  
   685     local config_rb="${1}/website/config.rb"
   686     local version="$2"
   687  
   688     if ! test -f "${config_rb}"
   689     then
   690        err "ERROR: File not found: '${config_rb}'"
   691        return 1
   692     fi
   693  
   694     if test -z "${version}"
   695     then
   696        err "ERROR: Must specify a version to put into ${config_rb}"
   697        return 1
   698     fi
   699  
   700     sed_i ${SED_EXT} -e "s/(h.version[[:space:]]*=[[:space:]]*)\"[^\"]*\"/\1\"${version}\"/g" "${config_rb}"
   701     return $?
   702  }
   703  
   704  function unset_changelog_version {
   705     # Arguments:
   706     #   $1 - Path to top level Consul source
   707     #
   708     # Returns:
   709     #   0 - success
   710     #   * - error
   711  
   712     local changelog="${1}/CHANGELOG.md"
   713  
   714     if ! test -f "${changelog}"
   715     then
   716        err "ERROR: File not found: ${changelog}"
   717        return 1
   718     fi
   719  
   720     sed_i ${SED_EXT} -e "1 s/^## [0-9]+\.[0-9]+\.[0-9]+ \([^)]*\)/## UNRELEASED/" "${changelog}"
   721     return $?
   722  }
   723  
   724  function add_unreleased_to_changelog {
   725     # Arguments:
   726     #   $1 - Path to top level Consul source
   727     #
   728     # Returns:
   729     #   0 - success
   730     #   * - error
   731  
   732     local changelog="${1}/CHANGELOG.md"
   733  
   734     if ! test -f "${changelog}"
   735     then
   736        err "ERROR: File not found: ${changelog}"
   737        return 1
   738     fi
   739  
   740     # Check if we are already in unreleased mode
   741     if head -n 1 "${changelog}" | grep -q -c UNRELEASED
   742     then
   743        return 0
   744     fi
   745  
   746     local tfile="$(mktemp) -t "CHANGELOG.md_")"
   747     (
   748        echo -e "## UNRELEASED\n" > "${tfile}" &&
   749        cat "${changelog}" >> "${tfile}" &&
   750        cp "${tfile}" "${changelog}"
   751     )
   752     local ret=$?
   753     rm "${tfile}"
   754     return $ret
   755  }
   756  
   757  function set_release_mode {
   758     # Arguments:
   759     #   $1 - Path to top level Consul source
   760     #   $2 - The version of the release
   761     #   $3 - The release date
   762     #   $4 - The pre-release version
   763     #
   764     #
   765     # Returns:
   766     #   0 - success
   767     #   * - error
   768  
   769     if ! test -d "$1"
   770     then
   771        err "ERROR: '$1' is not a directory. set_release_mode must be called with the path to a git repo as the first argument"
   772        return 1
   773     fi
   774  
   775     if test -z "$2"
   776     then
   777        err "ERROR: The version specified was empty"
   778        return 1
   779     fi
   780  
   781     local sdir="$1"
   782     local vers="$2"
   783     local rel_date="$(date +"%B %d, %Y")"
   784  
   785     if test -n "$3"
   786     then
   787        rel_date="$3"
   788     fi
   789  
   790     local changelog_vers="${vers}"
   791     if test -n "$4"
   792     then
   793        changelog_vers="${vers}-$4"
   794     fi
   795  
   796     status_stage "==> Updating CHANGELOG.md with release info: ${changelog_vers} (${rel_date})"
   797     set_changelog_version "${sdir}" "${changelog_vers}" "${rel_date}" || return 1
   798  
   799     status_stage "==> Updating version/version.go"
   800     if ! update_version "${sdir}/version/version.go" "${vers}" "$4"
   801     then
   802        unset_changelog_version "${sdir}"
   803        return 1
   804     fi
   805  
   806     # Only update the website when allowed and there is no pre-release version
   807     if ! is_set "${CONSUL_NO_WEBSITE_UPDATE}" && test -z "$4"
   808     then
   809        status_stage "==> Updating website/config.rb"
   810        if ! set_website_version "${sdir}" "${vers}"
   811        then
   812           unset_changelog_version "${sdir}"
   813           return 1
   814        fi
   815     fi
   816  
   817     return 0
   818  }
   819  
   820  function set_dev_mode {
   821     # Arguments:
   822     #   $1 - Path to top level Consul source
   823     #
   824     # Returns:
   825     #   0 - success
   826     #   * - error
   827  
   828     if ! test -d "$1"
   829     then
   830        err "ERROR: '$1' is not a directory. set_dev_mode must be called with the path to a git repo as the first argument'"
   831        return 1
   832     fi
   833  
   834     local sdir="$1"
   835     local vers="$(parse_version "${sdir}" false false)"
   836  
   837     status_stage "==> Setting VersionPreRelease back to 'dev'"
   838     update_version "${sdir}/version/version.go" "${vers}" dev || return 1
   839  
   840     status_stage "==> Adding new UNRELEASED label in CHANGELOG.md"
   841     add_unreleased_to_changelog "${sdir}" || return 1
   842  
   843     return 0
   844  }
   845  
   846  function git_staging_empty {
   847     # Arguments:
   848     #   $1 - Path to git repo
   849     #
   850     # Returns:
   851     #   0 - success (nothing staged)
   852     #   * - error (staged files)
   853  
   854     if ! test -d "$1"
   855     then
   856        err "ERROR: '$1' is not a directory. commit_dev_mode must be called with the path to a git repo as the first argument'"
   857        return 1
   858     fi
   859  
   860     pushd "$1" > /dev/null
   861  
   862     declare -i ret=0
   863  
   864     for status in $(git status --porcelain=v2 | awk '{print $2}' | cut -b 1)
   865     do
   866        if test "${status}" != "."
   867        then
   868           ret=1
   869           break
   870        fi
   871     done
   872  
   873     popd > /dev/null
   874     return ${ret}
   875  }
   876  
   877  function commit_dev_mode {
   878     # Arguments:
   879     #   $1 - Path to top level Consul source
   880     #
   881     # Returns:
   882     #   0 - success
   883     #   * - error
   884  
   885     if ! test -d "$1"
   886     then
   887        err "ERROR: '$1' is not a directory. commit_dev_mode must be called with the path to a git repo as the first argument'"
   888        return 1
   889     fi
   890  
   891     status "Checking for previously staged files"
   892     git_staging_empty "$1" || return 1
   893  
   894     declare -i ret=0
   895  
   896     pushd "$1" > /dev/null
   897  
   898     status "Staging CHANGELOG.md and version_*.go files"
   899     git add CHANGELOG.md && git add version/version*.go
   900     ret=$?
   901  
   902     if test ${ret} -eq 0
   903     then
   904        status "Adding Commit"
   905        git commit -m "Putting source back into Dev Mode"
   906        ret=$?
   907     fi
   908  
   909     popd >/dev/null
   910     return ${ret}
   911  }
   912  
   913  function gpg_detach_sign {
   914     # Arguments:
   915     #   $1 - File to sign
   916     #   $2 - Alternative GPG key to use for signing
   917     #
   918     # Returns:
   919     #   0 - success
   920     #   * - failure
   921  
   922     # determine whether the gpg key to use is being overridden
   923     local gpg_key=${HASHICORP_GPG_KEY}
   924     if test -n "$2"
   925     then
   926        gpg_key=$2
   927     fi
   928  
   929     gpg --default-key "${gpg_key}" --detach-sig --yes -v  "$1"
   930     return $?
   931  }
   932  
   933  function shasum_directory {
   934     # Arguments:
   935     #   $1 - Path to directory containing the files to shasum
   936     #   $2 - File to output sha sums to
   937     #
   938     # Returns:
   939     #   0 - success
   940     #   * - failure
   941  
   942     if ! test -d "$1"
   943     then
   944        err "ERROR: '$1' is not a directory and shasum_release requires passing a directory as the first argument"
   945        return 1
   946     fi
   947  
   948     if test -z "$2"
   949     then
   950        err "ERROR: shasum_release requires a second argument to be the filename to output the shasums to but none was given"
   951        return 1
   952     fi
   953  
   954     pushd $1 > /dev/null
   955     shasum -a256 * > "$2"
   956     ret=$?
   957     popd >/dev/null
   958  
   959     return $ret
   960  }
   961  
   962   function ui_version {
   963     # Arguments:
   964     #   $1 - path to index.html
   965     #
   966     # Returns:
   967     #   0 - success
   968     #   * -failure
   969     #
   970     # Notes: echoes the version to stdout upon success
   971     if ! test -f "$1"
   972     then
   973        err "ERROR: No such file: '$1'"
   974        return 1
   975     fi
   976  
   977     local ui_version="$(grep '<!-- CONSUL_VERSION: .* -->$' "$1" | sed 's/^<!-- CONSUL_VERSION: \(.*\) -->$/\1/')" || return 1
   978     echo "$ui_version"
   979     return 0
   980   }
   981   function ui_logo_type {
   982     # Arguments:
   983     #   $1 - path to index.html
   984     #
   985     # Returns:
   986     #   0 - success
   987     #   * -failure
   988     #
   989     # Notes: echoes the 'logo type' to stdout upon success
   990     # the 'logo' can be one of 'enterprise' or 'oss'
   991     # and doesn't necessarily correspond to the binary type of consul
   992     # the logo is 'enterprise' if the binary type is anything but 'oss'
   993     if ! test -f "$1"
   994     then
   995        err "ERROR: No such file: '$1'"
   996        return 1
   997     fi
   998     grep -q "data-enterprise-logo" < "$1"
   999  
  1000     if test $? -eq 0
  1001     then
  1002       echo "enterprise"
  1003     else
  1004       echo "oss"
  1005     fi
  1006     return 0
  1007   }