-
Notifications
You must be signed in to change notification settings - Fork 10
refactor: Shared lib #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
M03ED
wants to merge
9
commits into
main
Choose a base branch
from
shared_lib
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0d37ec2
refactor: lib directory and handle duplicate functions once
M03ED d22afb5
refactor: github, system and docker shared lib
M03ED 1b9f30c
feat: handle temp file usage
M03ED 273b4d4
feat: move compose files
M03ED fdd8513
feat: separate backup and restore bash code
M03ED edde1ab
fix
M03ED a61d3dc
fix
M03ED a021ba5
fix: emoji
M03ED cd4e860
fix
M03ED File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| colorized_echo() { | ||
| local color="$1" | ||
| local text="$2" | ||
| local style="${3:-0}" | ||
|
|
||
| case "$color" in | ||
| red) | ||
| printf "\e[${style};91m%s\e[0m\n" "$text" | ||
| ;; | ||
| green) | ||
| printf "\e[${style};92m%s\e[0m\n" "$text" | ||
| ;; | ||
| yellow) | ||
| printf "\e[${style};93m%s\e[0m\n" "$text" | ||
| ;; | ||
| blue) | ||
| printf "\e[${style};94m%s\e[0m\n" "$text" | ||
| ;; | ||
| magenta) | ||
| printf "\e[${style};95m%s\e[0m\n" "$text" | ||
| ;; | ||
| cyan) | ||
| printf "\e[${style};96m%s\e[0m\n" "$text" | ||
| ;; | ||
| *) | ||
| printf "%s\n" "$text" | ||
| ;; | ||
| esac | ||
| } | ||
|
|
||
| die() { | ||
| colorized_echo red "$*" | ||
| exit 1 | ||
| } | ||
|
|
||
| temp_root_dir() { | ||
| local root="" | ||
|
|
||
| if [ -n "${APP_TMP_DIR:-}" ]; then | ||
| root="$APP_TMP_DIR" | ||
| elif [ -n "${DATA_DIR:-}" ]; then | ||
| root="$DATA_DIR/tmp" | ||
| elif [ -n "${APP_NAME:-}" ]; then | ||
| root="/var/lib/$APP_NAME/tmp" | ||
| else | ||
| root="/var/lib/pasarguard-scripts/tmp" | ||
| fi | ||
|
|
||
| mkdir -p "$root" | ||
| printf '%s\n' "$root" | ||
| } | ||
|
|
||
| create_temp_dir() { | ||
| local prefix="${1:-tmpdir}" | ||
| local root="" | ||
| local candidate="" | ||
| local attempt=0 | ||
|
|
||
| root=$(temp_root_dir) | ||
| while [ "$attempt" -lt 20 ]; do | ||
| candidate="${root}/${prefix}-$$-${RANDOM}-${attempt}" | ||
| if mkdir "$candidate" 2>/dev/null; then | ||
| printf '%s\n' "$candidate" | ||
| return 0 | ||
| fi | ||
| attempt=$((attempt + 1)) | ||
| done | ||
|
|
||
| die "Failed to create temporary directory in $root" | ||
| } | ||
|
|
||
| create_temp_file() { | ||
| local prefix="${1:-tmpfile}" | ||
| local suffix="${2:-}" | ||
| local root="" | ||
|
|
||
| root=$(temp_root_dir) | ||
| create_temp_file_in_dir "$root" "$prefix" "$suffix" | ||
| } | ||
|
|
||
| create_temp_file_in_dir() { | ||
| local dir="$1" | ||
| local prefix="${2:-tmpfile}" | ||
| local suffix="${3:-}" | ||
| local candidate="" | ||
| local attempt=0 | ||
|
|
||
| mkdir -p "$dir" | ||
| while [ "$attempt" -lt 20 ]; do | ||
| candidate="${dir}/${prefix}-$$-${RANDOM}-${attempt}${suffix}" | ||
| if (set -C; : >"$candidate") 2>/dev/null; then | ||
| printf '%s\n' "$candidate" | ||
| return 0 | ||
| fi | ||
| attempt=$((attempt + 1)) | ||
| done | ||
|
|
||
| die "Failed to create temporary file in $dir" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| install_docker() { | ||
| colorized_echo blue "Installing Docker" | ||
| if ! bash -o pipefail -c 'curl -fsSL https://get.docker.com | sh'; then | ||
| die "Failed to install Docker" | ||
| fi | ||
| colorized_echo green "Docker installed successfully" | ||
| } | ||
|
|
||
| detect_compose() { | ||
| if docker compose version >/dev/null 2>&1; then | ||
| COMPOSE='docker compose' | ||
| elif docker-compose version >/dev/null 2>&1; then | ||
| COMPOSE='docker-compose' | ||
| else | ||
| die "docker compose not found" | ||
| fi | ||
| } | ||
|
|
||
| compose_up() { | ||
| $COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" up -d --remove-orphans | ||
| } | ||
|
|
||
| compose_down() { | ||
| $COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" down | ||
| } | ||
|
|
||
| compose_logs() { | ||
| $COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" logs | ||
| } | ||
|
|
||
| compose_logs_follow() { | ||
| $COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" logs -f | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| replace_or_append_env_var() { | ||
| local key="$1" | ||
| local value="$2" | ||
| local quote_value="${3:-false}" | ||
| local target_file="${4:-$ENV_FILE}" | ||
| local formatted_value="$value" | ||
| local escaped_value="" | ||
|
|
||
| if [ "$quote_value" = "true" ]; then | ||
| local sanitized_value="${value//\"/\\\"}" | ||
| formatted_value="\"$sanitized_value\"" | ||
| fi | ||
|
|
||
| escaped_value=$(printf '%s' "$formatted_value" | sed -e 's/[&|\\]/\\&/g') | ||
|
|
||
| if grep -q "^$key=" "$target_file"; then | ||
| sed -i "s|^$key=.*|$key=$escaped_value|" "$target_file" | ||
| else | ||
| printf '%s=%s\n' "$key" "$formatted_value" >>"$target_file" | ||
| fi | ||
| } | ||
|
|
||
| set_or_uncomment_env_var() { | ||
| local key="$1" | ||
| local value="$2" | ||
| local quote_value="${3:-false}" | ||
| local target_file="${4:-$ENV_FILE}" | ||
| local formatted_value="$value" | ||
| local tmp_file="" | ||
| local target_dir="" | ||
|
|
||
| if [ "$quote_value" = "true" ]; then | ||
| local sanitized_value="${value//\"/\\\"}" | ||
| formatted_value="\"$sanitized_value\"" | ||
| fi | ||
|
|
||
| [ -f "$target_file" ] || touch "$target_file" | ||
| target_dir=$(dirname "$target_file") | ||
| tmp_file=$(create_temp_file_in_dir "$target_dir" "env-edit" ".tmp") | ||
|
|
||
| awk -v env_key="$key" -v env_line="${key} = ${formatted_value}" ' | ||
| BEGIN { replaced = 0 } | ||
| { | ||
| if ($0 ~ "^[[:space:]]*#?[[:space:]]*" env_key "[[:space:]]*=") { | ||
| if (replaced == 0) { | ||
| print env_line | ||
| replaced = 1 | ||
| } | ||
| next | ||
| } | ||
| } | ||
| END { | ||
| if (replaced == 0) { | ||
| print env_line | ||
| } | ||
| } | ||
| ' "$target_file" >"$tmp_file" | ||
|
|
||
| mv "$tmp_file" "$target_file" | ||
| } | ||
|
|
||
| comment_out_env_var() { | ||
| local key="$1" | ||
| local target_file="${2:-$ENV_FILE}" | ||
| local tmp_file="" | ||
| local target_dir="" | ||
|
|
||
| [ -f "$target_file" ] || return 0 | ||
| target_dir=$(dirname "$target_file") | ||
| tmp_file=$(create_temp_file_in_dir "$target_dir" "env-comment" ".tmp") | ||
|
|
||
| awk -v env_key="$key" ' | ||
| BEGIN { done = 0 } | ||
| { | ||
| if ($0 ~ "^[[:space:]]*#?[[:space:]]*" env_key "[[:space:]]*=") { | ||
| if (done == 0) { | ||
| line = $0 | ||
| sub("^[[:space:]]*#?[[:space:]]*" env_key "[[:space:]]*=[[:space:]]*", "", line) | ||
| print "# " env_key " = " line | ||
| done = 1 | ||
| } | ||
| next | ||
| } | ||
| } | ||
| ' "$target_file" >"$tmp_file" | ||
|
|
||
| mv "$tmp_file" "$target_file" | ||
| } | ||
|
|
||
| delete_env_var() { | ||
| local key="$1" | ||
| local target_file="${2:-$ENV_FILE}" | ||
|
|
||
| [ -f "$target_file" ] || return 0 | ||
| sed -i "/^[[:space:]]*${key}[[:space:]]*=/d" "$target_file" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| SHARED_LIB_INSTALL_DIR="/usr/local/lib/pasarguard-scripts/lib" | ||
|
|
||
| github_raw_url() { | ||
| local repo="$1" | ||
| local path="$2" | ||
|
|
||
| printf 'https://github.com/%s/raw/main/%s\n' "$repo" "$path" | ||
| } | ||
|
|
||
| github_download_file() { | ||
| local url="$1" | ||
| local target_path="$2" | ||
|
|
||
| curl -fsSL "$url" -o "$target_path" | ||
| } | ||
|
|
||
| github_install_script_from_repo() { | ||
| local repo="$1" | ||
| local script_name="$2" | ||
| local install_name="$3" | ||
| local tmp_file="" | ||
|
|
||
| tmp_file=$(mktemp) || return 1 | ||
| trap 'rm -f "$tmp_file"' RETURN | ||
|
|
||
| if ! curl -fSL "$(github_raw_url "$repo" "$script_name")" -o "$tmp_file"; then | ||
| trap - RETURN | ||
| rm -f "$tmp_file" | ||
| return 1 | ||
| fi | ||
|
|
||
| if ! chmod 755 "$tmp_file"; then | ||
| trap - RETURN | ||
| rm -f "$tmp_file" | ||
| return 1 | ||
| fi | ||
|
|
||
| if ! install -m 755 "$tmp_file" "/usr/local/bin/$install_name"; then | ||
| trap - RETURN | ||
| rm -f "$tmp_file" | ||
| return 1 | ||
| fi | ||
|
|
||
| trap - RETURN | ||
| rm -f "$tmp_file" | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| install_shared_libs_from_local() { | ||
| local source_dir="$1" | ||
| shift | ||
| local lib_name="" | ||
|
|
||
| mkdir -p "$SHARED_LIB_INSTALL_DIR" | ||
| for lib_name in "$@"; do | ||
| if [ -f "$source_dir/lib/$lib_name" ]; then | ||
| install -m 644 "$source_dir/lib/$lib_name" "$SHARED_LIB_INSTALL_DIR/$lib_name" | ||
| fi | ||
| done | ||
| } | ||
|
|
||
| install_shared_libs_from_repo() { | ||
| local fetch_repo="$1" | ||
| shift | ||
| local tmp_dir="" | ||
| local lib_name="" | ||
|
|
||
| tmp_dir=$(create_temp_dir "shared-libs") | ||
| mkdir -p "$SHARED_LIB_INSTALL_DIR" | ||
|
|
||
| for lib_name in "$@"; do | ||
| github_download_file "$(github_raw_url "$fetch_repo" "lib/$lib_name")" "$tmp_dir/$lib_name" | ||
| install -m 644 "$tmp_dir/$lib_name" "$SHARED_LIB_INSTALL_DIR/$lib_name" | ||
| done | ||
|
|
||
| rm -rf "$tmp_dir" | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.