-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshcd
executable file
·80 lines (74 loc) · 2.02 KB
/
sshcd
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
#!/usr/bin/env bash
usage() {
echo "Usage: $0 [cluster] [optional: directory]"
echo
echo "Arguments:"
echo " cluster The remote server to navigate to: newton, jupiter, or pipeline."
echo " directory (optional) Specify a directory to navigate to. If omitted, defaults to the current directory if it’s a project subfolder."
echo
echo "Options:"
echo " -h, --help Show this help message."
exit 1
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
;;
newton|jupiter|trindatimes)
cluster="$1"
case "$cluster" in
newton)
remote_dir=$NEWTON_PROJECTS
remote_shell="bash"
;;
jupiter)
remote_dir=$JUPITER_PROJECTS
remote_shell="bash"
;;
trindatimes)
remote_dir=$LOCAL_PROJECTS
remote_shell="zsh"
;;
*)
echo "ERROR: Invalid cluster '$1'. Must be 'newton', 'jupiter', or 'trindatimes'."
usage
;;
esac
shift
;;
*)
target_dir="$1"
shift
;;
esac
done
# Validate cluster selection
if [[ -z "$cluster" ]]; then
echo "ERROR: Missing required cluster argument."
usage
fi
# Function to check if the directory is a subfolder of the project
check_local_subfolder() {
CURRENT_DIR=$(pwd)'/'
if [[ "$CURRENT_DIR" == "$LOCAL_PROJECTS"* ]]; then
len_local=${#LOCAL_PROJECTS}
len_current=${#CURRENT_DIR}
target="$remote_dir${CURRENT_DIR:$len_local:$len_current}"
else
echo "ERROR: Current directory is NOT a subfolder of the $cluster projects."
exit 1
fi
}
# Handle target directory
if [[ -z "$target_dir" ]]; then
# Default to checking if current directory is a subfolder
check_local_subfolder
else
# Use provided directory as target
target="$target_dir"
fi
# Construct and run SSH command
ssh_command="ssh -t $cluster \"if [[ -d $target ]]; then cd $target && $remote_shell --login; else echo 'ERROR: Directory does not exist on remote.'; fi\""
eval "$ssh_command"