github.com/imran-kn/cilium-fork@v1.6.9/contrib/shell/util.sh (about)

     1  #!/bin/bash
     2  # Copyright 2016 The Kubernetes Authors All rights reserved.
     3  # Copyright 2018-2019 Authors of Cilium
     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  readonly  reset=$(tput sgr0)
    18  readonly  green=$(tput bold; tput setaf 2)
    19  readonly yellow=$(tput bold; tput setaf 3)
    20  readonly   blue=$(tput bold; tput setaf 6)
    21  readonly timeout=$(if [ "$(uname)" == "Darwin" ]; then echo "1"; else echo "0.1"; fi)
    22  readonly ipv6regex='(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'
    23  
    24  function desc() {
    25      maybe_first_prompt
    26      echo "$blue# $@$reset"
    27      prompt
    28  }
    29  
    30  function desc_rate() {
    31      maybe_first_prompt
    32      rate=30
    33      if [ -n "$DEMO_RUN_FAST" ]; then
    34        rate=1000
    35      fi
    36      echo "$blue# $@$reset" | pv -qL $rate
    37      prompt
    38  }
    39  
    40  function prompt() {
    41      echo -n "$yellow\$ $reset"
    42  }
    43  
    44  started=""
    45  function maybe_first_prompt() {
    46      if [ -z "$started" ]; then
    47          prompt
    48          started=true
    49      fi
    50  }
    51  
    52  # After a `run` this variable will hold the stdout of the command that was run.
    53  # If the command was interactive, this will likely be garbage.
    54  DEMO_RUN_STDOUT=""
    55  
    56  function run() {
    57      maybe_first_prompt
    58      rate=50
    59      if [ -n "$DEMO_RUN_FAST" ]; then
    60        rate=1000
    61      fi
    62      echo "$green$1$reset" | pv -qL $rate
    63      if [ -n "$DEMO_RUN_FAST" ]; then
    64        sleep 0.5
    65      fi
    66      OFILE="$(mktemp -t $(basename $0).XXXXXX)"
    67      script -eq -c "$1" -f "$OFILE"
    68      r=$?
    69      read -d '' -t "${timeout}" -n 10000 # clear stdin
    70      prompt
    71      if [ -z "$DEMO_AUTO_RUN" ]; then
    72        read -s
    73      fi
    74      DEMO_RUN_STDOUT="$(tail -n +2 $OFILE | sed 's/\r//g')"
    75      return $r
    76  }
    77  
    78  function relative() {
    79      for arg; do
    80          echo "$(realpath $(dirname $(which $0)))/$arg" | sed "s|$(realpath $(pwd))|.|"
    81      done
    82  }
    83  
    84  # Continue rebasing and progressively update the daemon/bpf.sha each time there
    85  # is a merge conflict for it. If there are merge conflicts in other files, it
    86  # will stop rebasing and return for user input.
    87  #
    88  # Expected usage:
    89  #   $ git rebase origin/master
    90  #   <Merge failure on daemon/bpf.sha>
    91  #   $ rebase-bindata
    92  #   <For each conflict, your editor opens to review the commit. Save & exit>
    93  function rebase-bindata
    94  {
    95      (
    96          local dir
    97          set -x
    98          while ! git rebase --continue ; do
    99              dir=$(cd $(dirname ${BASH_SOURCE})/../.. && pwd)
   100              $dir/contrib/scripts/fix-sha.sh
   101              git add daemon/bpf.sha
   102              if [ $(git diff --diff-filter=U | wc -l) -ne 0 ]; then
   103                  echo "Files that need manual merge:"
   104                  git diff --name-only --diff-filter=U
   105                  break
   106              fi
   107          done
   108      )
   109  }
   110  
   111  trap "echo" EXIT