Skip to content

Commit 1074ddf

Browse files
authored
Update notifiarr-branch-builder.sh (#32)
1 parent 7cdaa3f commit 1074ddf

File tree

1 file changed

+54
-94
lines changed

1 file changed

+54
-94
lines changed

notifiarr-branch-builder.sh

Lines changed: 54 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,99 @@
11
#!/usr/bin/env bash
2+
set -euo pipefail
23

3-
# Extend the PATH to include the go binary directory
44
export PATH=$PATH:/usr/local/go/bin
55

6-
# Function to display error messages and exit with status 1
76
handle_error() {
87
echo "Error: $1" >&2
98
exit 1
109
}
1110

12-
# Function to display usage information
1311
display_help() {
14-
echo "Usage: $0 [options]"
15-
echo "Options:"
16-
echo " -h, --help Display this help message"
17-
echo " --repo-url URL Set the repository URL (default: https://github.com/Notifiarr/notifiarr.git)"
18-
echo " --repo-dir DIR Set the repository directory (default: /opt/notifiarr-repo)"
19-
echo " --bin-path PATH Set the binary path (default: /usr/bin/notifiarr)"
20-
echo " --branch BRANCH Set the branch (default: master)"
21-
echo " --reinstall-apt Reinstall Notifiarr using apt without prompting."
12+
cat <<EOF
13+
Usage: $0 [options]
14+
Options:
15+
-h, --help Show this help message
16+
--repo-url URL Git repository URL (default: https://github.com/Notifiarr/notifiarr.git)
17+
--repo-dir DIR Directory to clone or update repo (default: /opt/notifiarr-repo)
18+
--bin-path PATH Destination for compiled binary (default: /usr/bin/notifiarr)
19+
--branch BRANCH Git branch to checkout (default: master)
20+
--reinstall-apt Reinstall Notifiarr using apt without prompt
21+
EOF
2222
exit 0
2323
}
2424

25-
# Function to check and prompt for installation of a required tool
2625
ensure_tool_installed() {
27-
local tool=$1
28-
local install_cmd=$2
29-
if ! command -v "$tool" &>/dev/null; then
30-
read -r -p "$tool is not installed. Do you want to install it? [Y/n] " response
31-
if [[ "$response" =~ ^[Yy] ]]; then
32-
eval "$install_cmd" || handle_error "Failed to install $tool."
33-
else
34-
echo "$tool is required for this script. Exiting."
35-
exit 1
36-
fi
37-
fi
26+
local tool="$1" install_cmd="$2"
27+
command -v "$tool" &>/dev/null && return
28+
read -rp "$tool is not installed. Install it? [Y/n] " r
29+
[[ "$r" =~ ^[Yy] ]] && eval "$install_cmd" || handle_error "$tool is required"
3830
}
3931

40-
# Default parameters
4132
repo_url="https://github.com/Notifiarr/notifiarr.git"
4233
repo_dir="/opt/notifiarr-repo"
4334
bin_path="/usr/bin/notifiarr"
4435
branch="master"
36+
branch_explicit=false
4537
apt_reinstall=false
4638

47-
# Parse command line options
4839
while [[ $# -gt 0 ]]; do
4940
case "$1" in
50-
-h | --help)
51-
display_help
52-
;;
53-
--repo-url)
54-
repo_url="$2"
55-
shift
56-
;;
57-
--repo-dir)
58-
repo_dir="$2"
59-
shift
60-
;;
61-
--bin-path)
62-
bin_path="$2"
63-
shift
64-
;;
65-
--branch)
66-
branch="$2"
67-
shift
68-
;;
69-
--reinstall-apt)
70-
apt_reinstall=true
71-
;;
72-
*)
73-
echo "Invalid option: $1. Use -h for help."
74-
exit 1
75-
;;
41+
-h|--help) display_help ;;
42+
--repo-url) repo_url="$2"; shift ;;
43+
--repo-dir) repo_dir="$2"; shift ;;
44+
--bin-path) bin_path="$2"; shift ;;
45+
--branch) branch="$2"; branch_explicit=true; shift ;;
46+
--reinstall-apt) apt_reinstall=true ;;
47+
*) handle_error "Invalid option: $1. Use --help for usage." ;;
7648
esac
7749
shift
7850
done
7951

