-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrdev_bash_helpers
117 lines (113 loc) · 3.73 KB
/
rdev_bash_helpers
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
#!/usr/bin/env bash
#######################################
# Wrapper method for `rdev` to cache names of rdev instances
# Arguments:
# same as native `rdev` commands
#######################################
xdev() {
if [[ "$1" == "create" ]]; then
echo "This will create a new rdev instance"
echo "........................."
echo ""
if [[ -z $2 ]];then
echo "no product supplied. Using cache from ~/.rdevLastCreateCache"
product=$(<~/.rdevLastCreateCache)
else
echo "$2" > ~/.rdevLastCreateCache
product=$2
fi
echo "creating rdev instance for product ${product}"
rdev create ${product} > ~/.rdevLatestCreateResponse
echo "created:"
cat ~/.rdevLatestCreateResponse
echo "........................."
xdev refresh
elif [[ "$1" == "delete" ]]; then
echo "rdev delete wrapper"
rdev delete $2
xdev refresh
elif [[ "$1" == "refresh" ]]; then
confirmRdevCache
rdev ls > ~/.rdevLsCache
echo "New cache is set as:"
cat ~/.rdevLsCache
elif [[ "$1" == "ls" ]]; then
confirmRdevCache
echo "Dislaying cached rdev instances:"
cat ~/.rdevLsCache
else
rdev $@
fi
}
#######################################
# Filter rdev results and call connect()
# Arguments:
# (optional) flag: (passed down to connect()) "-c" or "-s" - only open either VS Code or a basic ssh session, respectively
#######################################
fconnect() {
if ! command -v fzf &> /dev/null; then
echo "fzf could not be found. Maybe it's not installed?"
elif [[ "-h" == $1 ]]; then
echo "fconnect filters rdev results and calls connect()"
echo
echo "Optional params:"
echo "-h help"
echo "-s establish a basic ssh session"
echo "-rs establish an rdev ssh session"
echo "-c establish an rdev VS Code session"
else
confirmRdevCache
rdevLsCacheResults=$(cat ~/.rdevLsCache | grep RUNNING | awk '{print $1}')
resultsCount=$(echo $rdevLsCacheResults | wc -l | sed 's/ //g')
echo $rdevLsCache
echo "running instances: " $resultsCount
if [[ resultsCount -gt 1 ]]; then
# call connect() and pass it the result we select from the fzf selection menu along with any flags set
connect $(echo $rdevLsCacheResults | fzf --height 25%) ${1}
elif [[ resultsCount -eq 1 ]]; then
# call connect() and pass it the only item in the cached results (along with any flags set)
connect $rdevLsCacheResults ${1}
else
echo "no results in rdevLsCache"
fi
fi
}
#######################################
# Connect to an rdev host with vscode then ssh into the host
# Arguments:
# host: name of rdev host to connect to
# (optional) flag: "-c" or "-s" - only open either VS Code or a basic ssh session, respectively
#######################################
connect() {
if [[ -z $1 ]]; then
echo "name of host must be given as the one and only argument to this function"
elif [[ "-c" == $2 ]]; then
echo "Connecting to host: $1"
echo "starting VS Code..."
rdev code $1
elif [[ "-s" == $2 ]]; then
echo "starting ssh session and CD'ing to the ${1%%/*} dir..."
ssh -t $1 "cd ${1%%/*}; bash -l"
elif [[ "-rs" == $2 ]]; then
echo "starting an RDEV ssh session"
rdev ssh $1
else
echo "Connecting to host: $1"
echo "starting VS Code..."
rdev code $1
echo "starting ssh session and CD'ing to the ${1%%/*} dir..."
ssh -t $1 "cd ${1%%/*}; bash -l"
fi
}
#######################################
# Check if rdev cache file exists and create one if it does not
# Arguments:
# none
#######################################
confirmRdevCache() {
FILE=~/.rdevLsCache
if [[ ! -f "$FILE" ]]; then
echo "$FILE does not exist. Creating and populating it now..."
rdev ls > ~/.rdevLsCache
fi
}