forked from msfragala/repo-sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo.sh
102 lines (96 loc) · 2.82 KB
/
repo.sh
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
export REPOSITORIES=~/Projects
function proj() {
local GREEN='\033[0;32m'
local RED='\033[0;31m'
local NC='\033[0m'
# Check that the $REPOSITORIES variable is set
if [ ! -d "$REPOSITORIES" ]; then
echo "${RED}\$REPOSITORIES must be a directory${NC}"
return 1
fi
case "$1" in
help)
echo "
Usage: proj [<command>] [<name>]
proj Change directory into ${GREEN}\$REPOSITORIES${NC}
proj <name> Change directory into a repository
proj list List all repositories
proj add <name> Create a new directory in ${GREEN}\$REPOSITORIES${NC} and change into it
proj add <git_url> <opt_dir_name> Clone a Git repository into ${GREEN}\$REPOSITORIES${NC} and change into it
proj remove <name> Move a repository into Trash
proj help Show help text for all commands
Where <name> is a directory inside ${GREEN}\$REPOSITORIES${NC} (or one to be added)
";
;;
add)
if [ -z "$2" ]; then
echo "${RED}Please specify a directory name to add${NC}"
return 1
elif [ -d "$REPOSITORIES/$2" ]; then
echo "${RED}A directory '$2' already exists${NC}"
return 1
elif [[ "$2" =~ \.git$ ]]; then
if [ -z "$3" ]; then base=$(basename $2) name="${base%.*}"; else name="$3"; fi
git clone $2 $REPOSITORIES/$name
cd "$REPOSITORIES/$name"
if [ -f .nvmrc ]; then
nvm use
fi
else
mkdir "$REPOSITORIES/$2"
cd "$REPOSITORIES/$2"
return 0
fi
;;
list)
local repos=$(find $REPOSITORIES -type d -maxdepth 1 -mindepth 1)
if [ -z "$repos" ]; then
echo "${RED}No repositories found${NC}"
return 1
else
for entry in $REPOSITORIES/*; do [ -d "$entry" ];
basename $entry
done
return 0
fi
;;
remove)
if [ -z "$2" ]; then
echo "${RED}Please specify a directory name to remove${NC}"
return 1
elif [ -d "$REPOSITORIES/$2" ]; then
mv "$REPOSITORIES/$2" ~/.trash
return 0
else
echo "${RED}No directory '$2' found${NC}"
return 1
fi
;;
*)
if [ -d "$REPOSITORIES/$1" ]; then
cd "$REPOSITORIES/$1"
if [ -f .nvmrc ]; then
nvm use
fi
return 0
else
cd "$REPOSITORIES"
return 0
fi
;;
esac
}
if [ -n "$ZSH_VERSION" ]; then
function proj_complete() {
compls=$(proj list)
completions=(${=compls})
compadd -- $completions
}
compdef proj_complete proj
elif [ -n "$BASH_VERSION" ]; then
function proj_complete () {
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "$(ls $REPOSITORIES)" -- $cur) )
}
complete -F proj_complete proj
fi