80-
# Ensure required tools are installed
8152
ensure_tool_installed "make" "sudo apt update && sudo apt install -y make"
53+
ensure_tool_installed "git" "sudo apt update && sudo apt install -y git"
54+
ensure_tool_installed "go" "sudo apt update && sudo apt install -y golang"
8255

83-
# Reinstallation condition handling
84-
reinstall_notifiarr() {
85-
# shellcheck disable=SC2015
86-
sudo apt update && sudo apt install --reinstall notifiarr || handle_error "Failed to reinstall Notifiarr using apt."
56+
$apt_reinstall && {
57+
sudo apt update
58+
sudo apt install --reinstall -y notifiarr || handle_error "APT reinstall failed"
59+
exit 0
8760
}
8861

89-
[[ $apt_reinstall == true ]] && reinstall_notifiarr
90-
91-
# Repository management
92-
if [[ ! -d "$repo_dir" ]]; then
93-
git clone "$repo_url" "$repo_dir" || handle_error "Failed to clone repository."
62+
if [[ ! -d "$repo_dir/.git" ]]; then
63+
git clone "$repo_url" "$repo_dir" || handle_error "Git clone failed"
9464
else
95-
git -C "$repo_dir" fetch --all --prune || handle_error "Failed to fetch updates from remote."
65+
git -C "$repo_dir" fetch --all --prune || handle_error "Git fetch failed"
9666
fi
9767

98-
# Branch handling and updating
99-
current_branch=$(git -C "$repo_dir" rev-parse --abbrev-ref HEAD)
100-
read -r -p "Do you want to use the current branch ($current_branch)? [Y/n] " choice
101-
if [[ "$choice" =~ ^[Nn] ]]; then
102-
branches=$(git -C "$repo_dir" branch -r | sed 's/origin\///;s/* //')
103-
echo "Available branches:"
104-
echo "$branches"
105-
while true; do
106-
read -r -p "Enter the branch name you want to use: " branch
107-
if [[ $branches =~ $branch ]]; then
108-
git -C "$repo_dir" checkout "$branch" || handle_error "Failed to checkout branch $branch."
109-
break
110-
else
111-
echo "Invalid choice. Please select a valid branch."
112-
fi
113-
done
68+
cd "$repo_dir"
69+
current_branch=$(git rev-parse --abbrev-ref HEAD)
70+
71+
if ! $branch_explicit; then
72+
read -rp "Use current branch ($current_branch)? [Y/n] " answer
73+
if [[ "$answer" =~ ^[Nn] ]]; then
74+
echo "Available branches:"
75+
git branch -r | sed 's|origin/||' | grep -vE 'HEAD|->'
76+
read -rp "Enter branch: " branch
77+
else
78+
branch="$current_branch"
79+
fi
11480
fi
11581

116-
git -C "$repo_dir" pull || handle_error "Failed to pull latest changes."
117-
make --directory="$repo_dir" || handle_error "Failed to compile."
82+
git checkout "$branch" || handle_error "Git checkout failed"
83+
git pull || handle_error "Git pull failed"
11884

119-
# Service management
120-
echo "Stopping notifiarr..."
121-
sudo systemctl stop notifiarr
85+
make || handle_error "Make failed"
12286

123-
if [[ -f "$bin_path" ]]; then
124-
sudo mv "$bin_path" "$repo_dir".old && echo "Old binary moved to $repo_dir.old"
125-
fi
87+
sudo systemctl stop notifiarr || true
12688

127-
sudo mv "$repo_dir/notifiarr" "$bin_path" && echo "New binary moved to $bin_path"
89+
[[ -f "$bin_path" ]] && sudo mv "$bin_path" "${bin_path}.old"
90+
sudo mv "$repo_dir/notifiarr" "$bin_path"
12891
sudo chown root:root "$bin_path"
12992

130-
echo "Starting Notifiarr..."
131-
sudo systemctl start notifiarr
93+
sudo systemctl start notifiarr || handle_error "Service failed to start"
13294

133-
if sudo systemctl is-active quiet notifiarr; then
134-
echo "Notifiarr service started and is currently running"
95+
if systemctl is-active --quiet notifiarr; then
96+
echo "Notifiarr service is running."
13597
else
136-
handle_error "Failed to start Notifiarr service"
98+
handle_error "Notifiarr service is not running."
13799
fi
138-
139-
exit 0

0 commit comments

Comments
 (0)