-
Notifications
You must be signed in to change notification settings - Fork 40
/
ysh
executable file
·163 lines (163 loc) · 6.78 KB
/
ysh
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
#! /bin/bash
# YSH from https://github.com/azohra/yaml.sh
YSH_version='0.1.4'
# Modified version of YSH. join_stack uses _ instead of . and adds y_ as a prefix to every line.
YAML_AWK_PARSER='
function raise(msg) { print msg > "/dev/stderr"; if (force_complete) { exit_status = 1; } else { exit 1; };};
function level() { match($0, /^[[:space:]]*/); if (RLENGTH % 2 != 0) { raise("Bad indentation on line "NR". Number of spaces uneven."); }; return RLENGTH / 2;};
function join_stack(depth) { r = sprintf("%"block_level"s",""); gsub(/ /,"-",r); for (i = 0; i <= depth; i++) { r = r "_" stack[i]; }; sub(/^\_/, "y_", r); return r;};
function safe_split(input, output) { split(input, chars, ""); in_quote = 1; acc = ""; count = 0; for (i=0; i <= length(input); i++) { c=chars[i]; if (c=="\"") { in_quote = (in_quote + 1) % 2; }; if (c=="," && in_quote) { output[count++] = acc; acc = ""; } else { acc = acc c; }; }; output[count++] = acc;};
function remove_sur_quotes(target) { sub(/^[[:space:]]*\"/, "", target) ; sub(/\"[[:space:]]*$/, "", target) ; return target;};
function check_started() { if (started == 0) { raise("Keys must be added before line "NR); };};
/^---/ { if (started==0) { started=1; } else { block_level++; }; next;};
/^[[:space:]]*[^[:space:]]+:/ { started=1; depth=level(); key=$1; sub(/:.*$/, "", key); stack[depth] = key;};
/^[[:space:]]*[^[:space:]]+:[[:space:]]+[^[:space:]]+/ { started=1; depth=level(); val=$0; sub(/^[[:space:]]*[^[:space:]]+:[[:space:]]+/, "", val); val = remove_sur_quotes(val); print join_stack(depth) "=\"" val "\""; next;};
/^[[:space:]]*\-/ { check_started(); depth=level(); stack_key=join_stack(depth-1); indx=list_counter[stack_key]++; stack[depth]="[" indx "]";};
/^[[:space:]]*-[[:space:]]+\{.*\}[[:space:]]*$/ { check_started(); line=$0; sub(/^[[:space:]]*-[[:space:]]+\{/, "", line); sub(/\}[[:space:]]*$/, "", line); safe_split(line, entries); for (i in entries) { key=entries[i]; val=entries[i]; sub(/^[[:space:]]*/, "", key); sub(/:.*$/, "", key); sub(/^[[:space:]]*[^[:space:]]+:[[:space:]]+\"?/, "", val) ; sub(/\"?[[:space:]]*$/, "", val) ; val = remove_sur_quotes(val); print join_stack(depth) "." key "=\"" val "\""; }; delete entries; next;};
/^[[:space:]]*-[[:space:]][^[:space:]]+:/ { check_started(); depth++; key=$0; sub(/^[[:space:]]*-[[:space:]]/, "", key); sub(/:.*$/, "", key); stack[depth]=key;};
/^[[:space:]]*-[[:space:]][^[:space:]]+:[[:space:]]+[^[:space:]]+/ { check_started(); val=$0; sub(/^[[:space:]]*-[[:space:]][^[:space:]]+:[[:space:]]+/, "", val); val = remove_sur_quotes(val); print join_stack(depth) "=\"" val "\""; next;};
/^[[:space:]]*-[[:space:]]+[^[:space:]]+/ { check_started(); val=$0; sub(/^[[:space:]]*-[[:space:]]+/, "", val); sub(/[[:space:]]*$/, "", val); val = remove_sur_quotes(val); print join_stack(depth) "=\"" val "\""; next;};
/^[[:space:]]*[^[:space:]]+:[[:space:]]*$/ {next};
/^[[:space:]]*-[[:space:]]+[^[:space:]]+:[[:space:]]*$/ {next};
/^[[:space:]]*$/ { next };
0
'
YSH_parse() {
awk "${YAML_AWK_PARSER}" "${1}"
}
YSH_parse_sdin() {
awk "${YAML_AWK_PARSER}"
}
YSH_query() {
grep -E "^${2}" <<< "${1}" | sed -E "s/^${2}[\\.=]?//"
}
YSH_safe_query() {
grep -E "^${2}=\".*\"$" <<< "${1}" | sed -E "s/^${2}=\"//" | sed -E "s/\"$//"
}
YSH_sub() {
grep -E "^${2}[^=]" <<< "${1}" | sed "s/^$2\\.//"
}
YSH_list() {
YSH_sub "${1}" "${2}" | grep -E "^\\[[0-9]+\\]"
}
YSH_list_values() {
YSH_sub "${1}" "${2}" | grep -E "^\\[[0-9]+\\]=" | sed "s/^\\[[0-9]\\]=//" | sed "s/[(^\")(\"$)]//g"
}
YSH_count() {
YSH_sub "${1}" "${2}" | grep -oE "^\\[[0-9]+\\]" | uniq | wc -l
}
YSH_index() {
YSH_query "${1}" "\\[${2}\\]"
}
YSH_safe_index() {
YSH_safe_query "${1}" "\\[${2}\\]"
}
YSH_safe_index() {
YSH_safe_query "${1}" "\\[${2}\\]"
}
YSH_tops() {
sed -E "s/[\\[\\.=].*$//" <<< "${1}" | uniq
}
YSH_next_block() {
grep -E "^-.*" <<< "${1}" | sed -E "s/^-\\.?//g"
}
YSH_usage() {
echo ""
echo "Usage: ysh [-fT input] [queries]"
echo ""
echo "input:"
echo " -f, --file <file_name> parse file"
echo " -T, --transpiled <file_name> use pre-transpiled file"
echo ""
echo "queries:"
echo " -q, --query <query> generic query"
echo " -Q, --query-val <query> safe query. Guarentees a value."
echo " -s, --sub <query> sub structure. No values."
echo " -l, --list <query> query for a list"
echo " -c, --count <query> count length of list element"
echo " -i, --index <i> array access by index"
echo " -I, --index-val <i> safe array access by index. Guarentees a value."
echo " -t, --tops top level children keys of structure"
echo " -n, --next move to next block"
echo " -h, --help prints this message"
echo ""
echo "And even more at https://docs.yaml.sh"
}
ysh() {
local YSH_RAW_STRING=""
case "$1" in
-v|--version)
echo "v${YSH_version}" && exit 0
;;
-h|--help)
YSH_usage
;;
-f|--file)
YSH_RAW_STRING="$(YSH_parse "${2}")"
shift; shift
;;
-T|--transpiled)
YSH_RAW_STRING="${2}"
shift; shift
;;
*)
YSH_RAW_STRING="$(YSH_parse_sdin)"
;;
esac
while [ $# -gt 0 ] ; do
case "$1" in
-q|--query)
YSH_RAW_STRING="$(YSH_query "${YSH_RAW_STRING}" "${2}")"
shift
;;
-Q|--query-val)
YSH_RAW_STRING="$(YSH_safe_query "${YSH_RAW_STRING}" "${2}")"
shift
;;
-c|--count)
YSH_RAW_STRING="$(YSH_count "${YSH_RAW_STRING}" "${2}")"
shift
;;
-l|--list)
YSH_RAW_STRING="$(YSH_list "${YSH_RAW_STRING}" "${2}")"
shift
;;
-L|--list-val)
YSH_RAW_STRING="$(YSH_list_values "${YSH_RAW_STRING}" "${2}")"
shift
;;
-i|--index)
YSH_RAW_STRING="$(YSH_index "${YSH_RAW_STRING}" "${2}")"
shift
;;
-I|--index-val)
YSH_RAW_STRING="$(YSH_safe_index "${YSH_RAW_STRING}" "${2}")"
shift
;;
-s|--sub)
YSH_RAW_STRING="$(YSH_sub "${YSH_RAW_STRING}" "${2}")"
shift
;;
-n|--next)
YSH_RAW_STRING="$(YSH_next_block "${YSH_RAW_STRING}")"
;;
-t|--tops)
YSH_RAW_STRING="$(YSH_tops "${YSH_RAW_STRING}")"
;;
-*)
echo "Unknown option: ${1} Refer to -h for help."
exit 1
;;
*)
echo "Unknown Ussage!" > /dev/stdout
exit 1
;;
esac
shift
done
if [[ $# -eq 0 ]]; then echo "${YSH_RAW_STRING}"; fi
}
if [[ YSH_LIB -ne 1 ]]; then
if [[ $# -eq 0 ]] ; then YSH_usage; exit 1; fi
ysh "$@";
fi