File: base.sh

package info (click to toggle)
mongo-c-driver 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 49,532 kB
  • sloc: ansic: 197,016; python: 7,890; cpp: 2,343; sh: 693; makefile: 77
file content (70 lines) | stat: -rwxr-xr-x 1,592 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash

## Commands defined by this file:
#
# * log [...]
#     • Print the given messages to stderr
# * debug [...]
#     • Print the given messages to stderr only if PRINT_DEBUG_LOGS is truthy
# * fail [...]
#     • Print the given messages to stderr and return non-zero
# * is-set <var>
#     • Return zero if the given argument names a variable that is defined
#       (empty strings are still considered to be defined)
# * run-chdir <dir> [<command> ...]
#     • Enter directory <dir> and execute the given command. The working
#       directory of the caller is not changed.
# * have-command <command>
#     • Return zero if <command> names a command that can be executed by the shell
# * is-file <path>
# * is-dir <path>
# * exists <path>
#     • Return zero if <path> names a file, directory, or either, respectively.

set -o errexit
set -o pipefail
set -o nounset

is-set() {
  [[ -n ${!1+x} ]]
}

log() {
  echo "${@}" 1>&2
  return 0
}

debug() {
  if [[ "${PRINT_DEBUG_LOGS:-0}" != "0" ]]; then
    log "${@}"
  fi
}

fail() {
  log "${@}"
  return 1
}

run-chdir() {
  [[ "$#" -gt 1 ]] || fail "run-chdir expects at least two arguments"
  local _dir="$1"
  shift
  pushd "$_dir" >/dev/null
  debug "Run in directory [$_dir]:" "$@"
  "$@"
  local _rc=$?
  popd >/dev/null
  return $_rc
}

is-file() { [[ -f "$1" ]]; }
is-dir() { [[ -d "$1" ]]; }
exists() { [[ -e "$1" ]]; }

have-command() {
  [[ "$#" -eq 1 ]] || fail "have-command expects a single argument"
  type "$1" >/dev/null 2>&1
}

# Inhibit msys path conversion
export MSYS2_ARG_CONV_EXCL="*"