Compare commits

...

1 Commits

Author SHA1 Message Date
95c179787c v0.0.2 2025-11-24 10:10:56 +08:00
4 changed files with 112 additions and 10 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.env

22
app-old.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/bash
# 环境变量
APP_DIR=$(cd "$(dirname "$0")" && pwd)
# 执行命令
if [ -f "$APP_DIR/cmds/app-$1.sh" ]; then
. "$APP_DIR/cmds/app-$1.sh"
else
echo "Error: usage: ./app.sh <command>"
echo "commands:"
if [ -d "$APP_DIR/cmds" ]; then
for file in "$APP_DIR"/cmds/app-*.sh; do
if [ -f "$file" ]; then
filename=$(basename "$file")
cmd_name=${filename#app-}
cmd_name=${cmd_name%.sh}
echo " $cmd_name"
fi
done
fi
fi

51
app.sh
View File

@ -1,16 +1,23 @@
#!/bin/bash
# 环境变量
APP_DIR=$(cd "$(dirname "$0")" && pwd)
# ********** 环境变量定义 **********
#
# 应用所在目录
APP_HOME=$(cd "$(dirname "$0")" && pwd)
# 应用名称
APP_NAME=$(basename "$APP_HOME")
APP_NAME="${APP_NAME#app-}"
# 扩展环境变量定义
if [ -f "$APP_HOME/.env" ]; then
. "$APP_HOME/.env"
fi
# 执行命令
if [ -f "$APP_DIR/cmds/app-$1.sh" ]; then
. "$APP_DIR/cmds/app-$1.sh"
else
echo "Error: usage: ./app.sh <command>"
app_usage() {
echo "usage: $0 <command> [args]"
echo;
echo "commands:"
if [ -d "$APP_DIR/cmds" ]; then
for file in "$APP_DIR"/cmds/app-*.sh; do
if [ -d "$APP_HOME/mods" ]; then
for file in "$APP_HOME"/mods/*.sh; do
if [ -f "$file" ]; then
filename=$(basename "$file")
cmd_name=${filename#app-}
@ -19,4 +26,30 @@ else
fi
done
fi
echo;
echo "args:"
echo " -h, --help show the help documentation"
echo " -v, --version show the version"
}
app_handle() {
if [ ! -f "$APP_HOME/mods/$1.sh" ]; then
echo "Error: unknown command [$1]"
exit 1
fi
. "$APP_HOME/mods/$1.sh"
}
case "$1" in
-h|--help|"")
app_usage
;;
-v|--version)
echo "v0.0.1"
;;
*)
app_handle $@
;;
esac

46
mods/tmux.sh Executable file
View File

@ -0,0 +1,46 @@
#!/bin/bash
export TMUX_SESSION="app-$APP_NAME"
case "$2" in
-h|--help|"")
echo "commands:"
echo " exec execute tmux with commands"
echo " stat show the status"
echo;
echo "args:"
echo " -h, --help show the help documentation"
echo " -v, --version show the version"
;;
-v|--version)
echo "v0.0.1"
;;
stat)
CURR_TMUX_SESSION=$(tmux list-session 2>&1 | grep "$TMUX_SESSION")
if [ -z "$CURR_TMUX_SESSION" ]; then
echo "tmux session [$TMUX_SESSION] not exists."
else
echo "tmux session [$TMUX_SESSION] => $CURR_TMUX_SESSION"
tmux attach -t $TMUX_SESSION
fi
;;
exec)
CURR_TMUX_SESSION=$(tmux list-session 2>&1 | grep "$TMUX_SESSION")
if [ -n "$CURR_TMUX_SESSION" ]; then
echo "tmux session [$TMUX_SESSION] => $CURR_TMUX_SESSION"
tmux attach -t $TMUX_SESSION
else
echo "starting..."
tmux new-session -d -s "$TMUX_SESSION"
sleep 3
if [ -n "$TMUX_COMMAND" ]; then
tmux send-keys -t "$TMUX_SESSION" "$TMUX_COMMAND" Enter
fi
tmux attach -t "$TMUX_SESSION"
fi
;;
*)
echo "Invalid command: ${@:2}"
;;
esac