-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlib.sh
More file actions
195 lines (176 loc) · 5.59 KB
/
Copy pathlib.sh
File metadata and controls
195 lines (176 loc) · 5.59 KB
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
function config_set_view {
local dirname="$1"
local view_command="$2"
local err="$(stat "$dirname/config.json" > /dev/null)"
if [ $? -ne 0 ]; then
echo "$err"
exit 1
fi
local JQ_PREFIX=".viewCommand = \"$view_command\""
err="$(jq "$JQ_PREFIX" "$dirname/config.json" > "$dirname/config.json.tmp")"
if [ $? -ne 0 ]; then
echo "$err"
exit 1
fi
err="$(mv "$dirname/config.json.tmp" "$dirname/config.json")"
if [ $? -ne 0 ]; then
echo "$err"
exit 1
fi
}
function config_set_preview {
local dirname="$1"
local preview_command="$2"
local err="$(stat "$dirname/config.json" > /dev/null)"
if [ $? -ne 0 ]; then
echo "$err"
exit 1
fi
local JQ_PREFIX=".previewCommand = \"$preview_command\""
err="$(jq "$JQ_PREFIX" "$dirname/config.json" > "$dirname/config.json.tmp")"
if [ $? -ne 0 ]; then
echo "$err"
exit 1
fi
err="$(mv "$dirname/config.json.tmp" "$dirname/config.json")"
if [ $? -ne 0 ]; then
echo "$err"
exit 1
fi
}
function config_get_view {
local dirname="$1"
local err="$(stat "$dirname/config.json" > /dev/null)"
if [ $? -ne 0 ]; then
echo "$err"
exit 1
fi
local view_command="$(jq -r '.viewCommand' "$dirname/config.json")"
echo "$view_command"
}
function config_get_preview {
local dirname="$1"
local err="$(stat "$dirname/config.json" > /dev/null)"
if [ $? -ne 0 ]; then
echo "$err"
exit 1
fi
local preview_command="$(jq -r '.previewCommand' "$dirname/config.json")"
echo "$preview_command"
}
function get_starred_repos {
local dirname="$1"
local username="$2"
local queryPath="$dirname/graphql/queries/get-starred-repos.graphql"
local response="$(exec gh api graphql \
-f login="$username" \
-f query="$(cat $queryPath)" \
--jq '[.data.user.starredRepositories.edges[] | .node]'
)"
# define table data
local table_headers=$(echo -e "REPO\tDESCRIPTION\tURL")
local table_data="$(echo "$response" | \
jq --color-output \
-r '.[] | "\(.nameWithOwner)\t\"\(.description)\"\t\(.url)\"\t\(.id)"'
)"
local table="$(echo -e "${table_headers}\n${table_data}")"
local readme_query=$(cat "$dirname/graphql/queries/get-readme.graphql")
README_QUERY_PREFIX="gh api graphql \
-f query='$readme_query' \
--jq '.data.node | if (.masterReadme.text == null or .masterReadme.text == \"\") then .mainReadme.text else .masterReadme.text end' \
-f nodeId="
SEP=" | "
WEB_BROWSER_CMD="gh repo view -w "
PREVIEW_CMD="$(config_get_preview "$dirname")"
BECOME_CMD="$(config_get_view "$dirname")"
echo "$table" | \
sed $'s/ /\u00a0/g' | \
column -t -s $'\t' | \
fzf --ansi \
--nth=1 \
--header-lines=1 \
--delimiter='\s+'\
--with-nth=1..4\
--preview-label='README'\
--bind "?:preview($README_QUERY_PREFIX{-1}$SEP$PREVIEW_CMD)"\
--bind "_:close"\
--bind "ctrl-v:become($README_QUERY_PREFIX{-1}$SEP$PREVIEW_CMD)"\
--bind "ctrl-t:become($WEB_BROWSER_CMD{1})"\
--bind "enter:become($README_QUERY_PREFIX{-1}$SEP$BECOME_CMD)"\
--bind "enter:+become(echo {})"
}
function get_user_lists {
local dirname="$1"
local username="$2"
local queryPath="$dirname/graphql/queries/get-lists-with-ids.graphql"
local pageSize=100 # max page size from GitHub GraphQL API
local response="$(exec gh api graphql \
-f login="$username" \
-f query="$(cat $queryPath)" \
--jq '.data.viewer.lists.nodes' #TODO: Also destructure pagination cursors
)"
local table_headers=$(echo -e "LIST\tLAST-ADDED-TO")
local table_data="$(\
echo "$response" | \
jq --color-output \
-r '.[] | "\(.name)\t\(.lastAddedAt | split("T")[0])\t\(.id)"'
)"
local table="$(echo -e "${table_headers}\n${table_data}")"
local id="$(echo "$table" | \
sed $'s/ /\u00a0/g' | \
column -t -s $'\t' | \
fzf --ansi \
--nth=1 \
--header-lines=1 \
--delimiter='\s+'\
--with-nth=1..2\
--preview-label='README'\
--bind "enter:become(echo {-1})")"
if [[ -z "$id" ]]; then
echo "No list selected" # TODO: REMOVE WHEN OBSELETE
exit 0
fi
get_repos_by_userlist_id "$dirname" "$username" "$id"
}
function get_repos_by_userlist_id {
local dirname="$1"
local username="$2"
local id="$3"
local repo_query_path="$dirname/graphql/queries/get-repos-by-userlist-id.graphql"
local response=$(exec gh api graphql \
-f login="$username" \
-f nodeId="$id" \
-f query="$(cat $repo_query_path)" \
--jq '.data.node.items.nodes' # TODO: Also destructure pagination cursors
)
local table_headers=$(echo -e "REPO\tDESCRIPTION\tURL")
local table_data="$(\
echo "$response" | \
jq --color-output \
-r '.[] | "\(.nameWithOwner)\t\(.description)\t\(.url)\t\(.id)"'\
)"
local table="$(echo -e "${table_headers}\n${table_data}")"
local readme_query=$(cat "$dirname/graphql/queries/get-readme.graphql")
README_QUERY_PREFIX="gh api graphql \
-f query='$readme_query' \
--jq '.data.node | if (.masterReadme.text == null or .masterReadme.text == \"\") then .mainReadme.text else .masterReadme.text end' \
-f nodeId="
SEP=" | "
PREVIEW_CMD="$(config_get_preview "$dirname")"
BECOME_CMD="$(config_get_view "$dirname")"
WEB_BROWSER_CMD="gh repo view -w "
echo "$table" | \
sed $'s/ /\u00a0/g' | \
column -t -s $'\t' | \
fzf --ansi \
--nth=1 \
--header-lines=1 \
--delimiter='\s+'\
--with-nth=1..3\
--preview-label='README'\
--bind "?:preview($README_QUERY_PREFIX{-1}$SEP$PREVIEW_CMD)"\
--bind "_:close"\
--bind "ctrl-t:become($WEB_BROWSER_CMD{1})"\
--bind "ctrl-v:become($README_QUERY_PREFIX{-1}$SEP$PREVIEW_CMD)"\
--bind "enter:become($README_QUERY_PREFIX{-1}$SEP$BECOME_CMD)"
}