vitess.io/vitess@v0.16.2/tools/shell_functions.inc (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2019 The Vitess Authors.
     4  # 
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  # 
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  # 
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # Library of functions which are used by bootstrap.sh or the Makefile.
    18  
    19  # goversion_min returns true if the installed go version is at least
    20  # the version passed as the first parameter
    21  function goversion_min() {
    22    [[ "$(go version)" =~ go([0-9]+)[\.]?([0-9]*)[\.]?([0-9]*) ]]
    23    gotmajor=${BASH_REMATCH[1]}
    24    gotminor=${BASH_REMATCH[2]}
    25    gotpatch=${BASH_REMATCH[3]}
    26    [[ "$1" =~ ([0-9]+)[\.]?([0-9]*)[\.]?([0-9]*) ]]
    27    wantmajor=${BASH_REMATCH[1]}
    28    wantminor=${BASH_REMATCH[2]}
    29    wantpatch=${BASH_REMATCH[3]}
    30    [[ $gotmajor -lt $wantmajor ]] && return 1
    31    [[ $gotmajor -gt $wantmajor ]] && return 0
    32    [[ $gotminor -lt $wantminor ]] && return 1
    33    [[ $gotminor -gt $wantminor ]] && return 0
    34    [[ $gotpatch -ge $wantpatch ]] && return 0
    35    return 1
    36  }
    37  
    38  # prepend_path returns $2 prepended the colon separated path $1.
    39  # If it's already part of the path, it won't be added again.
    40  #
    41  # Note the first time it's called, the original value is empty,
    42  # and the second value has the path to add. We just end up adding it regardless
    43  # of its existence.
    44  function prepend_path() {
    45    # $1 path variable
    46    # $2 path to add
    47    if [[ ! -d "$2" ]]; then
    48      # To be added path does not exist. Ignore it and return the path variable unchanged.
    49      echo "$1"
    50      return
    51    fi
    52  
    53    if [[ -z "$1" ]]; then
    54      # path variable is empty. Set its initial value to the path to add.
    55      echo "$2"
    56      return
    57    fi
    58  
    59    if [[ ":$1:" != *":$2:"* ]]; then
    60      # path variable does not contain path to add yet. Prepend it.
    61      echo "$2:$1"
    62      return
    63    fi
    64  
    65    # Return path variable unchanged.
    66    echo "$1"
    67  }
    68  
    69  function fail() {
    70    echo "ERROR: $1"
    71    exit 1
    72  }
    73