-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkspace.sh
More file actions
executable file
·128 lines (112 loc) · 2.61 KB
/
workspace.sh
File metadata and controls
executable file
·128 lines (112 loc) · 2.61 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
#!/bin/bash
function workspace() {
local DIR=~/.workspaces
local EXITCODE=0
if [[ ! -d $DIR ]]; then
mkdir $DIR
fi
__workspaceman_add() {
if [[ -f $DIR/.$1 ]]; then
echo "Workspace with this name already exists, overwrite? "
select yn in "Yes" "No"; do
case $yn in
Yes ) break;;
No ) return $EXITCODE;;
esac
done
fi
pwd > $DIR/.$1
EXITCODE=0
}
__workspaceman_delete() {
if [[ -f $DIR/.$1 ]]; then
rm $DIR/.$1
EXITCODE=0
else
EXITCODE=1
fi
}
__workspaceman_list() {
ls -A $DIR | grep ^\\. | sed s/\.//
EXITCODE=0
}
__workspaceman_help() {
cat "$DIR/help"
EXITCODE=0
}
__workspaceman_version() {
cat "$DIR/version"
EXITCODE=0
}
__workspaceman_which() {
if [[ -f $DIR/.$1 ]]; then
cat $DIR/.$1
EXITCODE=0
else
EXITCODE=1
fi
}
# Handle flags, getops breaks for functions
if [[ ! -z "$1" ]] && [[ $1 =~ ^- ]]; then
# Handle -a/--add flag
if [[ "$1" = "-a" ]] || [[ "$1" = "--add" ]]; then
# Second parameter is required
if [[ ! -z "$2" ]]; then
__workspaceman_add $2
else
echo "A name is required when adding a new workspace."
EXITCODE=1
return $EXITCODE
fi
return $EXITCODE
# Handle -l/--list flag
elif [[ "$1" = "-l" ]] || [[ "$1" = "--list" ]]; then
__workspaceman_list
return $EXITCODE
# Handle -h/--help flag
elif [[ "$1" = "-h" ]] || [[ "$1" = "--help" ]]; then
__workspaceman_help
return $EXITCODE
# Handle -v/--version flag
elif [[ "$1" = "-v" ]] || [[ "$1" = "--version" ]]; then
__workspaceman_version
return $EXITCODE
# Handle -d/--delete flag
elif [[ "$1" = "-d" ]] || [[ "$1" = "--delete" ]]; then
# Second parameter is required
if [[ ! -z "$2" ]]; then
__workspaceman_delete $2
else
echo "A name is required when deleting a workspace."
EXITCODE=1
return $EXITCODE
fi
return $EXITCODE
# Handle -w/--which flag
elif [[ "$1" = "-w" ]] || [[ "$1" = "--which" ]]; then
# Second parameter is required
if [[ ! -z "$2" ]]; then
__workspaceman_which $2
else
echo "A name is required when checking a workspace directory."
EXITCODE=1
return $EXITCODE
fi
return $EXITCODE
fi
# Handle all other commands (i.e, switching directory)
elif [[ ! -z "$1" ]] && [[ -f $DIR/.$1 ]]; then
cd $(head -n 1 $DIR/.$1)
cp $DIR/.$1 $DIR/last_used 2> /dev/null
return $EXITCODE
elif [[ ! -z "$1" ]] && [[ ! -f $DIR/.$1 ]]; then
echo "workspace not found: $1"
EXITCODE=1
return $EXITCODE
elif [[ -f $DIR/last_used ]]; then
cd $(head -n 1 $DIR/last_used)
return $EXITCODE
fi
EXITCODE=1
return $EXITCODE
}