summaryrefslogtreecommitdiffstats
path: root/contrib/util.sh
blob: 5f846bec79a4c0bae2972519deced510f3156e06 (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
set -e

# Logging
TASKS=()
_last() {
  echo -n "${TASKS[0]}"
}
start() {
  for n in "${TASKS[@]}"; do
    echo -n '  '
  done
  TASKS=(
    "$@"
    "${TASKS[@]}"
  )
  echo -e "\x1b[0;1;34m@\x1b[0;34m $@\x1b[0m"
}
complete() {
  last="$(_last)"
  if [[ "$NOSHIFT" != 1 ]]; then
    TASKS=(
      "${TASKS[@]:1}"
    )
  fi
  if [[ "$1" == "" ]] && [[ "$last" != "" ]]; then
    NOSHIFT=1 complete "$last"
  else
    for n in "${TASKS[@]}"; do
      echo -n '  '
    done
    echo -e "\x1b[0;1;32m+\x1b[0;32m $@\x1b[0m"
  fi
}
err() {
  last="$(_last)"
  if [[ "$NOSHIFT" != 1 ]]; then
    TASKS=(
      "${TASKS[@]:1}"
    )
  fi
  if [[ "$1" == "" ]] && [[ "$last" != "" ]]; then
    NOSHIFT=1 err "$last"
  else
    for n in "${TASKS[@]}"; do
      echo -n '  '
    done
    echo -e "\x1b[0;1;31m!\x1b[0;31m $@\x1b[0m" 1>&2;
  fi
}
info() {
  echo -e "\x1b[0;1;94mi\x1b[0;94m $@\x1b[0m" 1>&2;
}

# Bash Array Joiner
join_array() {
  printf -v __ "${1//%/%%}%s" "${@:2}"
  echo -n "${__:${#1}}"
}

# jq abstraction layer

# Determines where to store the JSON - if the value is `IN_MEM`, then the value is kept in the JQ_BUFFER variable
JSON_STORAGE_LOCATION="IN_MEM"
JQ_BUFFER=""
JQ_BUFFER_OUTPUT_FILE=""
JQ_TRANSACTION=()
JQ_IS_DOING_TRANSACTION=false
JQ_POSITIONALS=()
JQ_POSITIONAL_IDX=1
ARG=null
jsettempstorage() {
  newstorage="${1:-"IN_MEM"}"
  if [[ "$newstorage" == "$JSON_STORAGE_LOCATION" ]]; then return 0; fi
  if [[ "$JSON_STORAGE_LOCATION" == "IN_MEM" ]]; then
    touch "$newstorage"
    <<< "$JQ_BUFFER" > "$newstorage"
    JQ_BUFFER=""
  elif [[ "$newstorage" == "IN_MEM" ]]; then
    JQ_BUFFER="$(<"$JSON_STORAGE_LOCATION")"
    rm "$JSON_STORAGE_LOCATION"
  else
    mv "$JSON_STORAGE_LOCATION" "$newstorage"
  fi
  JSON_STORAGE_LOCATION="$newstorage"
}
# Reads the storage buffer
jreadbuffer() {
  if [[ "$JSON_STORAGE_LOCATION" == "IN_MEM" ]]; then
    echo -n "$JQ_BUFFER"
  else
    cat "$JSON_STORAGE_LOCATION"
  fi
}
# Writes the storage buffer
jwritebuffer() {
  if [[ "$JSON_STORAGE_LOCATION" == "IN_MEM" ]]; then
    JQ_BUFFER="$@"
  else
    echo -n "$@" > "$JSON_STORAGE_LOCATION"
  fi
}

# Creates a new jq blank file
jnew() {
  JQ_BUFFER_OUTPUT_FILE="$(realpath "${1:-"/dev/null"}")"
  jwritebuffer "${2:-"{}"}"
}
# Loads an existing jq file, falling back to creating a blank one if it doesnt exist
jload() {
  JQ_BUFFER_OUTPUT_FILE="$(realpath "${1:-"/dev/null"}")"
  if [[ -f "$JQ_BUFFER_OUTPUT_FILE" ]]; then
    jwritebuffer "$(cat $JQ_BUFFER_OUTPUT_FILE)"
  else
    jwritebuffer "${2:-"{}"}"
  fi
}

# SQL-like Transaction Logic
jtransaction() {
  if [[ "$JQ_IS_DOING_TRANSACTION" == "true" ]]; then
    err "Already performing transaction!"
    return 1
  fi
  JQ_IS_DOING_TRANSACTION=true
  JQ_TRANSACTION=()
}
jabort() {
  if [[ "$JQ_IS_DOING_TRANSACTION" == "false" ]]; then
    err "No pending transaction!"
    return 1
  fi
  JQ_IS_DOING_TRANSACTION=false
  JQ_TRANSACTION=()
}
jcommit() {
  if [[ "$JQ_IS_DOING_TRANSACTION" == "false" ]]; then
    err "No pending transaction!"
    return 1
  fi
  JQ_IS_DOING_TRANSACTION=false
  # Incase it's empty:
  JQ_TRANSACTION+=(
    "."
  )
  # As we are no longer in a transaction, jrun will run the pipe-delimited query
  jrun "$(join_array ' | ' "${JQ_TRANSACTION[@]}")"
  # We can, after running, clear the transaction
  JQ_TRANSACTION=()
}

# Loads a string that jq can positionally access later
jsetasset() {
  ARG="(\$ARGS.positional[$JQ_POSITIONAL_IDX] | @base64d)"
  JQ_POSITIONALS+=(
    "$(base64 -w 0 <<< "$@")"
  );
  ((JQ_POSITIONAL_IDX=JQ_POSITIONAL_IDX+1))
}
jgetassetref() {
  echo -n "$ARG"
  ARG=null
}

# Perform a jq write operation
jrun() {
  if [[ "$JQ_IS_DOING_TRANSACTION" == "true" ]]; then
    # If we're in a transaction, just push it to the list
    JQ_TRANSACTION+=(
      "($@)"
    )
  else
    # Otherwise, perform the operation and write to the buffer
    if [[ "$JSON_STORAGE_LOCATION" == "IN_MEM" ]]; then
      JQ_BUFFER="$(jq -Mc "$@" --args owo ${JQ_POSITIONALS[@]} <<< "$JQ_BUFFER")"
    else
      jq -Mc "$@" --args owo ${JQ_POSITIONALS[@]} < "$JSON_STORAGE_LOCATION" > "$JSON_STORAGE_LOCATION~"
      mv "$JSON_STORAGE_LOCATION~" "$JSON_STORAGE_LOCATION"
    fi
    JQ_POSITIONALS=()
    JQ_POSITIONAL_IDX=1
  fi
}
# Perform a jq write operation using a temp file
_jrun_file() {
  if [[ "$JQ_IS_DOING_TRANSACTION" == "true" ]]; then
    # Does not work for transactions
    err "Cannot use _jrun_file in transaction!";
    return 1;
  else
    # Otherwise, perform the operation and write to the buffer
    if [[ "$JSON_STORAGE_LOCATION" == "IN_MEM" ]]; then
      echo -n "$@" > /tmp/.operation.jq
      JQ_BUFFER="$(jq -Mcf /tmp/.operation.jq --args owo ${JQ_POSITIONALS[@]} <<< "$JQ_BUFFER")"
      rm /tmp/.operation.jq
    else
      echo -n "$@" > /tmp/.operation.jq
      jq -Mcf /tmp/.operation.jq --args owo ${JQ_POSITIONALS[@]} < "$JSON_STORAGE_LOCATION" > "$JSON_STORAGE_LOCATION~"
      rm /tmp/.operation.jq
      mv "$JSON_STORAGE_LOCATION~" "$JSON_STORAGE_LOCATION"
    fi
    JQ_POSITIONALS=()
    JQ_POSITIONAL_IDX=1
  fi
}
jsave() {
  if [[ "$JQ_IS_DOING_TRANSACTION" == "true" ]]; then
    err "There is a pending transaction."
    return 1
  fi
  jreadbuffer | jq -M . > "${1:-"$JQ_BUFFER_OUTPUT_FILE"}"
}
escape_str() {
  jq '.=$ARGS.positional[0]' --args "$@" -M <<< '""'
}