feat(config): implement configuration system and contributing guide (#6, #4)

This commit is contained in:
2026-01-27 00:10:27 -08:00
parent 631046df34
commit 092299ff33
6 changed files with 767 additions and 2 deletions

69
scripts/load-config.sh Executable file
View File

@@ -0,0 +1,69 @@
#!/bin/bash
# Load configuration from JSON file and export as environment variables
# Environment variables take precedence over JSON values
set -e
# Default config file location
CONFIG_FILE="${CONFIG_FILE:-config.json}"
# Color output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to safely extract JSON values
get_json_value() {
local file="$1"
local key="$2"
local default="${3:-}"
if [ -f "$file" ]; then
value=$(jq -r "$key" "$file" 2>/dev/null || echo "null")
if [ "$value" = "null" ] || [ -z "$value" ]; then
echo "$default"
else
echo "$value"
fi
else
echo "$default"
fi
}
# Check if config file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo -e "${YELLOW}Warning: $CONFIG_FILE not found${NC}" >&2
echo -e "${YELLOW}Using environment variables only${NC}" >&2
echo -e "${YELLOW}Copy config.example.json to config.json and fill in your credentials${NC}" >&2
echo "" >&2
fi
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo -e "${RED}Error: jq is not installed${NC}" >&2
echo -e "${RED}Install it with: sudo apt-get install jq${NC}" >&2
exit 1
fi
# Load configuration values, preferring environment variables
export NEXTCLOUD_URL="${NEXTCLOUD_URL:-$(get_json_value "$CONFIG_FILE" '.nextcloud.url' '')}"
export NEXTCLOUD_USER="${NEXTCLOUD_USER:-$(get_json_value "$CONFIG_FILE" '.nextcloud.username' '')}"
export NEXTCLOUD_PASSWORD="${NEXTCLOUD_PASSWORD:-$(get_json_value "$CONFIG_FILE" '.nextcloud.password' '')}"
# Load settings
export MAX_RECENT_FOLDERS="${MAX_RECENT_FOLDERS:-$(get_json_value "$CONFIG_FILE" '.settings.maxRecentFolders' '5')}"
export UPLOAD_CHUNK_SIZE="${UPLOAD_CHUNK_SIZE:-$(get_json_value "$CONFIG_FILE" '.settings.uploadChunkSize' '10485760')}"
# Show what was loaded (without showing password)
if [ -n "$NEXTCLOUD_URL" ]; then
echo -e "${GREEN}${NC} NEXTCLOUD_URL: $NEXTCLOUD_URL"
fi
if [ -n "$NEXTCLOUD_USER" ]; then
echo -e "${GREEN}${NC} NEXTCLOUD_USER: $NEXTCLOUD_USER"
fi
if [ -n "$NEXTCLOUD_PASSWORD" ]; then
echo -e "${GREEN}${NC} NEXTCLOUD_PASSWORD: [set]"
fi
echo -e "${GREEN}${NC} MAX_RECENT_FOLDERS: $MAX_RECENT_FOLDERS"
echo -e "${GREEN}${NC} UPLOAD_CHUNK_SIZE: $UPLOAD_CHUNK_SIZE"

73
scripts/validate-config.sh Executable file
View File

@@ -0,0 +1,73 @@
#!/bin/bash
# Validate that all required configuration is present
set -e
# Color output
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Load configuration first
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/load-config.sh" > /dev/null 2>&1 || true
# Validation flags
VALID=true
# Validate required fields
if [ -z "$NEXTCLOUD_URL" ]; then
echo -e "${RED}✗ Error: NEXTCLOUD_URL is not set${NC}" >&2
VALID=false
fi
if [ -z "$NEXTCLOUD_USER" ]; then
echo -e "${RED}✗ Error: NEXTCLOUD_USER is not set${NC}" >&2
VALID=false
fi
if [ -z "$NEXTCLOUD_PASSWORD" ]; then
echo -e "${RED}✗ Error: NEXTCLOUD_PASSWORD is not set${NC}" >&2
VALID=false
fi
# Validate URL format
if [ -n "$NEXTCLOUD_URL" ]; then
if [[ ! "$NEXTCLOUD_URL" =~ ^https?:// ]]; then
echo -e "${RED}✗ Error: NEXTCLOUD_URL must start with http:// or https://${NC}" >&2
VALID=false
fi
# Warn if using HTTP instead of HTTPS
if [[ "$NEXTCLOUD_URL" =~ ^http:// ]]; then
echo -e "${RED}⚠ Warning: Using HTTP instead of HTTPS is insecure!${NC}" >&2
fi
fi
# Validate numeric settings
if ! [[ "$MAX_RECENT_FOLDERS" =~ ^[0-9]+$ ]]; then
echo -e "${RED}✗ Error: MAX_RECENT_FOLDERS must be a number${NC}" >&2
VALID=false
fi
if ! [[ "$UPLOAD_CHUNK_SIZE" =~ ^[0-9]+$ ]]; then
echo -e "${RED}✗ Error: UPLOAD_CHUNK_SIZE must be a number${NC}" >&2
VALID=false
fi
# Exit with appropriate code
if [ "$VALID" = true ]; then
echo -e "${GREEN}✓ Configuration is valid${NC}"
exit 0
else
echo ""
echo "Please set the missing configuration values in:"
echo " 1. config.json (copy from config.example.json), or"
echo " 2. Environment variables"
echo ""
echo "Required environment variables:"
echo " - NEXTCLOUD_URL"
echo " - NEXTCLOUD_USER"
echo " - NEXTCLOUD_PASSWORD"
exit 1
fi