-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkcmd
More file actions
executable file
·138 lines (121 loc) · 4.22 KB
/
mkcmd
File metadata and controls
executable file
·138 lines (121 loc) · 4.22 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
#!/bin/zsh
# A simple command to automate the creation of shell scripts.
VERSION="1.1.0"
VERSION_ECHO=" mkcmd v$VERSION"
HOME_BIN="$HOME/bin/"
LOCAL_BIN="$HOME/.local/bin/"
TARGET_BIN=""
EDIT_COMMANDS=true
RUN_AFTER_EDITING=false
CMD_EDITOR=("$EDITOR")
[[ "$EDITOR" =~ "nano" ]] && CMD_EDITOR+="+2"
showHelp()
{
cat <<- "EOF"
Usage: mkcmd [-hvnr] [-b|--bin <directory>] [-e|--editor <editor>] <command>
-h, --help print help document
-v, --version print script version
-n, --no-edit do not edit file
-r, --run run the command after exiting the editor
-b, --bin=OUTDIR set the output directory to OUTDIR
-e, --editor=EDIT set the command editor to EDIT or the global editor ($EDITOR) if EDIT is invalid
Config:
mkcmd can be configured with a pre-determined output directory via the
variable "default_bin_directory". The config file can be either
$HOME/.mkcmd.conf or $XDG_CONFIG_HOME/mkcmd.conf. As an example, to set
the default output directory to $HOME/bin, you would write this in the
config file: default_bin_directory=$HOME/bin
Examples:
mkcmd new_command
mkcmd -n --bin .local/bin new_command_2
EOF
printf "\n$VERSION_ECHO\n"
}
readConfig()
{
[[ ! -f "$HOME/.mkcmd.conf" && ! -f "${XDG_CONFIG_HOME:-HOME"/.config"}/mkcmd.conf" ]] && return 0
[[ -f "$HOME/.mkcmd.conf" ]] && CONFIG="$HOME/.mkcmd.conf"
[[ -f "${XDG_CONFIG_HOME:-HOME"/.config"}/mkcmd.conf" ]] && CONFIG="${XDG_CONFIG_HOME:-HOME"/.config"}/mkcmd.conf"
CONF_BIN_DIR=$(grep -P "(?<=default_bin_directory=).+" --only-matching --color=no "$CONFIG")
BUFFER=""
for (( i=0 ; i < ${#CONF_BIN_DIR} ; i++ )); do
if [[ ${CONF_BIN_DIR:$i:1} == "$" ]]; then
i+=1
VAR_BUFFER=""
while [[ ${CONF_BIN_DIR:$i:1} =~ '\w' ]]; do
VAR_BUFFER=""$VAR_BUFFER"${CONF_BIN_DIR:$i:1}"
i+=1
done
BUFFER="$BUFFER$(env | grep --only-matching --color=no -P "(?<=\b$VAR_BUFFER\b=).+")"
fi
BUFFER="$BUFFER${CONF_BIN_DIR:$i:1}"
done
TARGET_BIN="$BUFFER/"
}
setEditorSafe()
{
[[ "$(which $1 &>/dev/null; printf $?)" == "0" ]] && CMD_EDITOR="$1" || printf "::ERROR - Editor \"$1\" not found! Keeping default editor ($EDITOR)\n"
}
readConfig
OPTIONS=$(getopt -l "help,version,no-edit,run,bin:,editor:" -o "hvnrb:e:" -a -- "$@")
eval set -- "$OPTIONS"
while true
do
case "$1" in
-h|--help)
showHelp
exit 0
;;
-v|--version)
printf "$VERSION_ECHO\n"
exit 0
;;
-n|--no-edit)
EDIT_COMMANDS=false
;;
-r|--run)
RUN_AFTER_EDITING=true
;;
-b|--bin)
shift
TARGET_BIN="$1"
;;
-e|--editor)
shift
setEditorSafe "$1"
;;
--)
shift
break
;;
esac
shift
done
CMDNAMES=($@)
[[ ${#CMDNAMES[@]} == 0 ]] && (printf "::ERROR - Command name(s)/path(s) missing!\n" ; exit -1)
# FIXME: make the directory querying, setting, and other shit BETTER!
queryHomeBin()
{
TARGET_BIN=${HOME_BIN:-LOCAL_BIN}
if [[ -d "$HOME_BIN" && -d "$LOCAL_BIN" ]]; then
printf "Multiple user bin/ directories discovered\n:: Directories\n 1) "$HOME_BIN" 2) "$LOCAL_BIN"\n"
read -s "?Selection (default = 1): " CHOICE ; echo ""
[[ "$CHOICE" == 2 ]] && TARGET_BIN="$LOCAL_BIN"
return 0
fi
[[ -d "$TARGET_BIN" ]] || { printf "::ERROR - Couldn't find "$HOME_BIN" or "$LOCAL_BIN". Make sure at least one of them exists!\n"; return -1 }
}
for COMMAND in ${CMDNAMES[@]}; do
[[ -e "$COMMAND" ]] || COMMAND="$TARGET_BIN$COMMAND"
if [[ -e "$COMMAND" ]]; then
[[ ! -x "$COMMAND" ]] && chmod +x "$COMMAND"
[[ "$EDIT_COMMANDS" == "true" ]] && { "${CMD_EDITOR[@]}" "$COMMAND"; [[ "$RUN_AFTER_EDITING" == "true" ]] && "$COMMAND"}
continue
fi
if [[ ! "$COMMAND" =~ "/" ]]; then
[[ -z "$TARGET_BIN" ]] && queryHomeBin
[[ $? -eq -1 ]] && exit -1
fi
printf "#!${SHELL#/usr}" > "$COMMAND" ; chmod +x "$COMMAND"
[[ "$EDIT_COMMANDS" == "true" ]] && { "${CMD_EDITOR[@]}" "$COMMAND"; [[ "$RUN_AFTER_EDITING" == "true" ]] && "$COMMAND" }
done