-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript
More file actions
executable file
·113 lines (90 loc) · 2.59 KB
/
Copy pathscript
File metadata and controls
executable file
·113 lines (90 loc) · 2.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
#!/bin/bash
readonly SUBCOMMAND_PREFIX="subcommand"
readonly DESCRIPTION_PREFIX="desc"
readonly HELP_PREFIX="help"
function _main() {
readonly SUBCOMMAND="$(echo "$1" | tr "-" "_")"
if [[ -z "$SUBCOMMAND" ]] ; then
_usage
exit
fi
if ! type "${SUBCOMMAND_PREFIX}_${SUBCOMMAND}" &>/dev/null ; then
_usage
exit 1
fi
shift
eval "${SUBCOMMAND_PREFIX}_${SUBCOMMAND} $@"
exit $!
}
function _usage() {
cat <<USAGE >&2
./script - one script to rule them all
subcommands:
$(_subcommands)
USAGE
}
function _subcommands() {
for cmd in $(_cmd_list) ; do
printf " $cmd" | tr "_" "-"
if type "${DESCRIPTION_PREFIX}_${cmd}" &>/dev/null ; then
printf " $("${DESCRIPTION_PREFIX}_${cmd}")"
fi
echo
done
}
function _cmd_list() {
compgen -A function | \
grep -vE "^_.*" | \
grep "${SUBCOMMAND_PREFIX}" | \
awk "/^${SUBCOMMAND_PREFIX}_\./{print;next}{sub(/^${SUBCOMMAND_PREFIX}_/,\"\",\$1);print}"
}
# help
function desc_help() { echo "<subcommand> \t- print help text for subcommand" ; }
function subcommand_help() {
local subcommand="$(echo "$1" | tr "-" "_")"
if type "${HELP_PREFIX}_${subcommand}" &>/dev/null ; then
eval "${HELP_PREFIX}_${subcommand}"
else
echo "no help text available for subcommand \"$subcommand\""
fi
}
# -h
function desc__h() { echo "\t\t\t- display this message and exit" ; }
function help__h() { cat <<HELP
This is example help text for the "-h/--help" commands.
HELP
}
function subcommand__h() {
_usage
}
# --help
function desc___help() { echo "\t\t- display this message and exit" ; }
function help___help() { help__h ; }
function subcommand___help() { subcommand__h ; }
function desc_get() { echo "\t\t\t- download all dependencies" ; }
function help_get() { cat <<HELP
Downloads all dependencies via \`go get\`. Example args might include '-u -d -t'
Help text for \`go help get\`:
$(go help get 2>&1 | awk '{ print " " $0}')
HELP
}
function subcommand_get() {
go get "$@" ./...
}
function desc_install() { echo "\t\t- go install this library" ; }
function subcommand_install() {
go install "$@" ./...
}
function desc_fmtpolice() { echo "\t\t- validate formatting using gofmt and golint" ; }
function help_fmtpolice() { cat <<HELP
Validate formatting using gofmt and golint. Will download the fmpolice file to ./fmtpolice
if not already present, so please add "fmtpolice" to your \`.gitignore\`
HELP
}
function subcommand_fmtpolice() {
if [[ ! -s fmtpolice ]] ; then
curl -sLOf https://raw.githubusercontent.com/rafecolton/fmtpolice/master/fmtpolice
fi
bash fmtpolice
}
_main "$@" # this line must always be last