Make session -p work remotely, also make host a flag
This commit is contained in:
@@ -34,7 +34,7 @@ __session_hosts() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_arguments \
|
_arguments \
|
||||||
'(- *)'{-h,--help}'[Show usage information]' \
|
'(- : *)'{-h,--help}'[Show usage information]' \
|
||||||
'(- *)'{-p,--project}'[Create or attach to a session for a project in ~/Projects]:project:__session_projects' \
|
'(-H --host)'{-H,--host}'[Create or attach to the session on a remote host via ssh]:host:__session_hosts' \
|
||||||
':session:__session_sessions' \
|
'(-p --project :)'{-p,--project}'[Create or attach to a session for a project in ~/Projects]:project:__session_projects' \
|
||||||
':host:__session_hosts'
|
'(-p --project)1:session:__session_sessions'
|
||||||
|
|||||||
@@ -1,28 +1,97 @@
|
|||||||
session() {
|
session() {
|
||||||
if [[ "$1" == "" ]]; then
|
local usage="usage: session [-h] [-H <host>] <name>
|
||||||
echo "usage: session [-h] <name> [<host>]
|
session [-H <host>] -p <project>"
|
||||||
session -p <project>"
|
local name="" project="" host=""
|
||||||
elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
|
|
||||||
echo "usage: session [-h] <name> [<host>]
|
|
||||||
session -p <project>
|
|
||||||
|
|
||||||
Create or attach to a tmux session by name either locally on on a remote host
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case "$1" in
|
||||||
|
-h|--help)
|
||||||
|
echo "$usage
|
||||||
|
|
||||||
|
Create or attach to a tmux session by name either locally or on a remote host
|
||||||
via ssh.
|
via ssh.
|
||||||
|
|
||||||
With -p create or attach to a session for <project>, a path in ~/Projects with
|
With -p create or attach to a session for <project>, a path in ~/Projects with
|
||||||
the prefix removed, which must be a git repository. The session is named after
|
the prefix removed, which must be a git repository. The session is named after
|
||||||
the project and holds a home window plus a window at the project, named after
|
the project and holds a home window plus a window at the project, named after
|
||||||
its default branch.
|
its default branch.
|
||||||
|
|
||||||
|
With -H create or attach to the session on <host> via ssh, where <host> is
|
||||||
|
either a hostname or a shortname defined in ~/.config/session. Combined with -p
|
||||||
|
the project is resolved on <host>, not locally. Not allowed inside a tmux
|
||||||
|
session.
|
||||||
"
|
"
|
||||||
elif [[ "$1" == "-p" ]] || [[ "$1" == "--project" ]]; then
|
return 0
|
||||||
local project=${2%/}
|
;;
|
||||||
if [[ "$project" == "" ]]; then
|
-p|--project)
|
||||||
echo "$fg[red]error:$reset_color missing <project> argument"
|
if [[ "$2" == "" ]]; then
|
||||||
|
echo "$fg[red]error:$reset_color missing <project> argument"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
project=${2%/}
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
-H|--host)
|
||||||
|
if [[ "$2" == "" ]]; then
|
||||||
|
echo "$fg[red]error:$reset_color missing <host> argument"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
host=$2
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
-*)
|
||||||
|
echo "$fg[red]error:$reset_color unknown option: $1"
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
if [[ "$name" != "" ]]; then
|
||||||
|
echo "$fg[red]error:$reset_color invalid argument: $1"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
name=$1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ "$project" != "" ]] && [[ "$name" != "" ]]; then
|
||||||
|
echo "$fg[red]error:$reset_color <name> not allowed with -p"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
if [[ "$project" == "" ]] && [[ "$name" == "" ]]; then
|
||||||
|
echo "$usage"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$host" != "" ]]; then
|
||||||
|
if [[ "$TMUX" != "" ]]; then
|
||||||
|
echo "$fg[red]error:$reset_color <host> not allowed inside tmux session"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
if [[ "$3" != "" ]]; then
|
declare -A hosts
|
||||||
echo "$fg[red]error:$reset_color invalid argument: $3"
|
if [ -f ~/.config/session ]; then
|
||||||
return 1
|
source ~/.config/session
|
||||||
|
fi
|
||||||
|
host=${hosts[$host]:-$host}
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$project" != "" ]]; then
|
||||||
|
# tmux rewrites `.` and `:` in session names, so mirror that to keep the
|
||||||
|
# name we create and the name we target in sync.
|
||||||
|
name=${project//[.:]/_}
|
||||||
|
if [[ "$host" != "" ]]; then
|
||||||
|
# The project lives in the remote ~/Projects, so resolve it there rather
|
||||||
|
# than locally. The remote session-created hook reads TMUX_PROJECT back
|
||||||
|
# out of the remote tmux server to build the layout.
|
||||||
|
ssh "$host" -t "
|
||||||
|
repo=\$HOME/Projects/${(q)project}
|
||||||
|
[ -d \"\$repo\" ] ||
|
||||||
|
{ echo 'error: no such project: ${(q)project}' >&2; exit 1; }
|
||||||
|
git -C \"\$repo\" rev-parse --git-dir >/dev/null 2>&1 ||
|
||||||
|
{ echo \"error: not a git repository: \$repo\" >&2; exit 1; }
|
||||||
|
exec tmux new-session -As ${(q)name} -c \"\$HOME\" -e TMUX_PROJECT=${(q)project}
|
||||||
|
"
|
||||||
|
return
|
||||||
fi
|
fi
|
||||||
local repo=$HOME/Projects/$project
|
local repo=$HOME/Projects/$project
|
||||||
if [ ! -d "$repo" ]; then
|
if [ ! -d "$repo" ]; then
|
||||||
@@ -33,9 +102,6 @@ its default branch.
|
|||||||
echo "$fg[red]error:$reset_color not a git repository: $repo"
|
echo "$fg[red]error:$reset_color not a git repository: $repo"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
# tmux rewrites `.` and `:` in session names, so mirror that to keep the
|
|
||||||
# name we create and the name we target in sync.
|
|
||||||
local name=${project//[.:]/_}
|
|
||||||
if [[ "$TMUX" == "" ]]; then
|
if [[ "$TMUX" == "" ]]; then
|
||||||
tmux new-session -As "$name" -c $HOME -e "TMUX_PROJECT=$project"
|
tmux new-session -As "$name" -c $HOME -e "TMUX_PROJECT=$project"
|
||||||
else
|
else
|
||||||
@@ -43,33 +109,13 @@ its default branch.
|
|||||||
tmux new-session -Ads "$name" -c $HOME -e "TMUX_PROJECT=$project"
|
tmux new-session -Ads "$name" -c $HOME -e "TMUX_PROJECT=$project"
|
||||||
tmux switch-client -t "=$name"
|
tmux switch-client -t "=$name"
|
||||||
fi
|
fi
|
||||||
|
elif [[ "$host" != "" ]]; then
|
||||||
|
ssh "$host" -t tmux new-session -As "$name"
|
||||||
|
elif [[ "$TMUX" == "" ]]; then
|
||||||
|
tmux new-session -As "$name"
|
||||||
else
|
else
|
||||||
local name=$1
|
tmux has-session -t "=$name" &> /dev/null || \
|
||||||
local host=$2
|
tmux new-session -Ads "$name" -c $HOME
|
||||||
if [[ "$3" != "" ]]; then
|
tmux switch-client -t "=$name"
|
||||||
echo "$fg[red]error:$reset_color invalid argument: $3"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
declare -A hosts
|
|
||||||
if [ -f ~/.config/session ]; then
|
|
||||||
source ~/.config/session
|
|
||||||
fi
|
|
||||||
local url=$hosts[$host]
|
|
||||||
host=${url:-$host}
|
|
||||||
if [[ "$TMUX" == "" ]]; then
|
|
||||||
local cmd="tmux new-session -As $name"
|
|
||||||
if [[ "$host" != "" ]]; then
|
|
||||||
cmd="ssh $host -t $cmd"
|
|
||||||
fi
|
|
||||||
eval $cmd
|
|
||||||
else
|
|
||||||
if [[ "$host" != "" ]]; then
|
|
||||||
echo "$fg[red]error:$reset_color <host> not allowed inside tmux session"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
tmux list-sessions | grep "$name:" &> /dev/null || \
|
|
||||||
tmux new-session -Ads $name -c $HOME
|
|
||||||
tmux switch-client -t $name
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user