You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.2 KiB

#!/bin/bash
help() {
cat <<EOF
Usage: $(basename $0) [OPTION]
Easily cd to a project directory and launch a dedicated tmux server.
Project paths are stored in $PROJECTS_FILE.
If there is at least 1 project path and this script is called without
arguments, it will launch fzf so you can select one.
Options:
-a [dir] Add a new project. Defaults to current dir
-r Remove project from projects. (select with fzf)
-l List projects
-h Print this message
EOF
}
list_projects() {
cat $PROJECTS_FILE
}
add_project() {
echo "Info: adding project $NEW_PROJECT_DIR"
# Check if project is already present
cat $PROJECTS_FILE | grep $NEW_PROJECT_DIR &> /dev/null
if [ $? == 0 ]; then
echo "Info: project already exists"
exit 0
fi
# Check if dir exists
if [ ! -d $NEW_PROJECT_DIR ]; then
echo "Error: directory $NEW_PROJECT_DIR does not exists"
exit 1
fi
echo "$NEW_PROJECT_DIR" >> "$PROJECTS_FILE"
if [ $? == 0 ]; then
echo "Info: added successfully"
exit 0
else
echo "Error: couldn't add $NEW_PROJECT_DIR"
exit 1
fi
}
remove_project() {
PROJECT=$(cat $PROJECTS_FILE | eval "$FINDER")
if [ -z "$PROJECT" ]; then
exit 0
fi
read -p "Remove project $(basename $PROJECT)? [Ny]: " SHOULD_REMOVE
SHOULD_REMOVE=${SHOULD_REMOVE:-n}
if [[ $SHOULD_REMOVE =~ [yY] ]]; then
sed -i "\#$PROJECT#d" $PROJECTS_FILE
if [ $? == 0 ]; then
echo "Info: removed $PROJECT"
exit 0
else
echo "Error: couldn't remove $PROJECT"
exit 1
fi
fi
exit 0
}
open_project() {
PROJECT=$(cat $PROJECTS_FILE | eval "$FINDER")
if [ -z "$PROJECT" ]; then
exit 0
fi
if [ -d "$PROJECT" ]; then
PROJECT_BASENAME=$(basename $PROJECT)
TMUX_ARGS=""
tmux -L $PROJECT_BASENAME has-session
if [ $? == 0 ]; then
TMUX_ARGS="-L $PROJECT_BASENAME attach"
else
TMUX_ARGS="-L $PROJECT_BASENAME new-session -c $PROJECT"
fi
if [ $TERM == "linux" ]; then
$TERMINAL -e tmux $TMUX_ARGS
else
tmux $TMUX_ARGS
fi
else
read -p "Error: directory for $(basename $PROJECT) not found. Remove project? [Ny]: " REMOVE_MISSING
REMOVE_MISSING=${REMOVE_MISSING:-n}
if [[ $REMOVE_MISSING =~ [yY] ]]; then
sed -i "\#$PROJECT#d" $PROJECTS_FILE
echo "Info: Removed $PROJECT"
fi
exit 1
fi
}
PROJECTS_FILE="$HOME/.projects"
OPEN="true"
NEW_PROJECT_DIR=""
FINDER="fzf"
# Parse args
while getopts "f:hral" opt; do
case $opt in
f) FINDER=$OPTARG ;;
a)
OPEN="false"
if [ ! -z $OPTARG ]; then
NEW_PROJECT_DIR=$OPTARG
else
NEW_PROJECT_DIR=$(pwd)
fi
add_project
;;
r) OPEN="false"; remove_project ;;
l) list_projects; exit 0 ;;
h) OPEN="false"; help; exit 0 ;;
\?) echo "" >&2; help; exit 1 ;;
esac
done
COMMAND=$(cut -f 1 -d " " <<< $FINDER)
if ! command -v $COMMAND &> /dev/null
then
echo "$COMMAND could not be found"
exit 1
fi
if [ ! -f "$PROJECTS_FILE" ]; then
touch "$PROJECTS_FILE"
echo "Info: Created projects file"
fi
if [ ! -s "$PROJECTS_FILE" ]; then
echo "Error: No project found. Add at least 1 project using -a first"
help
exit 1
fi
if [ $OPEN == "true" ]; then
open_project
exit 0
fi