-
Notifications
You must be signed in to change notification settings - Fork 5
/
xf
executable file
·114 lines (97 loc) · 1.95 KB
/
xf
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
#!/bin/bash
VERSION_NUMBER=1.0.0
usage() {
cat << EOF
Usage: xf
COMMANDS:
--help, -h
Show this message
--version, -v
Show version
init <path>
Initialize cmd project. Uses current working
directory if no path is given.
ls
List available commands
<command> [options]
Run the specified command
EOF
}
show_cmd_version() {
echo "xf v$VERSION_NUMBER"
}
find_project_root() {
while [[ ! -a .xf ]]
do
cd ..
# using dev/null because it always? exists at root
# need to find a better way of identifying root directory
if [[ -a dev/null ]]
then
echo "ERROR: No .xf file found"
exit 1 # no cmd files found
fi
done
PROJECT_PATH=.
}
list_all_commands() {
find $PROJECT_PATH -perm +111 -type f -not -path "*.git*"
}
find_command() {
list_all_commands | grep $1$
}
select_command() {
echo "Multiple matches found:"
echo $1 | tr ' ' '\n' > /tmp/xfcommands
cat -n /tmp/xfcommands
echo "Enter the command you want to execute:"
read command_number
command=$(sed -n "$command_number p" < /tmp/xfcommands)
}
exit_no_such_cmd() {
echo "ERROR: No such command"
exit 1 # no command to run
}
if [[ -z "$1" ]] # if no arguments were given, print usage
then
show_cmd_version
usage
exit 1
elif [[ $1 = "--help" ]] || [[ $1 = "-h" ]]
then
usage
exit 0
elif [[ $1 = "--version" ]] || [[ $1 = "-v" ]]
then
show_cmd_version
exit 0
elif [[ $1 = "init" ]]
then
# use specified path if it exists
if [[ -z "$2" ]]
then
PROJECT_PATH=.
else
PROJECT_PATH=$2
fi
touch $PROJECT_PATH/.xf
exit 0
else
find_project_root
if [[ $1 = "ls" ]]
then
list_all_commands
else
command=$(find_command $1)
count=`echo $command | wc -w`
if [[ $count -eq 0 ]]
then
exit_no_such_cmd
elif [[ $count -ne 1 ]]
then
select_command "$command"
fi
$command ${@:2:$#}
exit $?
fi
fi