~tardypad/dotfiles

ref: 00c0c349a1b8960152233f11caa9ea1a40e395a3 dotfiles/files/scripts/shell/redis-env -rwxr-xr-x 957 bytes
00c0c349 — Damien Tardy-Panis Add presentation mode to mako to hide notifications 4 months ago
                                                                                
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
#!/bin/sh

init_variables() {
  COMMAND=${0##*/}

  ENV_DIR_PATH="${XDG_DATA_HOME:-$HOME/.local/share}/env/redis"

  REDIS_OPTIONS=
  ENV_ID=
  QUERY=

  ENV_PATH=
}

parse_command_line() {
  ENV_ID="$1"
  shift 2> /dev/null

  while [ "$#" -gt 0 ] && [ "$1" != '--' ]; do
    QUERY="${QUERY} $1"
    shift
  done

  shift 2> /dev/null
  REDIS_OPTIONS="$*"
}

validate_command_line() {
  if [ -z "${ENV_ID}" ]; then
    exit_error 'missing environment identifier'
  fi

  ENV_PATH="${ENV_DIR_PATH}/${ENV_ID}"

  if [ ! -f "${ENV_PATH}" ]; then
    exit_error 'invalid environment'
  fi
}

exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  exit 1
} >&2

run_redis() {
  # shellcheck disable=SC2046 disable=SC2086
  if [ -z "${QUERY}" ]; then
    redis-cli $( cat "${ENV_PATH}" ) ${REDIS_OPTIONS}
  else
    redis-cli $( cat "${ENV_PATH}" ) --raw ${REDIS_OPTIONS} ${QUERY}
  fi
}

init_variables
parse_command_line "$@"
validate_command_line

run_redis