-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzsh-ghf.zsh
217 lines (179 loc) · 5.07 KB
/
zsh-ghf.zsh
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
216
217
[[ -e ~/.ghfrc ]] && source ~/.ghfrc
0=${(%):-%N}
source ${0:A:h}/app/zsh-cache.zsh
source ${0:A:h}/app/zsh-log.zsh
source ${0:A:h}/app/zsh-oss.zsh
typeset -g ZSH_GHF_START_DAEMON="${0:A:h}"/app/zsh-start-daemon.zsh
typeset -g ZSH_GHF_CACHE="${0:A:h}"/app/zsh-cache.zsh
typeset -g ZSH_GHF_VERSION=$(<"${0:A:h}"/.version)
typeset -g ZSH_GHF_REVISION=$(<"${0:A:h}"/.revision-hash)
typeset -g ZSH_GHF_DEBUG=false
pre-check() {
if [[ $ZSH_GHF_API_URL == "" ]]; then
echo 'please configure $ZSH_GHF_API_URL in ~/.ghfrc'
echo 'TL;DR https://github.com/soraliu/zsh-ghf'
return 1
fi
if [[ $ZSH_GHF_REPO_NAME_FRAGMENT == "" ]]; then
echo 'please configure $ZSH_GHF_REPO_NAME_FRAGMENT in ~/.ghfrc'
echo 'TL;DR https://github.com/soraliu/zsh-ghf'
return 1
fi
if ! command -v jq 1>/dev/null 2>&1; then
echo 'please install jq'
echo 'TL;DR https://github.com/soraliu/zsh-ghf'
return 1
fi
if ! command -v curl 1>/dev/null 2>&1; then
echo 'please install curl'
echo 'TL;DR https://github.com/soraliu/zsh-ghf'
return 1
fi
}
ghf() {
set -e
pre-check
usage() {
echo "usage:
[-t | --tag]
[-c | --is_code [if wrap comment with \`\`\`]]
[-f | --is_from_clipboard]
[-h]"
}
if [[ ${ZSH_GHF_REPO_NAME} == "" ]]; then
ZSH_GHF_REPO_NAME=${ZSH_GHF_REPO_NAME_FRAGMENT}
fi
comment=
tags=
api=${ZSH_GHF_API_URL}/${ZSH_GHF_REPO_NAME}/issues
today=$(date +"%Y-%m-%d")
while [ "$1" != "" ]; do
case $1 in
-t | --tag ) shift
if ! echo $tags | grep "\`$1\`" 1>/dev/null 2>&1; then
tags="$tags\`$1\` "
fi
;;
-c | --code )
is_code=true
;;
-f | --from_clipboard )
is_from_clipboard=true
;;
-h | --help )
usage
return
;;
* )
if [[ $comment == "" ]]; then
comment="$1"
else
comment="$comment
$1"
fi
esac
shift
done
if [[ $is_from_clipboard == true ]]; then
# 1. check if image
# yes -> upload to oss & get link & assign to comment
# no -> assign clipboard content to comment
img_temp=$(mktemp)
img_name="$(uuidgen)"
if pngpaste "${img_temp}.png" 2>/dev/null; then
sips -s format jpeg "${img_temp}.png" --out "${img_temp}.jpg" 1>/dev/null 2>&1
url=$(upload_file_to_aliyun_oss -s "${img_temp}.jpg" -d "${img_name}.jpg")
comment="![img](${url})"
else
comment="$(pbpaste)"
if command -v wrapper 1>/dev/null 2>&1; then
comment=$(wrapper $comment)
fi
if [[ $is_code == true ]]; then
comment="\`\`\`\n$comment\n\`\`\`"
fi
fi
rm -f "${img_temp}.png"
rm -f "${img_temp}.jpg"
else
if command -v wrapper 1>/dev/null 2>&1; then
comment=$(wrapper $comment)
fi
# if is_code, wrap comment with ```
if [[ $is_code == true ]]; then
comment="\`\`\`\n$comment\n\`\`\`"
fi
fi
if [[ ${ZSH_GHF_DEBUG} == true ]]; then
echo api: $api
echo today: $today
fi
# get pr number if exist
id=$(curl -s $api | jq ".[] | select(.title == \"${today}\") | .number")
if [[ "" == "$id" ]]; then
# create issue
id=$(curl -s -X POST --data '{"title": "'$today'"}' $api | jq '.number')
fi
if [[ ${ZSH_GHF_DEBUG} == true ]]; then
echo issue id: $id
fi
if [[ $tags == "" ]]; then
body="${comment}"
else
body="${tags}
${comment}"
fi
echo $(curl -s -X POST --data '{"body": '"$(jq -aRs . <<< $(echo ${body}))"'}' $api/$id/comments | jq -r '.html_url')
echo "$body"
# refresh cache
issue_to_cache -r ${ZSH_GHF_REPO_NAME} -n ${id} > ${ZSH_GHF_PATH_TO_CACHE_ROOT}/${ZSH_GHF_REPO_NAME}/${today}
echo ${ZSH_GHF_PATH_TO_CACHE_ROOT}/${ZSH_GHF_REPO_NAME}/${today} 1>>/tmp/ghf-daemon.stdout
echo ${id} 1>>/tmp/ghf-daemon.stdout
}
ghf-list() {
set -e
#
# list=$(<${ZSH_GHF_PATH_TO_CACHE_ROOT}/.list)
#
# echo ${list}
ghf-log -c 'call ghf-list'
path_to_root=$(realpath "$0")
if [[ -e ${ZSH_GHF_PATH_TO_CACHE_ROOT}/.list ]]; then
cat ${ZSH_GHF_PATH_TO_CACHE_ROOT}/.list
nohup zsh -c '(
source ~/.zshrc
data=$(get_issue_list \
| jq ".[] |= {title, subtitle: .title, autocomplete: .title, arg: .html_url}" \
| jq "{items: .}")
[[ ${data} != "" ]] && (echo "${data}" > ${ZSH_GHF_PATH_TO_CACHE_ROOT}/.list)
ghf-log -c "[ghf-list] sync data from github"
)' >> /tmp/ghf-list.stdout 2>&1 &
return
fi
get_issue_list \
| jq '.[] |= {title, subtitle: .title, autocomplete: .title, arg: .html_url}' \
| jq '{items: .}' | tee ${ZSH_GHF_PATH_TO_CACHE_ROOT}/.list
path_to_root=
}
dict() {
wrapper() {
trans -no-ansi $1
}
ZSH_GHF_REPO_NAME=${ZSH_GHF_REPO_NAME_LANG_LEARNING}
ghf -t 'language learning' -t 'english' -c $@
}
tozh() {
wrapper() {
trans -no-ansi en:zh $1
}
ZSH_GHF_REPO_NAME=${ZSH_GHF_REPO_NAME_LANG_LEARNING}
ghf -t 'language learning' -t 'english' -c $@
}
toen() {
wrapper() {
trans -no-ansi zh:en $1
}
ZSH_GHF_REPO_NAME=${ZSH_GHF_REPO_NAME_LANG_LEARNING}
ghf -t 'language learning' -t 'english' -c $@
}
alias push="ghf"