-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ghf.bash
134 lines (119 loc) · 3.12 KB
/
ghf.bash
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
if [[ "${_GHF_SOURCE_DIR}" == "" ]]; then
_GHF_SOURCE_DIR="$( cd "$( dirname "$0" )" && pwd )"
fi
source "${_GHF_SOURCE_DIR}/ghf-view-url.bash"
source "${_GHF_SOURCE_DIR}/bitly.bash"
function _ghfFzf() {
local COMMAND="$1"
local SUBCOMMAND="$2"
shift 2
# Try list candidates
local STDERR="$( mktemp )"
local CANDIDATES="$( "$COMMAND" "$SUBCOMMAND" list "$@" 2> "$STDERR" )"
if [[ ! "$( command cat "$STDERR" )" == "" ]]; then
"$COMMAND" "$SUBCOMMAND" list "$@"
return $?
fi
# Choose one with fzf
local CHOICE="$(
set -o pipefail
command echo "$CANDIDATES" \
| command fzf --preview "command echo {} \
| command grep -oE '^\S+' \
| command xargs '$COMMAND' '$SUBCOMMAND' view" \
| command grep -oE '^\S+'
)"
# Remove MR prefix for glab
if [[ "$SUBCOMMAND" == "mr" ]]; then
command echo "${CHOICE#\!}"
return 0
fi
command echo "$CHOICE"
}
function _ghfFzfView() {
local CHOICE="$( _ghfFzf "$@" )"
if [[ $CHOICE == "" ]]; then
echo "No $2 has chosen to view." >&2
return 1
fi
if ! test -t; then
echo "$CHOICE"
return $?
fi
case "${_GH_FZF_VIEWER:-web}" in
"web" ) # run e.g. gh issue view 1 --web
"$1" "$2" view --web "$CHOICE";;
"text" ) "$1" "$2" view "$CHOICE";;
"id" ) echo "$CHOICE";;
"url" ) echo "$( _ghf_view_url "$CHOICE" "$@" )";;
"short_url" )
local PATH_TOKEN="$( _bitly_path )"
if [[ ! -f "$PATH_TOKEN" ]] || \
[[ "$(command cat "$PATH_TOKEN" )" == "" ]]
then
bitly login
fi
echo "$( _ghf_view_url "$CHOICE" "$@" )";;
esac
}
function _ghfWrapper() {
local COMMAND="$1"
shift
if [[ "$2" == "fzf" ]]; then
local SUBCOMMAND="$1"
shift 2
_ghfFzf "$COMMAND" "$SUBCOMMAND" "$@"
elif [[ "$2" == "--help" ]] || [[ "$2" =~ ^[^-] ]] ; then
"$COMMAND" "$@"
else
_ghfFzfView "$COMMAND" "$@"
fi
}
function _ghfIsAliasOrExtensions() {
PATTERN="^gh-${2}\$"
local ALIASES="$(
"$1" alias list | command grep -oE "^\S+" | command sed -E -e 's/:$//g'
)"
# https://github.com/cli/cli/blob/029d49f3b38ebd54cd84b23732bb7efabfce4896/internal/config/config_file.go#L78-L90
local EXTENSIONS="$(
case "$( basename "$1" )" in
"gh")
command ls "${XDG_DATA_HOME:-$HOME/.local/share}/gh/extensions";;
"glab" ) echo "";;
esac
)"
if echo -e "${ALIASES}\n${EXTENSIONS}" | command grep -q "$PATTERN"; then
echo true
else
echo false
fi
}
function _ghf() {
local COMMAND="$(
unalias "$1" &> /dev/null
command -v "$1"
)" # typically `gh`
shift
# stdin goes to original gh
if [ -p /dev/stdin ]; then
"$COMMAND" "$@" "$(command cat -)"
elif [[ $# -eq 0 ]]; then
"$COMMAND"
elif [[ "$( _ghfIsAliasOrExtensions "$COMMAND" "$@" )" == true ]]; then
"$COMMAND" "$@"
elif "$COMMAND" "$1" list --help &> /dev/null; then
_ghfWrapper "$COMMAND" "$@"
else
"$COMMAND" "$@"
fi
}
if command -v gh > /dev/null; then
function ghf() {
_ghf gh "$@"
}
fi
if command -v glab > /dev/null; then
function glabf() {
_ghf glab "$@"
}
fi