From e9bd0acd5fdf6d9b6fc934b19ffea9c86104a7d3 Mon Sep 17 00:00:00 2001 From: Lukas Bestle Date: Sat, 30 May 2020 17:10:25 +0200 Subject: [PATCH] Support for $XDG_CONFIG_HOME --- README.md | 4 ++-- bin/project_add | 14 +++++++++++--- bin/project_deploy | 5 ++++- bin/project_list | 16 ++++++++++++---- bin/project_origin | 5 ++++- bin/project_remove | 14 +++++++++++--- webhook.gitea.php | 8 ++++++-- webhook.github.php | 8 ++++++-- webhook.gitlab.php | 8 ++++++-- 9 files changed, 62 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index f553fd4..58e50e2 100644 --- a/README.md +++ b/README.md @@ -132,12 +132,12 @@ The tool `project_deploy` takes the full path to the project and the Git revisio You can find example PHP implementations for GitHub, GitLab and Gitea webhooks in `webhook.github.php`, `webhook.gitlab.php` and `webhook.gitea.php`, but you can also create your own if you use a different repository service: 1. Write a script that receives webhooks from the repository and gets the repository URL, commit SHA-1 and branch name of the event from the transmitted data. -2. Read the file `~/.projects`, which contains the paths to all known projects and sites, and iterate through it. +2. Read the projects file at `~/.config/projectr/projects` if it exists (otherwise `~/.projects`), which contains the paths to all known projects and sites, and iterate through it. 3. Open the project's `.origin` and `.branch` files. If they match the web-hook, run `project_deploy ` and you are done. ### Configuration -If you want to customize specific settings, you can create a Bash file at `~/.project.cnf` to override the default values. These are the possible settings and also the format of the file: +If you want to customize specific settings, you can create a Bash file at `~/.project.cnf` or `$XDG_CONFIG_HOME/projectr/config.sh` (which defaults to `~/.config/projectr/config.sh`) to override the default values. These are the possible settings and also the format of the file: ```bash # Default branch to set if no one is given to `project_origin`/`site_origin` diff --git a/bin/project_add b/bin/project_add index 73474dc..b268a1a 100755 --- a/bin/project_add +++ b/bin/project_add @@ -35,9 +35,17 @@ if ! (mkdir "$project" && mkdir "$project/data" && touch "$project/.project"); t fi echo -e " => \033[32mSuccess.\033[0m" -# Add to ~/.projects -echo -e "\033[1mAdding project to \033[34m~/.projects\033[0;1m...\033[0m" -if ! echo "$(readlink -f "$project")" >> "$HOME/.projects"; then +# Determine the path to the projects file +configPath=${XDG_CONFIG_HOME:-$HOME/.config} +if [[ -d "$configPath/projectr" ]]; then + projectsFile="$configPath/projectr/projects" +else + projectsFile="$HOME/.projects" +fi + +# Add to the projects file +echo -e "\033[1mAdding project to \033[34m$projectsFile\033[0;1m...\033[0m" +if ! echo "$(readlink -f "$project")" >> "$projectsFile"; then echo -e " => \033[31mSomething went wrong.\033[0m" >&2 exit 1 fi diff --git a/bin/project_deploy b/bin/project_deploy index d795743..7d3aa0b 100755 --- a/bin/project_deploy +++ b/bin/project_deploy @@ -66,7 +66,10 @@ revision="${2:-latest}" # Load configuration CONFIG_PRESERVE_VERSIONS=5 CONFIG_HASH_LENGTH=40 -if [[ -f "$HOME/.project.cnf" ]]; then +configPath=${XDG_CONFIG_HOME:-$HOME/.config} +if [[ -f "$configPath/projectr/config.sh" ]]; then + source "$configPath/projectr/config.sh" +elif [[ -f "$HOME/.project.cnf" ]]; then source "$HOME/.project.cnf" fi if [[ $CONFIG_PRESERVE_VERSIONS == 1 ]]; then diff --git a/bin/project_list b/bin/project_list index 19d34b9..201f779 100755 --- a/bin/project_list +++ b/bin/project_list @@ -8,11 +8,19 @@ # Usage: project_list ######################################################################## -# Check if the ~/.project file exists -if [[ ! -f "$HOME/.projects" ]]; then - echo -e "\033[31mThere is no \033[34m~/.projects\033[31m file yet.\033[0m" >&2 +# Determine the path to the projects file +configPath=${XDG_CONFIG_HOME:-$HOME/.config} +if [[ -d "$configPath/projectr" ]]; then + projectsFile="$configPath/projectr/projects" +else + projectsFile="$HOME/.projects" +fi + +# Check if the projects file exists +if [[ ! -f "$projectsFile" ]]; then + echo -e "\033[31mThere is no \033[34m$projectsFile\033[31m file yet.\033[0m" >&2 exit 1 fi -cat "$HOME/.projects" +cat "$projectsFile" exit $? diff --git a/bin/project_origin b/bin/project_origin index 51ce622..6b752fb 100755 --- a/bin/project_origin +++ b/bin/project_origin @@ -20,7 +20,10 @@ origin="$2" # Load configuration CONFIG_DEFAULT_BRANCH="master" -if [[ -f "$HOME/.project.cnf" ]]; then +configPath=${XDG_CONFIG_HOME:-$HOME/.config} +if [[ -f "$configPath/projectr/config.sh" ]]; then + source "$configPath/projectr/config.sh" +elif [[ -f "$HOME/.project.cnf" ]]; then source "$HOME/.project.cnf" fi diff --git a/bin/project_remove b/bin/project_remove index b99d064..4e75dbe 100755 --- a/bin/project_remove +++ b/bin/project_remove @@ -32,10 +32,18 @@ if ! rm -Rf $project; then fi echo -e " => \033[32mSuccess.\033[0m" -# Remove from ~/.projects -echo -e "\033[1mRemoving project from \033[34m~/.projects\033[0;1m...\033[0m" +# Determine the path to the projects file +configPath=${XDG_CONFIG_HOME:-$HOME/.config} +if [[ -d "$configPath/projectr" ]]; then + projectsFile="$configPath/projectr/projects" +else + projectsFile="$HOME/.projects" +fi + +# Remove from the projects file +echo -e "\033[1mRemoving project from \033[34m$projectsFile\033[0;1m...\033[0m" realpath="$(readlink -f "$project")" -if ! sed -i "/${realpath//\//\/}/d" "$HOME/.projects"; then # Replace / with \/ because of the sed separators +if ! sed -i "/${realpath//\//\/}/d" "$projectsFile"; then # Replace / with \/ because of the sed separators echo -e " => \033[31mSomething went wrong.\033[0m" >&2 exit 1 fi diff --git a/webhook.gitea.php b/webhook.gitea.php index 1dcdbb0..a246014 100644 --- a/webhook.gitea.php +++ b/webhook.gitea.php @@ -136,8 +136,12 @@ $branch = $matches[1]; // Debug echo "Received commit hash \"$commit\" for repository URL \"$url\" (branch \"$branch\").\n"; -// Open ~/.projects and iterate through every project -$listPointer = fopen($_SERVER['HOME'] . '/.projects', 'r'); +// Determine the path to the projects file +$xdgProjectsFile = ($_ENV['XDG_CONFIG_HOME'] ?? ($_SERVER['HOME'] . '/.config')) . '/projectr/projects'; +$projectsFile = is_file($xdgProjectsFile) === true ? $xdgProjectsFile : $_SERVER['HOME'] . '/.projects'; + +// Open the projects file and iterate through every project +$listPointer = fopen($projectsFile, 'r'); $exitCode = 0; while (($project = fgets($listPointer)) !== false) { // Trim whitespace diff --git a/webhook.github.php b/webhook.github.php index cc6024e..31e71a7 100644 --- a/webhook.github.php +++ b/webhook.github.php @@ -139,8 +139,12 @@ $branch = $matches[1]; // Debug echo "Received commit hash \"$commit\" for repository URL \"$url\" (branch \"$branch\").\n"; -// Open ~/.projects and iterate through every project -$listPointer = fopen($_SERVER['HOME'] . '/.projects', 'r'); +// Determine the path to the projects file +$xdgProjectsFile = ($_ENV['XDG_CONFIG_HOME'] ?? ($_SERVER['HOME'] . '/.config')) . '/projectr/projects'; +$projectsFile = is_file($xdgProjectsFile) === true ? $xdgProjectsFile : $_SERVER['HOME'] . '/.projects'; + +// Open the projects file and iterate through every project +$listPointer = fopen($projectsFile, 'r'); $exitCode = 0; while (($project = fgets($listPointer)) !== false) { // Trim whitespace diff --git a/webhook.gitlab.php b/webhook.gitlab.php index 6acb86e..3492870 100644 --- a/webhook.gitlab.php +++ b/webhook.gitlab.php @@ -108,8 +108,12 @@ $branch = $matches[1]; // Debug echo "Received commit hash \"$commit\" for repository URL \"$url\" (branch \"$branch\").\n"; -// Open ~/.projects and iterate through every project -$listPointer = fopen($_SERVER['HOME'] . '/.projects', 'r'); +// Determine the path to the projects file +$xdgProjectsFile = ($_ENV['XDG_CONFIG_HOME'] ?? ($_SERVER['HOME'] . '/.config')) . '/projectr/projects'; +$projectsFile = is_file($xdgProjectsFile) === true ? $xdgProjectsFile : $_SERVER['HOME'] . '/.projects'; + +// Open the projects file and iterate through every project +$listPointer = fopen($projectsFile, 'r'); $exitCode = 0; while (($project = fgets($listPointer)) !== false) { // Trim whitespace