commit fac02942f8f98065946c2dc6e7b0588059aaeaa1
parent 2cf7a15fb4b93a44af0f74b7f55d4859c388f83d
Author: Fred Großkopf <fred@kuandu.systems>
Date: Thu, 17 Sep 2026 18:05:41 +0200
Migrate setup command
Diffstat:
| M | gitpages-cmd.sh | | | 113 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
| D | gitpages-init-repos.sh | | | 156 | ------------------------------------------------------------------------------- |
2 files changed, 112 insertions(+), 157 deletions(-)
diff --git a/gitpages-cmd.sh b/gitpages-cmd.sh
@@ -75,6 +75,7 @@ check_deps() {
command -v git > /dev/null 2>&1 || error "git not found"
}
+
# --- Core Logic ---
generate_repo_html() {
rm -rf "$STAGING_DIR"
@@ -170,8 +171,118 @@ generate_repo() {
info "Generation complete for $REPO_BASE"
}
+setup_ask() {
+ prompt="$1"
+ current="${2:-}"
+
+ if [ -t 0 ]; then
+ if [ -n "$current" ]; then
+ printf '%s [%s]: ' "$prompt" "$current" >&2
+ else
+ printf '%s: ' "$prompt" >&2
+ fi
+ fi
+
+ read -r answer
+ case "$answer" in
+ "") printf '%s\n' "$current" ;;
+ *) printf '%s\n' "$answer" ;;
+ esac
+}
+
+setup_install_hook() {
+ repo_dir="$1"
+
+ hook_dir="$repo_dir/hooks"
+ hook_file="$hook_dir/post-receive"
+
+ mkdir -p "$hook_dir"
+ cp "$POST_RECEIVE" "$hook_file"
+ chmod 755 "$hook_file"
+
+ info "Installed post-receive hook in $repo_dir"
+}
+
+setup_repo() {
+ repo="$1"
+ repo_name=$(basename "$repo" .git)
+
+ desc_file="$repo/description"
+ owner_file="$repo/owner"
+ url_file="$repo/url"
+
+ current_desc=""
+ [ -f "$desc_file" ] && current_desc="$(cat "$desc_file")"
+
+ current_owner=""
+ [ -f "$owner_file" ] && current_owner="$(cat "$owner_file")"
+
+ current_url=""
+ [ -f "$url_file" ] && current_url="$(cat "$url_file")"
+
+ printf '## Repository: %s ##\n' "$repo_name"
+
+ new_desc=$(setup_ask "Description" "$current_desc")
+ if [ -n "$new_desc" ]; then
+ printf '%s\n' "$new_desc" > "$desc_file"
+ fi
+
+ new_owner=$(setup_ask "Owner/Contact" "$current_owner")
+ if [ -n "$new_owner" ]; then
+ printf '%s\n' "$new_owner" > "$owner_file"
+ fi
+
+ new_url=$(setup_ask "URL" "$current_url")
+ if [ -n "$new_url" ]; then
+ printf '%s\n' "$new_url" > "$url_file"
+ fi
+
+ setup_install_hook "$repo"
+}
+
+setup_all() {
+ GIT_SRC="${1:-}"
+
+ if [ -z "$GIT_SRC" ]; then
+ CONFIG="$DEFAULT_CONFIG"
+
+ [ -r "$CONFIG" ] ||
+ error "Config file $CONFIG not readable"
+
+ GIT_SRC=$(get_config_val "GIT_SRC")
+
+ [ -n "$GIT_SRC" ] ||
+ error "GIT_SRC not set in $CONFIG"
+ fi
+
+ [ -d "$GIT_SRC" ] ||
+ error "Directory '$GIT_SRC' not found"
+
+ POST_RECEIVE="$SCRIPT_DIR/post-receive.hook"
+
+ [ -f "$POST_RECEIVE" ] ||
+ error "post-receive helper not found in $SCRIPT_DIR"
+
+ for repo in "$GIT_SRC"/*.git; do
+ [ -d "$repo" ] || continue
+ setup_repo "$repo"
+ done
+
+ info "Setup complete for all repos in $GIT_SRC"
+}
+
cmd_setup() {
- "$SCRIPT_DIR/gitpages-init-repos.sh" "$@"
+ case "$#" in
+ 0)
+ setup_all
+ ;;
+ 1)
+ setup_all "$1"
+ ;;
+ *)
+ error "usage: gitpages setup [SRC]"
+ ;;
+ esac
}
cmd_process_queue() {
diff --git a/gitpages-init-repos.sh b/gitpages-init-repos.sh
@@ -1,156 +0,0 @@
-#!/bin/sh
-#
-# Copyright (c) 2026 Fred Großkopf
-#
-# Permission to use, copy, modify, and/or distribute this software for any
-# purpose with or without fee is hereby granted, provided that the above
-# copyright notice and this permission notice appear in all copies.
-#
-# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
-set -eu
-
-log() { printf 'info: %s\n' "$1"; }
-warn() { printf 'warning: %s\n' "$1" >&2; }
-error() {
- printf 'error: %s\n' "$1" >&2
- exit 1
-}
-
-# Global temp directory (set once in main)
-WORKDIR=""
-
-cleanup() { [ -n "${WORKDIR-}" ] && rm -rf "$WORKDIR"; }
-trap cleanup EXIT INT TERM
-
-ask() {
- prompt="$1"
- current="${2:-}"
-
- # Only show prompt if stdin is a terminal
- if [ -t 0 ]; then
- if [ -n "$current" ]; then
- printf '%s [%s]: ' "$prompt" "$current" >&2
- else
- printf '%s: ' "$prompt" >&2
- fi
- fi
-
- read -r answer
- case "$answer" in
- "") printf '%s\n' "$current" ;;
- *) printf '%s\n' "$answer" ;;
- esac
-}
-
-update_post_receive_hook() {
- repo_dir="$1"
-
-
- # Install hook into repo
- hook_dir="$repo_dir/hooks"
- hook_file="$hook_dir/post-receive"
-
- mkdir -p "$hook_dir"
- cp "$POST_RECEIVE" "$hook_file"
- chmod 755 "$hook_file"
- log "Installed post-receive hook in $repo_dir"
-}
-
-process_repo() {
- repo="$1"
- repo_name=$(basename "$repo" .git)
-
- desc_file="$repo/description"
- owner_file="$repo/owner"
- url_file="$repo/url"
-
- # Read current values
- current_desc=""
- [ -f "$desc_file" ] && current_desc="$(cat "$desc_file")"
-
- current_owner=""
- [ -f "$owner_file" ] && current_owner="$(cat "$owner_file")"
-
- current_url=""
- [ -f "$url_file" ] && current_url="$(cat "$url_file")"
-
- # Show repo boundary
- printf '## Repository: %s ##\n' "$repo_name"
-
- # Ask for description
- ask_desc="$WORKDIR/ask_desc"
- ask "Description" "$current_desc" > "$ask_desc"
- new_desc="$(cat "$ask_desc")"
- if [ -n "$new_desc" ]; then
- printf '%s\n' "$new_desc" > "$desc_file"
- fi
-
- # Ask for owner
- ask_owner="$WORKDIR/ask_owner"
- ask "Owner/Contact" "$current_owner" > "$ask_owner"
- new_owner="$(cat "$ask_owner")"
- if [ -n "$new_owner" ]; then
- printf '%s\n' "$new_owner" > "$owner_file"
- fi
-
- # Ask for URL
- ask_url="$WORKDIR/ask_url"
- ask "URL" "$current_url" > "$ask_url"
- new_url="$(cat "$ask_url")"
- if [ -n "$new_url" ]; then
- printf '%s\n' "$new_url" > "$url_file"
- fi
-
- update_post_receive_hook "$repo"
-}
-
-main() {
- # Decide source directory
- if [ $# -eq 1 ]; then
- GIT_SRC="$1"
- else
- CONFIG="/etc/gitpages.conf"
- [ -r "$CONFIG" ] || error "Config file $CONFIG not read. Please create it or run as ./gitpages-init-repos.sh /path/to/repos"
- GIT_SRC=$(awk -F '=' '
- $1 ~ /^[[:space:]]*GIT_SRC[[:space:]]*$/ {
- gsub(/^[[:space:]]+/, "", $2)
- gsub(/[[:space:]]+$/, "", $2)
- print $2
- exit
- }
- ' "$CONFIG")
- [ -n "$GIT_SRC" ] || error "GIT_SRC not set in $CONFIG"
- fi
-
- [ -d "$GIT_SRC" ] || error "Directory '$GIT_SRC' not found"
-
- # Create one working temp dir, removed by trap
- WORKDIR="$(mktemp -d -t gitpages_ask.XXXXXX)" ||
- error "mktemp failed"
-
- # Find script directory and locate post‑receive
- script_dir=$(cd "$(dirname "$0")" && pwd -P)
- POST_RECEIVE="$script_dir/post-receive.hook"
- [ -f "$POST_RECEIVE" ] || error "post-receive helper not found in $script_dir"
-
- # Gather repos into "$@"
- set -- $(find "$GIT_SRC" -maxdepth 1 -name '*.git' -type d)
-
- # Process each repo
- for repo in "$@"; do
- if [ -d "$repo" ]; then
- process_repo "$repo"
- fi
- done
-
- log "Initialization complete for all repos in $GIT_SRC"
-}
-
-main "$@"