initial commit
This commit is contained in:
192
bin/helpers/func.sh
Executable file
192
bin/helpers/func.sh
Executable file
@@ -0,0 +1,192 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
#Color print function, usage: println "message" "color"
|
||||
println() {
|
||||
color=$2
|
||||
printfe "%s\n" $color "$1"
|
||||
}
|
||||
|
||||
# print colored with printf (args: format, color, message ...)
|
||||
printfe() {
|
||||
format=$1
|
||||
color=$2
|
||||
message=$3
|
||||
show_time=true
|
||||
|
||||
# Check if $4 is explicitly set to false, otherwise default to true
|
||||
if [ ! -z "$4" ] && [ "$4" == "false" ]; then
|
||||
show_time=false
|
||||
fi
|
||||
|
||||
red=$(tput setaf 1)
|
||||
green=$(tput setaf 2)
|
||||
yellow=$(tput setaf 3)
|
||||
blue=$(tput setaf 4)
|
||||
magenta=$(tput setaf 5)
|
||||
cyan=$(tput setaf 6)
|
||||
normal=$(tput sgr0)
|
||||
grey=$(tput setaf 8)
|
||||
|
||||
case $color in
|
||||
"red")
|
||||
color=$red
|
||||
;;
|
||||
"green")
|
||||
color=$green
|
||||
;;
|
||||
"yellow")
|
||||
color=$yellow
|
||||
;;
|
||||
"blue")
|
||||
color=$blue
|
||||
;;
|
||||
"magenta")
|
||||
color=$magenta
|
||||
;;
|
||||
"cyan")
|
||||
color=$cyan
|
||||
;;
|
||||
"grey")
|
||||
color=$grey
|
||||
;;
|
||||
*)
|
||||
color=$normal
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ "$show_time" == "false" ]; then
|
||||
printf "$color$format$normal" "$message"
|
||||
return
|
||||
fi
|
||||
|
||||
printf $grey"%s" "$(date +'%H:%M:%S')"$normal
|
||||
|
||||
case $color in
|
||||
$green | $cyan | $blue | $magenta | $normal)
|
||||
printf "$green INF $normal"
|
||||
;;
|
||||
$yellow)
|
||||
printf "$yellow WRN $normal"
|
||||
;;
|
||||
$red)
|
||||
printf "$red ERR $normal"
|
||||
;;
|
||||
*)
|
||||
printf "$normal"
|
||||
;;
|
||||
esac
|
||||
printf "$color$format$normal" "$message"
|
||||
}
|
||||
|
||||
# Print and run a command in yellow
|
||||
log_and_run() {
|
||||
printfe "%s\n" "yellow" "$*"
|
||||
eval "$@"
|
||||
}
|
||||
|
||||
run_docker_command() {
|
||||
cmd=$1
|
||||
log_level="$2"
|
||||
shift
|
||||
shift
|
||||
params=$@
|
||||
composer_image="composer/composer:2.7.8"
|
||||
php_image="php:8.3-cli-alpine3.20"
|
||||
phpstan_image="atishoo/phpstan:latest"
|
||||
AUTH_SOCK_DIRNAME=$(dirname $SSH_AUTH_SOCK)
|
||||
|
||||
# It's possible $SSH_AUTH_SOCK is not set, in that case we should set it to /tmp/ssh_auth_sock
|
||||
if [ -z "$SSH_AUTH_SOCK" ]; then
|
||||
AUTH_SOCK_DIRNAME="/tmp/ssh_auth_sock:/tmp/ssh_auth_sock"
|
||||
fi
|
||||
|
||||
# Take the name of the current directory
|
||||
container=$(basename $(pwd) | tr '[:upper:]' '[:lower:]')
|
||||
|
||||
# Check if the $container is an actual container from the $TRADAWARE_PATH/docker-compose.yml
|
||||
result=$(docker compose -f $TRADAWARE_PATH/docker-compose.yml ps -q $container 2>/dev/null)
|
||||
if [ -z "$result" ]; then
|
||||
# Ensure /home/$USER/.config/composer/auth.json exists, if not prefill it with an empty JSON object
|
||||
if [ ! -f /home/$USER/.config/composer/auth.json ]; then
|
||||
mkdir -p /home/$USER/.config/composer
|
||||
touch /home/$USER/.config/composer/auth.json
|
||||
echo "{
|
||||
\"github-oauth\": {
|
||||
\"github.com\": \"KEY_HERE\"
|
||||
}
|
||||
}" > /home/$USER/.config/composer/auth.json
|
||||
printfe "%s" "yellow" "Created an empty auth.json file at '"
|
||||
printfe "%s" "cyan" "/home/$USER/.config/composer/auth.json"
|
||||
printfe "%s\n" "yellow" "', you should edit this file and add your GitHub OAuth key."
|
||||
return
|
||||
fi
|
||||
|
||||
# In case cmd is composer run it with composer image
|
||||
if [ "$cmd" == "composer" ]; then
|
||||
if [ "$log_level" == "0" ] || [ "$log_level" == "-1" ]; then
|
||||
printfe "%s" "cyan" "Running '"
|
||||
printfe "%s" "yellow" "$cmd $params"
|
||||
printfe "%s" "cyan" "' in "
|
||||
printfe "%s" "yellow" "'$composer_image'"
|
||||
printfe "%s\n" "cyan" " container..."
|
||||
fi
|
||||
|
||||
docker run --rm --interactive --tty \
|
||||
--volume $PWD:/app \
|
||||
--volume $AUTH_SOCK_DIRNAME \
|
||||
--volume /etc/passwd:/etc/passwd:ro \
|
||||
--volume /etc/group:/etc/group:ro \
|
||||
--volume /home/$USER/.ssh:/root/.ssh \
|
||||
--volume /home/$USER/.config/composer/auth.json:/tmp/auth.json \
|
||||
--env SSH_AUTH_SOCK=$SSH_AUTH_SOCK \
|
||||
--user $(id -u):$(id -g) \
|
||||
$composer_image $cmd $params
|
||||
elif [ "$cmd" == "php" ]; then
|
||||
if [ "$log_level" == "0" ] || [ "$log_level" == "-1" ]; then
|
||||
printfe "%s" "cyan" "Running '"
|
||||
printfe "%s" "yellow" "$cmd $params"
|
||||
printfe "%s" "cyan" "' in "
|
||||
printfe "%s" "yellow" "'$php_image'"
|
||||
printfe "%s\n" "cyan" " container..."
|
||||
fi
|
||||
|
||||
docker run --rm --interactive --tty \
|
||||
--volume $PWD:/app \
|
||||
--volume $AUTH_SOCK_DIRNAME \
|
||||
--volume /etc/passwd:/etc/passwd:ro \
|
||||
--volume /etc/group:/etc/group:ro \
|
||||
--volume /home/$USER/.ssh:/root/.ssh \
|
||||
--volume /home/$USER/.config/composer/auth.json:/tmp/auth.json \
|
||||
--env SSH_AUTH_SOCK=$SSH_AUTH_SOCK \
|
||||
--user $(id -u):$(id -g) \
|
||||
$php_image $cmd $params
|
||||
elif [ "$cmd" == "phpstan" ]; then
|
||||
if [ "$log_level" == "0" ] || [ "$log_level" == "-1" ]; then
|
||||
printfe "%s" "cyan" "Running '"
|
||||
printfe "%s" "yellow" "$cmd $params"
|
||||
printfe "%s" "cyan" "' in "
|
||||
printfe "%s" "yellow" "'$phpstan_image'"
|
||||
printfe "%s\n" "cyan" " container..."
|
||||
fi
|
||||
|
||||
docker run --rm --interactive --tty \
|
||||
--volume $PWD:/app \
|
||||
--user $(id -u):$(id -g) \
|
||||
$phpstan_image $params
|
||||
else
|
||||
println "No container found named $container and given command is not composer or php." "red"
|
||||
fi
|
||||
return
|
||||
fi
|
||||
|
||||
docker_user=docker
|
||||
|
||||
if [ "$log_level" == "0" ] || [ "$log_level" == "-1" ]; then
|
||||
printfe "%s" "cyan" "Running '"
|
||||
printfe "%s" "yellow" "$cmd $params"
|
||||
printfe "%s" "cyan" "' in "
|
||||
printfe "%s" "yellow" "'$container'"
|
||||
printfe "%s\n" "cyan" " container..."
|
||||
fi
|
||||
docker compose -f $TRADAWARE_PATH/docker-compose.yml exec -u $docker_user --interactive --tty $container $cmd $params
|
||||
}
|
||||
48
bin/scripts/build-binary.sh
Executable file
48
bin/scripts/build-binary.sh
Executable file
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
BINARY_NAME=$1
|
||||
BINARY_PATH=$2
|
||||
COMPLETION_SCRIPT=$3
|
||||
BINARY_PATH_VERSION=$BINARY_PATH.version
|
||||
|
||||
source bin/helpers/func.sh
|
||||
|
||||
# Check if HEAD is clean, if not abort
|
||||
if [ -n "$(git status --porcelain)" ]; then
|
||||
printfe "%s\n" "yellow" "You have uncomitted and/or untracked changes in your working directory."
|
||||
fi
|
||||
|
||||
# Get the current tag checked out to HEAD and hash
|
||||
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null)
|
||||
LATEST_COMMIT_HASH=$(git rev-parse --short HEAD 2>/dev/null)
|
||||
LATEST_TAG_HASH=$(git rev-list -n 1 --abbrev-commit $LATEST_TAG 2>/dev/null)
|
||||
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
|
||||
|
||||
# If BRANCH is HEAD and latest commit hash equals latest tag hash, we are on a tag and up to date
|
||||
if [ "$BRANCH" == "HEAD" ] && [ "$LATEST_COMMIT_HASH" == "$LATEST_TAG_HASH" ]; then
|
||||
BRANCH=$LATEST_TAG
|
||||
fi
|
||||
|
||||
# In case the current head has uncomitted and/or untracked changes, append a postfix to the version saying (dirty)
|
||||
if [ -n "$(git status --porcelain)" ]; then
|
||||
POSTFIX=" (dirty)"
|
||||
fi
|
||||
|
||||
printfe "%s\n" "cyan" "Building $BINARY_NAME binary for $BRANCH ($LATEST_COMMIT_HASH)$POSTFIX..."
|
||||
go build -o $BINARY_PATH
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
printf "\033[0;31m"
|
||||
echo "Build failed."
|
||||
printf "\033[0m"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Put tag and hash in .sshtunnel_version file
|
||||
echo "$BRANCH ($LATEST_COMMIT_HASH)$POSTFIX" > $BINARY_PATH_VERSION
|
||||
|
||||
printfe "%s\n" "cyan" "Generating Bash completion script..."
|
||||
$BINARY_PATH completion bash > $COMPLETION_SCRIPT
|
||||
|
||||
printfe "%s\n" "green" "Bash completion script installed to $COMPLETION_SCRIPT."
|
||||
printfe "%s\n" "green" "Restart or 'source ~/.bashrc' to update your shell."
|
||||
19
bin/scripts/clean.sh
Executable file
19
bin/scripts/clean.sh
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
source bin/helpers/func.sh
|
||||
|
||||
# $1 should be binary path
|
||||
BINARY_PATH=$1
|
||||
|
||||
# $2 should be completion script path
|
||||
COMPLETION_SCRIPT=$2
|
||||
|
||||
# Confirm these are paths
|
||||
if [ -z "$BINARY_PATH" ] || [ -z "$COMPLETION_SCRIPT" ]; then
|
||||
printfe "%s\n" "red" "Usage: $0 <binary_path> <completion_script_path>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printfe "%s\n" "cyan" "Cleaning up old binaries and completion scripts..."
|
||||
rm -f $BINARY_PATH
|
||||
rm -f $COMPLETION_SCRIPT
|
||||
18
bin/scripts/install-local.sh
Executable file
18
bin/scripts/install-local.sh
Executable file
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
source bin/helpers/func.sh
|
||||
|
||||
# Create any missing directories/files
|
||||
touch ~/.bash_completion
|
||||
mkdir -p $HOME/.local/bin/
|
||||
|
||||
# Symbolically link binaries
|
||||
ln -sf $(pwd)/bin/sshtunnel $HOME/.local/bin/sshtunnel
|
||||
ln -sf $(pwd)/bin/sshtunnel-completion.bash $HOME/.local/bin/sshtunnel-completion.bash
|
||||
|
||||
# Add completion to bash_completion for sshtunnel
|
||||
sed -i '/sshtunnel/d' ~/.bash_completion
|
||||
echo "source $HOME/.local/bin/sshtunnel-completion.bash" >> ~/.bash_completion
|
||||
|
||||
printfe "%s\n" "green" "Local installation complete. Binary has been installed to $HOME/.local/bin/sshtunnel"
|
||||
source ~/.bash_completion
|
||||
49
bin/scripts/install.sh
Executable file
49
bin/scripts/install.sh
Executable file
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
source bin/helpers/func.sh
|
||||
|
||||
# Test for root privileges
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
printfe "%s\n" "red" "Please run as root"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Firstly compile the sshtunnel binary
|
||||
printfe "%s\n" "cyan" "Compiling sshtunnel..."
|
||||
MAKE_OUTPUT=$(make 2>&1)
|
||||
MAKE_EXIT_CODE=$?
|
||||
if [ $MAKE_EXIT_CODE -ne 0 ]; then
|
||||
printfe "%s\n" "red" "Compilation failed. Please check the output below."
|
||||
echo "$MAKE_OUTPUT"
|
||||
exit 1
|
||||
fi
|
||||
printfe "%s\n" "green" "Compilation successful."
|
||||
|
||||
# Remove any existing sshtunnel installation
|
||||
printfe "%s\n" "cyan" "Removing existing sshtunnel installation..."
|
||||
if [ -f "/usr/local/bin/sshtunnel" ]; then
|
||||
log_and_run rm /usr/local/bin/sshtunnel
|
||||
fi
|
||||
if [ -f "/usr/share/bash-completion/completions/sshtunnel" ]; then
|
||||
log_and_run rm /usr/share/bash-completion/completions/sshtunnel
|
||||
fi
|
||||
if [ -f "/usr/local/share/sshtunnel/sshtunnel.version" ]; then
|
||||
log_and_run rm /usr/local/share/sshtunnel/sshtunnel.version
|
||||
fi
|
||||
|
||||
# Copy binary files to /usr/local/bin
|
||||
printfe "%s\n" "cyan" "Installing sshtunnel..."
|
||||
log_and_run cp $(pwd)/bin/sshtunnel /usr/local/bin/sshtunnel
|
||||
log_and_run cp $(pwd)/bin/sshtunnel-completion.bash /usr/share/bash-completion/completions/sshtunnel
|
||||
|
||||
# Copy version file to /usr/local/share/sshtunnel/sshtunnel.version
|
||||
mkdir -p /usr/local/share/sshtunnel
|
||||
log_and_run cp $(pwd)/bin/sshtunnel.version /usr/local/share/sshtunnel/sshtunnel.version
|
||||
|
||||
# Clean up any compiled files
|
||||
printfe "%s\n" "cyan" "Cleaning up..."
|
||||
log_and_run rm $(pwd)/bin/sshtunnel
|
||||
log_and_run rm $(pwd)/bin/sshtunnel-completion.bash
|
||||
log_and_run rm $(pwd)/bin/sshtunnel.version
|
||||
|
||||
printfe "%s\n" "green" "Installation complete."
|
||||
36
bin/scripts/uninstall.sh
Executable file
36
bin/scripts/uninstall.sh
Executable file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
source bin/helpers/func.sh
|
||||
|
||||
# Test for root privileges if uninstalling system-wide files
|
||||
NEED_ROOT=0
|
||||
if [ -f /usr/local/bin/sshtunnel ] || [ -f /usr/share/bash-completion/completions/sshtunnel ] || [ -f /usr/local/share/sshtunnel/sshtunnel.version ]; then
|
||||
NEED_ROOT=1
|
||||
fi
|
||||
if [ $NEED_ROOT -eq 1 ] && [ "$EUID" -ne 0 ]; then
|
||||
printfe "%s\n" "red" "Please run as root"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Remove user-local files
|
||||
printfe "%s\n" "cyan" "Removing sshtunnel from user-local locations..."
|
||||
if [ -f $HOME/.local/bin/sshtunnel ]; then
|
||||
log_and_run rm $HOME/.local/bin/sshtunnel
|
||||
fi
|
||||
if [ -f $HOME/.local/bin/sshtunnel-completion.bash ]; then
|
||||
log_and_run rm $HOME/.local/bin/sshtunnel-completion.bash
|
||||
fi
|
||||
|
||||
# Remove system-wide files using log_and_run
|
||||
printfe "%s\n" "cyan" "Removing sshtunnel from system-wide locations..."
|
||||
if [ -f /usr/share/bash-completion/completions/sshtunnel ]; then
|
||||
log_and_run rm /usr/share/bash-completion/completions/sshtunnel
|
||||
fi
|
||||
if [ -f /usr/local/bin/sshtunnel ]; then
|
||||
log_and_run rm /usr/local/bin/sshtunnel
|
||||
fi
|
||||
if [ -f /usr/local/share/sshtunnel/sshtunnel.version ]; then
|
||||
log_and_run rm /usr/local/share/sshtunnel/sshtunnel.version
|
||||
fi
|
||||
|
||||
printfe "%s\n" "green" "Uninstall complete."
|
||||
Reference in New Issue
Block a user