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