...

Text file src/oss.terrastruct.com/d2/ci/release/template/scripts/lib.sh

Documentation: oss.terrastruct.com/d2/ci/release/template/scripts

     1#!/bin/sh
     2
     3# *************
     4# DO NOT EDIT
     5#
     6# lib.sh was bundled together from
     7#
     8# - ./ci/sub/lib/rand.sh
     9# - ./ci/sub/lib/temp.sh
    10# - ./ci/sub/lib/log.sh
    11# - ./ci/sub/lib/release.sh
    12#
    13# Generated by ./ci/release/gen_template_lib.sh.
    14# *************
    15
    16#!/bin/sh
    17if [ "${LIB_RAND-}" ]; then
    18  return 0
    19fi
    20LIB_RAND=1
    21
    22pick() {
    23  seed="$1"
    24  shift
    25
    26  seed_file="$(mktempd)/pickseed"
    27
    28  # We add 32 more bytes to the seed file for sufficient entropy. Otherwise both Cygwin's
    29  # and MinGW's sort for example complains about the lack of entropy on stderr and writes
    30  # nothing to stdout. I'm sure there are more platforms that would too.
    31  #
    32  # We also limit to a max of 32 bytes as otherwise macOS's sort complains that the random
    33  # seed is too large. Probably more platforms too.
    34  (echo "$seed" && echo "================================") | head -c32 >"$seed_file"
    35
    36  while [ $# -gt 0 ]; do
    37    echo "$1"
    38    shift
    39  done \
    40    | sort --sort=random --random-source="$seed_file" \
    41    | head -n1
    42}
    43#!/bin/sh
    44if [ "${LIB_TEMP-}" ]; then
    45  return 0
    46fi
    47LIB_TEMP=1
    48
    49ensure_tmpdir() {
    50  if [ -n "${_TMPDIR-}" ]; then
    51    return
    52  fi
    53  _TMPDIR=$(mktemp -d)
    54  export _TMPDIR
    55}
    56
    57if [ -z "${_TMPDIR-}" ]; then
    58  trap 'rm -Rf "$_TMPDIR"' EXIT
    59fi
    60ensure_tmpdir
    61
    62temppath() {
    63  while true; do
    64    temppath=$_TMPDIR/$(</dev/urandom od -N8 -tx -An -v | tr -d '[:space:]')
    65    if [ ! -e "$temppath" ]; then
    66      echo "$temppath"
    67      return
    68    fi
    69  done
    70}
    71
    72mktempd() {
    73  tp=$(temppath)
    74  mkdir -p "$tp"
    75  echo "$tp"
    76}
    77#!/bin/sh
    78if [ "${LIB_LOG-}" ]; then
    79  return 0
    80fi
    81LIB_LOG=1
    82
    83if [ -n "${TRACE-}" ]; then
    84  set -x
    85fi
    86
    87tput() {
    88  if should_color; then
    89    TERM=${TERM:-xterm-256color} command tput "$@"
    90  fi
    91}
    92
    93should_color() {
    94  if [ -n "${COLOR-}" ]; then
    95    if [ "$COLOR" = 1 -o "$COLOR" = true ]; then
    96      _COLOR=1
    97      __COLOR=1
    98      return 0
    99    elif [ "$COLOR" = 0 -o "$COLOR" = false ]; then
   100      _COLOR=
   101      __COLOR=0
   102      return 1
   103    else
   104      printf '$COLOR must be 0, 1, false or true but got %s\n' "$COLOR" >&2
   105    fi
   106  fi
   107
   108  if [ -t 1 -a "${TERM-}" != dumb ]; then
   109    _COLOR=1
   110    __COLOR=1
   111    return 0
   112  else
   113    _COLOR=
   114    __COLOR=0
   115    return 1
   116  fi
   117}
   118
   119setaf() {
   120  fg=$1
   121  shift
   122  printf '%s\n' "$*" | while IFS= read -r line; do
   123    tput setaf "$fg"
   124    printf '%s' "$line"
   125    tput sgr0
   126    printf '\n'
   127  done
   128}
   129
   130_echo() {
   131  printf '%s\n' "$*"
   132}
   133
   134get_rand_color() {
   135  if [ "${TERM_COLORS+x}" != x ]; then
   136    TERM_COLORS=""
   137    export TERM_COLORS
   138    ncolors=$(TERM=${TERM:-xterm-256color} command tput colors)
   139    if [ "$ncolors" -ge 8 ]; then
   140      # 1-6 are regular
   141      TERM_COLORS="$TERM_COLORS 1 2 3 4 5 6"
   142    elif [ "$ncolors" -ge 16 ]; then
   143      # 9-14 are bright.
   144      TERM_COLORS="$TERM_COLORS 9 10 11 12 13 14"
   145    fi
   146  fi
   147  pick "$*" $TERM_COLORS
   148}
   149
   150echop() {
   151  prefix="$1"
   152  shift
   153
   154  if [ "$#" -gt 0 ]; then
   155    printfp "$prefix" "%s\n" "$*"
   156  else
   157    printfp "$prefix"
   158    printf '\n'
   159  fi
   160}
   161
   162printfp() {(
   163  prefix="$1"
   164  shift
   165
   166  _FGCOLOR=${FGCOLOR:-$(get_rand_color "$prefix")}
   167  should_color || true
   168  if [ $# -eq 0 ]; then
   169    printf '%s' "$(COLOR=$__COLOR setaf "$_FGCOLOR" "$prefix")"
   170  else
   171    printf '%s: %s\n' "$(COLOR=$__COLOR setaf "$_FGCOLOR" "$prefix")" "$(printf "$@")"
   172  fi
   173)}
   174
   175catp() {
   176  prefix="$1"
   177  shift
   178
   179  should_color || true
   180  sed "s/^/$(COLOR=$__COLOR printfp "$prefix" '')/"
   181}
   182
   183repeat() {
   184  char="$1"
   185  times="$2"
   186  seq -s "$char" "$times" | tr -d '[:digit:]'
   187}
   188
   189strlen() {
   190  printf %s "$1" | wc -c
   191}
   192
   193echoerr() {
   194  FGCOLOR=1 logp err "$*"
   195}
   196
   197caterr() {
   198  FGCOLOR=1 logpcat err "$@"
   199}
   200
   201printferr() {
   202  FGCOLOR=1 logfp err "$@"
   203}
   204
   205logp() {
   206  should_color >&2 || true
   207  COLOR=$__COLOR echop "$@" | humanpath >&2
   208}
   209
   210logfp() {
   211  should_color >&2 || true
   212  COLOR=$__COLOR printfp "$@" | humanpath >&2
   213}
   214
   215logpcat() {
   216  should_color >&2 || true
   217  COLOR=$__COLOR catp "$@" | humanpath >&2
   218}
   219
   220log() {
   221  FGCOLOR=5 logp log "$@"
   222}
   223
   224logf() {
   225  FGCOLOR=5 logfp log "$@"
   226}
   227
   228logcat() {
   229  FGCOLOR=5 logpcat log "$@"
   230}
   231
   232warn() {
   233  FGCOLOR=3 logp warn "$@"
   234}
   235
   236warnf() {
   237  FGCOLOR=3 logfp warn "$@"
   238}
   239
   240warncat() {
   241  FGCOLOR=3 logpcat warn "$@"
   242}
   243
   244sh_c() {
   245  FGCOLOR=3 logp exec "$*"
   246  if [ -z "${DRY_RUN-}" ]; then
   247    eval "$@"
   248  fi
   249}
   250
   251sudo_sh_c() {
   252  if [ "$(id -u)" -eq 0 ]; then
   253    sh_c "$@"
   254  elif command -v doas >/dev/null; then
   255    sh_c "doas $*"
   256  elif command -v sudo >/dev/null; then
   257    sh_c "sudo $*"
   258  elif command -v su >/dev/null; then
   259    sh_c "su root -c '$*'"
   260  else
   261    caterr <<EOF
   262Unable to run the following command as root:
   263  $*
   264Please install doas, sudo, or su.
   265EOF
   266    return 1
   267  fi
   268}
   269
   270header() {
   271  FGCOLOR=${FGCOLOR:-4} logp "/* $1 */"
   272}
   273
   274bigheader() {
   275  set -- "$(echo "$*" | sed "s/^/ * /")"
   276  FGCOLOR=${FGCOLOR:-6} logp "/****************************************************************
   277$*
   278 ****************************************************************/"
   279}
   280
   281# humanpath replaces all occurrences of " $HOME" with " ~"
   282# and all occurrences of '$HOME' with the literal '$HOME'.
   283humanpath() {
   284  if [ -z "${HOME-}" ]; then
   285    cat
   286  else
   287    sed -e "s# $HOME# ~#g" -e "s#$HOME#\$HOME#g"
   288  fi
   289}
   290
   291hide() {
   292  out="$(mktempd)/hideout"
   293  capcode "$@" >"$out" 2>&1
   294  if [ "$code" -eq 0 ]; then
   295    return
   296  fi
   297  cat "$out" >&2
   298  return "$code"
   299}
   300
   301hide_stderr() {
   302  out="$(mktempd)/hideout"
   303  capcode "$@" 2>"$out"
   304  if [ "$code" -eq 0 ]; then
   305    return
   306  fi
   307  cat "$out" >&2
   308  return "$code"
   309}
   310
   311echo_dur() {
   312  local dur=$1
   313  local h=$((dur/60/60))
   314  local m=$((dur/60%60))
   315  local s=$((dur%60))
   316  printf '%dh%dm%ds' "$h" "$m" "$s"
   317}
   318
   319sponge() {
   320  dst="$1"
   321  tmp="$(mktempd)/sponge"
   322  cat > "$tmp"
   323  cat "$tmp" > "$dst"
   324}
   325
   326stripansi() {
   327  # First regex gets rid of standard xterm escape sequences for controlling
   328  # visual attributes.
   329  # The second regex I'm not 100% sure, the reference says it selects the US
   330  # encoding but I'm not sure why that's necessary or why it always occurs
   331  # in tput sgr0 before the standard escape sequence.
   332  # See tput sgr0 | xxd
   333  sed -e $'s/\x1b\[[0-9;]*m//g' -e $'s/\x1b(.//g'
   334}
   335
   336runtty() {
   337  case "$(uname)" in
   338    Darwin)
   339      script -q /dev/null "$@"
   340      ;;
   341    Linux)
   342      script -eqc "$*"
   343      ;;
   344    *)
   345      echoerr "runtty: unsupported OS $(uname)"
   346      return 1
   347  esac
   348}
   349
   350capcode() {
   351  set +e
   352  "$@"
   353  code=$?
   354  set -e
   355}
   356
   357strjoin() {
   358  (IFS="$1"; shift; echo "$*")
   359}
   360#!/bin/sh
   361if [ "${LIB_RELEASE-}" ]; then
   362  return 0
   363fi
   364LIB_RELEASE=1
   365
   366ensure_os() {
   367  if [ -n "${OS-}" ]; then
   368    # Windows defines OS=Windows_NT.
   369    if [ "$OS" = Windows_NT ]; then
   370      OS=windows
   371    fi
   372    return
   373  fi
   374  uname=$(uname)
   375  case $uname in
   376    Linux) OS=linux;;
   377    Darwin) OS=macos;;
   378    FreeBSD) OS=freebsd;;
   379    *) OS=$uname;;
   380  esac
   381}
   382
   383ensure_arch() {
   384  if [ -n "${ARCH-}" ]; then
   385    return
   386  fi
   387  uname_m=$(uname -m)
   388  case $uname_m in
   389    aarch64) ARCH=arm64;;
   390    x86_64) ARCH=amd64;;
   391    *) ARCH=$uname_m;;
   392  esac
   393}
   394
   395ensure_goos() {
   396  if [ -n "${GOOS-}" ]; then
   397    return
   398  fi
   399  ensure_os
   400  case "$OS" in
   401    macos) export GOOS=darwin;;
   402    *) export GOOS=$OS;;
   403  esac
   404}
   405
   406ensure_goarch() {
   407  if [ -n "${GOARCH-}" ]; then
   408    return
   409  fi
   410  ensure_arch
   411  case "$ARCH" in
   412    *) export GOARCH=$ARCH;;
   413  esac
   414}
   415
   416gh_repo() {
   417  gh repo view --json nameWithOwner --template '{{ .nameWithOwner }}'
   418}
   419
   420manpath() {
   421  if command -v manpath >/dev/null; then
   422    command manpath
   423  elif man -w 2>/dev/null; then
   424    man -w
   425  else
   426    echo "${MANPATH-}"
   427  fi
   428}
   429
   430is_writable_dir() {
   431  mkdir -p "$1" 2>/dev/null
   432  # directory must exist otherwise -w returns 1 even for paths that should be writable.
   433  [ -w "$1" ]
   434}
   435
   436ensure_prefix() {
   437  if [ -n "${PREFIX-}" ]; then
   438    return
   439  fi
   440  # The reason for checking whether lib is writable is that on macOS you have /usr/local
   441  # owned by root but you don't need root to write to its subdirectories which is all we
   442  # need to do.
   443  if ! is_writable_dir "/usr/local/lib"; then
   444    # This also handles M1 Mac's which do not allow modifications to /usr/local even
   445    # with sudo.
   446    PREFIX=$HOME/.local
   447  else
   448    PREFIX=/usr/local
   449  fi
   450}
   451
   452ensure_prefix_sh_c() {
   453  ensure_prefix
   454
   455  sh_c="sh_c"
   456  # The reason for checking whether lib is writable is that on macOS you have /usr/local
   457  # owned by root but you don't need root to write to its subdirectories which is all we
   458  # need to do.
   459  if ! is_writable_dir "$PREFIX/lib"; then
   460    sh_c="sudo_sh_c"
   461  fi
   462}

View as plain text