[PR #4907] [MERGED] core.func - Centralized Utility and Trap Framework for Community Scripts #5350

Open
opened 2025-11-20 07:05:33 -05:00 by saavagebueno · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/community-scripts/ProxmoxVE/pull/4907
Author: @MickLesk
Created: 6/2/2025
Status: Merged
Merged: 6/2/2025
Merged by: @michelroegl-brunner

Base: mainHead: core_func


📝 Commits (1)

📊 Changes

1 file changed (+544 additions, -0 deletions)

View changed files

misc/core.func (+544 -0)

📄 Description

⚙️ core.func – Centralized Utility and Trap Framework for Community Scripts

This PR introduces a robust and fully modular core.func library for all community-scripts-based projects. It provides a reusable, fault-tolerant core for Bash scripts in Proxmox environments (and beyond).


🔧 Features

🔒 Robust Error and Signal Handling

  • Catches:
    • ERR (command failure)
    • INT (Ctrl+C)
    • TERM (SIGTERM)
    • EXIT (cleanup)
  • Unified error output with contextual messages
  • Spinner-safe cleanup on failure/abort
  • Predefined fallback for undefined wait_for calls

💬 Unified Output System

  • msg_info, msg_ok, msg_error, msg_warn, msg_custom
  • Built-in icons and coloring via ANSI escape sequences
  • msg_custom allows flexible messaging with icons and color
  • Spinner support with start_spinner / stop_spinner using UTF-8 frames

🌐 Curl with Retry and Exit Code Handling

  • curl_handler includes:
    • Retry mechanism (default: 3 attempts)
    • Automatic detection of -o flags
    • Inline feedback with exit code awareness
  • __curl_err_handler maps specific curl exit codes to readable messages

🖥 Terminal Formatting

  • color, formatting, icons defined once and reused
  • Visual structure: Tabs, Bolds, Resets
  • Standardized icon set for Proxmox use cases (e.g., CPU, RAM, Container ID)

📦 System Defaults & Configuration

  • default_vars for retry logic and default modes
  • set_std_mode for toggling verbosity (VERBOSE=yes / no)
  • Safe silent execution via silent()

🧪 Utility Functions

  • run_container_safe: Executes commands inside a container with signal handling
  • check_or_create_swap: Interactive swap creation if not present
  • get_header / header_info: Dynamic header loading for script branding
  • msg_progress: Draws a progress bar based on percentage completion

📚 Benefits

  • Fully POSIX-compatible (Debian, Ubuntu, Alpine)
  • 🧩 Easily included in other *.func or *-install.sh scripts
  • 💥 Fail-fast and recover-safe
  • 🎨 Consistent terminal UX across all scripts

🧱 Intended Usage

This file is meant to be sourced early via:

source <(curl -fsSL https://git.community-scripts.org/community-scripts/ProxmoxVED/raw/branch/main/misc/core.func)
load_functions

Prerequisites (X in brackets)

  • Self-review completed – Code follows project standards.
  • Tested thoroughly – Changes work as expected.
  • No security risks – No hardcoded secrets, unnecessary privilege escalations, or permission issues.

🛠️ Type of Change (X in brackets)

  • New feature – Adds new, non-breaking functionality.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/community-scripts/ProxmoxVE/pull/4907 **Author:** [@MickLesk](https://github.com/MickLesk) **Created:** 6/2/2025 **Status:** ✅ Merged **Merged:** 6/2/2025 **Merged by:** [@michelroegl-brunner](https://github.com/michelroegl-brunner) **Base:** `main` ← **Head:** `core_func` --- ### 📝 Commits (1) - [`001314a`](https://github.com/community-scripts/ProxmoxVE/commit/001314a3dcf36e119e05a8e5467a2180b4b6cd8a) Introducing core.func ### 📊 Changes **1 file changed** (+544 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `misc/core.func` (+544 -0) </details> ### 📄 Description ## ⚙️ core.func – Centralized Utility and Trap Framework for Community Scripts This PR introduces a robust and fully modular `core.func` library for all community-scripts-based projects. It provides a reusable, fault-tolerant core for Bash scripts in Proxmox environments (and beyond). --- ### 🔧 Features #### 🔒 Robust Error and Signal Handling - Catches: - `ERR` (command failure) - `INT` (Ctrl+C) - `TERM` (SIGTERM) - `EXIT` (cleanup) - Unified error output with contextual messages - Spinner-safe cleanup on failure/abort - Predefined fallback for undefined `wait_for` calls #### 💬 Unified Output System - `msg_info`, `msg_ok`, `msg_error`, `msg_warn`, `msg_custom` - Built-in icons and coloring via ANSI escape sequences - `msg_custom` allows flexible messaging with icons and color - Spinner support with `start_spinner` / `stop_spinner` using UTF-8 frames #### 🌐 Curl with Retry and Exit Code Handling - `curl_handler` includes: - Retry mechanism (default: 3 attempts) - Automatic detection of `-o` flags - Inline feedback with exit code awareness - `__curl_err_handler` maps specific `curl` exit codes to readable messages #### 🖥 Terminal Formatting - `color`, `formatting`, `icons` defined once and reused - Visual structure: Tabs, Bolds, Resets - Standardized icon set for Proxmox use cases (e.g., CPU, RAM, Container ID) #### 📦 System Defaults & Configuration - `default_vars` for retry logic and default modes - `set_std_mode` for toggling verbosity (`VERBOSE=yes` / `no`) - Safe silent execution via `silent()` #### 🧪 Utility Functions - `run_container_safe`: Executes commands inside a container with signal handling - `check_or_create_swap`: Interactive swap creation if not present - `get_header` / `header_info`: Dynamic header loading for script branding - `msg_progress`: Draws a progress bar based on percentage completion --- ### 📚 Benefits - ✅ Fully POSIX-compatible (Debian, Ubuntu, Alpine) - 🧩 Easily included in other `*.func` or `*-install.sh` scripts - 💥 Fail-fast and recover-safe - 🎨 Consistent terminal UX across all scripts --- ### 🧱 Intended Usage This file is meant to be sourced early via: ```bash source <(curl -fsSL https://git.community-scripts.org/community-scripts/ProxmoxVED/raw/branch/main/misc/core.func) load_functions ``` ## ✅ Prerequisites (**X** in brackets) - [x] **Self-review completed** – Code follows project standards. - [x] **Tested thoroughly** – Changes work as expected. - [x] **No security risks** – No hardcoded secrets, unnecessary privilege escalations, or permission issues. --- ## 🛠️ Type of Change (**X** in brackets) - [x] ✨ **New feature** – Adds new, non-breaking functionality. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
saavagebueno added the pull-request label 2025-11-20 07:05:33 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: SVI/ProxmoxVE#5350