Skip to content

Commit 72cda4d

Browse files
committed
Refactoring into sub-modules
1 parent a27a4bb commit 72cda4d

File tree

4 files changed

+350
-251
lines changed

4 files changed

+350
-251
lines changed

lib/getters.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env sh
2+
3+
snippet_list() {
4+
snippet_curl | yush_json | grep -E '/[0-9]+/id' | awk '{print $3}'
5+
}
6+
7+
snippet_get() {
8+
if [ "$#" = "0" ]; then
9+
yush_warn "You have to specify a snippet ID"
10+
else
11+
snippet_curl "${1}/raw"
12+
fi
13+
}
14+
15+
snippet_details() {
16+
_json=0
17+
while [ $# -gt 0 ]; do
18+
case "$1" in
19+
-j | --json)
20+
_json=1; shift;;
21+
22+
-h | --help)
23+
usage "" 0;;
24+
25+
--)
26+
shift; break;;
27+
-*)
28+
usage "Unknown option: $1 !";;
29+
*)
30+
break;;
31+
esac
32+
done
33+
34+
if [ "$#" = "0" ]; then
35+
yush_warn "You have to specify a snippet ID"
36+
else
37+
if [ "$_json" = "1" ]; then
38+
snippet_curl "${1}"
39+
else
40+
snippet_curl "${1}" | yush_json
41+
fi
42+
fi
43+
}

lib/setters.sh

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/usr/bin/env sh
2+
3+
4+
_snippet_jsongen() {
5+
_fields=
6+
_title=
7+
_description=
8+
_filename=
9+
_content=
10+
_visibility=
11+
while [ $# -gt 0 ]; do
12+
case "$1" in
13+
-t | --title)
14+
_title=$2; _fields="t${_fields}"; shift 2;;
15+
--title=*)
16+
_title="${1#*=}"; _fields="t${_fields}"; shift 1;;
17+
18+
-d | --description)
19+
_description=$2; _fields="d${_fields}"; shift 2;;
20+
--description=*)
21+
_description="${1#*=}"; _fields="d${_fields}"; shift 1;;
22+
23+
-f | --filename)
24+
_filename=$2; _fields="f${_fields}"; shift 2;;
25+
--filename=*)
26+
_filename="${1#*=}"; _fields="f${_fields}"; shift 1;;
27+
28+
-c | --content)
29+
_content=$2; _fields="c${_fields}"; shift 2;;
30+
--content=*)
31+
_content="${1#*=}"; _fields="c${_fields}"; shift 1;;
32+
33+
--content-file)
34+
_content=$(cat "$2"); _fields="c${_fields}"; shift 2;;
35+
--content-file=*)
36+
_content=$(cat "${1#*=}"); _fields="c${_fields}"; shift 1;;
37+
38+
-v | --visibility)
39+
_visibility=$2; _fields="v${_fields}"; shift 2;;
40+
--visibility=*)
41+
_visibility="${1#*=}"; _fields="v${_fields}"; shift 1;;
42+
43+
-h | --help)
44+
usage "" 0;;
45+
46+
--)
47+
shift; break;;
48+
-*)
49+
usage "Unknown option: $1 !";;
50+
*)
51+
break;;
52+
esac
53+
done
54+
55+
# Converts line endings to \n in content, this is what the snippet API
56+
# wants.
57+
_content=$(printf %s\\n "$_content"|sed -e 's/"/\\"/g' -e 's/$/\\n/g'|tr -d '\n')
58+
# Create a temporary file to host "almost JSON" content and the final JSON
59+
# file. We do this because we need to get rid of the last , at the end of
60+
# the last line of the JSON that we output, as we can't control which
61+
# options were set from the outside.
62+
_fpath=$(mktemp)
63+
_jsonpath=$(mktemp)
64+
# Print fields that were set from the outside in turns to the temporary,
65+
# almost JSON, _fpath
66+
printf '{\n' > "$_fpath"
67+
printf %s\\n "$_fields" | grep -q t && printf '"title":"%s",\n' "$_title" >> "$_fpath"
68+
printf %s\\n "$_fields" | grep -q d && printf '"description":"%s",\n' "$_description" >> "$_fpath"
69+
printf %s\\n "$_fields" | grep -q f && printf '"file_name":"%s",\n' "$_filename" >> "$_fpath"
70+
printf %s\\n "$_fields" | grep -q c && printf '"content":"%s",\n' "$_content" >> "$_fpath"
71+
printf %s\\n "$_fields" | grep -q v && printf '"visibility":"%s",\n' "$_visibility" >> "$_fpath"
72+
# Remove last , from last line of _fpath to create beginning of real JSON
73+
# file at _jsonpath
74+
head -n -1 "$_fpath" > "$_jsonpath"
75+
tail -n 1 "$_fpath" | sed -E 's/,$//' >> "$_jsonpath"
76+
# Close the JSON file and remove the temporary _fpath. We are done!
77+
printf '}' >> "$_jsonpath"
78+
rm -f "$_fpath"
79+
printf %s\\n "$_jsonpath"
80+
yush_trace "Generated: $(cat "$_jsonpath")"
81+
}
82+
83+
84+
snippet_create() {
85+
_json=$(_snippet_jsongen "$@")
86+
res=$(snippet_curl "" \
87+
--header "Content-Type: application/json" \
88+
--request POST \
89+
-d @"$_json")
90+
if printf %s\\n "$res" | grep -qE '"error"\s*:\s*"'; then
91+
if printf %s\\n "$res" | yush_json | grep -q '/error_description'; then
92+
yush_error "$(printf %s\\n "$res" | yush_json | grep '/error_description' | cut -d " " -f 3-)"
93+
else
94+
yush_error "$(printf %s\\n "$res" | yush_json | grep '/error' | cut -d " " -f 3-)"
95+
fi
96+
exit 1
97+
else
98+
printf %s\\n "$res" | yush_json | grep -E "^/id " | awk '{print $3}'
99+
fi
100+
rm -f "$_json"
101+
}
102+
103+
snippet_update() {
104+
_json=$(_snippet_jsongen "$@")
105+
if [ "$#" = "0" ]; then
106+
yush_warn "You have to specify a snippet ID"
107+
else
108+
res=$(snippet_curl "$(eval echo "\$$#")" \
109+
--header "Content-Type: application/json" \
110+
--request PUT \
111+
-d @"$_json")
112+
if printf %s\\n "$res" | grep -qE '"error"\s*:\s*"'; then
113+
if printf %s\\n "$res" | yush_json | grep -q '/error_description'; then
114+
yush_error "$(printf %s\\n "$res" | yush_json | grep '/error_description' | cut -d " " -f 3-)"
115+
else
116+
yush_error "$(printf %s\\n "$res" | yush_json | grep '/error' | cut -d " " -f 3-)"
117+
fi
118+
exit 1
119+
else
120+
printf %s\\n "$res" | yush_json | grep -E "^/id " | awk '{print $3}'
121+
fi
122+
fi
123+
rm -f "$_json"
124+
}
125+
126+
snippet_delete() {
127+
if [ "$#" = "0" ]; then
128+
yush_warn "You have to specify a snippet ID"
129+
else
130+
snippet_curl "$1" --request "DELETE"
131+
fi
132+
}

snippet

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#!/usr/bin/env sh
2+
3+
4+
# Find yu.sh and load modules. We support several locations to ease
5+
# installation.
6+
SCRIPT_DIR=$( cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P )
7+
### AMLG_START
8+
LIB_DIR=
9+
for _lib in lib libexec ../lib; do
10+
[ -z "$LIB_DIR" ] && [ -d "$SCRIPT_DIR/$_lib" ] && LIB_DIR="$SCRIPT_DIR/$_lib"
11+
done
12+
[ -z "$LIB_DIR" ] && echo "Cannot find library directory!" >&2 && exit 1
13+
YUSH_DIR="$LIB_DIR/yu.sh"
14+
! [ -d "$YUSH_DIR" ] && echo "canno find yu.sh directory!" >&2 && exit 1
15+
### AMLG_END
16+
17+
### AMLG_START ./lib/yu.sh/log.sh ./lib/yu.sh/json.sh
18+
# shellcheck source=./lib/yu.sh/log.sh disable=SC1091
19+
. "$YUSH_DIR/log.sh"
20+
# shellcheck source=./lib/yu.sh/json.sh disable=SC1091
21+
. "$YUSH_DIR/json.sh"
22+
### AMLG_END
23+
24+
# Shell sanity
25+
set -eu
26+
27+
### AMLG_START ./lib/*.sh
28+
# Source in all relevant modules. This is where most of the "stuff" will occur.
29+
_modules=$(find "${LIB_DIR}" -maxdepth 1 -mindepth 1 \( -name '*.sh' -a ! -name 'x-*.sh' \) -exec basename '{}' \; | sed -e 's/\.sh$//g' | tr '\n' ' ')
30+
for module in $_modules; do
31+
module_path="${LIB_DIR}/${module}.sh"
32+
if [ -f "$module_path" ]; then
33+
yush_trace "Loading $module from $module_path"
34+
# shellcheck disable=SC1090
35+
. "$module_path"
36+
fi
37+
done
38+
### AMLG_END
39+
40+
# Print usage on stderr and exit
41+
usage() {
42+
[ -n "$1" ] && echo "$1" >&2
43+
exitcode="${2:-1}"
44+
cat << USAGE >&2
45+
Yet to be documented
46+
USAGE
47+
exit "$exitcode"
48+
}
49+
50+
# Root of the gitlab API endpoint
51+
GITLAB_HOST=${GITLAB_HOST:-gitlab.com}
52+
GITLAB_ROOT=${GITLAB_ROOT:-}
53+
54+
# Token for accessing the API
55+
GITLAB_TOKEN=${GITLAB_TOKEN:-}
56+
57+
# Project to access/modify snippets for
58+
GITLAB_PROJECT=${GITLAB_PROJECT:-}
59+
60+
61+
while [ $# -gt 0 ]; do
62+
case "$1" in
63+
-g | --gitlab)
64+
GITLAB_HOST=$2; shift 2;;
65+
--gitlab=*)
66+
GITLAB_HOST="${1#*=}"; shift 1;;
67+
68+
-r | --root)
69+
GITLAB_ROOT=$2; shift 2;;
70+
--root=*)
71+
GITLAB_ROOT="${1#*=}"; shift 1;;
72+
73+
-p | --project)
74+
GITLAB_PROJECT=$2; shift 2;;
75+
--project=*)
76+
GITLAB_PROJECT="${1#*=}"; shift 1;;
77+
78+
-t | --token)
79+
GITLAB_TOKEN=$2; shift 2;;
80+
--token=*)
81+
GITLAB_TOKEN="${1#*=}"; shift 1;;
82+
83+
-v | --verbose)
84+
# shellcheck disable=SC2034
85+
YUSH_LOG_LEVEL=$2; shift 2;;
86+
--verbose=*)
87+
# shellcheck disable=SC2034
88+
YUSH_LOG_LEVEL="${1#*=}"; shift 1;;
89+
90+
--non-interactive | --no-colour | --no-color)
91+
# shellcheck disable=SC2034
92+
YUSH_LOG_COLOUR=0; shift 1;;
93+
94+
-h | --help)
95+
usage "" 0;;
96+
97+
--)
98+
shift; break;;
99+
-*)
100+
usage "Unknown option: $1 !";;
101+
*)
102+
break;;
103+
esac
104+
done
105+
106+
abort() {
107+
yush_error "$1"
108+
exit 1
109+
}
110+
111+
# Converts string passed as a parameter to its URL encoded representation. This
112+
# is heavily modified from https://stackoverflow.com/a/10660730
113+
_snippet_urlenc() {
114+
_encoded=
115+
for _pos in $(seq 1 "${#1}"); do
116+
_c=$(printf %s\\n "$1" | cut -c "$_pos")
117+
case "$_c" in
118+
[_.~a-zA-Z0-9-]) _encoded="${_encoded}${_c}" ;;
119+
# The single quote before $_c below converts $_c to its numeric
120+
# value: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html#tag_20_94_13
121+
*) _encoded="${_encoded}$(printf '%%%02x' "'$_c")";;
122+
esac
123+
done
124+
printf %s\\n "$_encoded"
125+
}
126+
127+
snippet_curl() {
128+
_path=
129+
if [ "$#" -gt "0" ]; then
130+
_path=${1:-}
131+
shift
132+
fi
133+
yush_debug "Curling ${GITLAB_ROOT%/}/projects/${GITLAB_PROJECT}/snippets/${_path%/} with $*"
134+
curl -sSL \
135+
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
136+
"$@" \
137+
"${GITLAB_ROOT%/}/projects/${GITLAB_PROJECT}/snippets/${_path%/}"
138+
}
139+
140+
# Ensure curl is installed
141+
! command -v curl >/dev/null && abort "This script requires curl installed!"
142+
143+
# Construct a root from just the gitlab host (and port) whenever none was
144+
# specified. This is the default behaviour.
145+
[ -z "$GITLAB_ROOT" ] && GITLAB_ROOT="https://${GITLAB_HOST}/api/v4"
146+
147+
if ! printf %s\\n "$GITLAB_PROJECT" | grep -qE '^[0-9]+$'; then
148+
GITLAB_PROJECT=$(_snippet_urlenc "$GITLAB_PROJECT")
149+
yush_debug "Converted non-numeric project name to URL encoded: $GITLAB_PROJECT"
150+
fi
151+
152+
# The default command is to list existing snippets by ID
153+
if [ "$#" = "0" ]; then
154+
cmd=list
155+
else
156+
cmd=$(printf %s\\n "$1" | tr '[:upper:]' '[:lower:]')
157+
shift
158+
fi
159+
160+
case "$cmd" in
161+
list)
162+
snippet_list "$@";;
163+
get|read)
164+
snippet_get "$@";;
165+
details)
166+
snippet_details "$@";;
167+
create|add)
168+
snippet_create "$@";;
169+
update|change)
170+
snippet_update "$@";;
171+
delete|remove)
172+
snippet_delete "$@";;
173+
*)
174+
usage "Unknown command: $cmd !";;
175+
esac

0 commit comments

Comments
 (0)