From f41c91bb629d063b30cfd39987d4ab1caa80a016 Mon Sep 17 00:00:00 2001 From: Sven Ullmann Date: Tue, 4 Jul 2023 23:48:44 +0200 Subject: [PATCH] refactoring --- src/commands/configure-plugin | 5 +++-- src/commands/install-plugin | 5 +++-- src/commands/list-projects | 5 +++-- src/commands/remove-project | 5 +++-- src/includes/configure-json-file.php | 21 +++++++++++++++------ 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/commands/configure-plugin b/src/commands/configure-plugin index 9bd2b65..9e9a49c 100644 --- a/src/commands/configure-plugin +++ b/src/commands/configure-plugin @@ -18,8 +18,9 @@ source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/b shortname=$(getArgument "$1" "shortname required" true) plugin=$(getArgument "$2" "plugin required" true) -customer=$(getConfig false "project_manager.projects.$shortname.customer") -project=$(getConfig false "project_manager.projects.$shortname.project") +escapedShortname=${shortname//./\\.} +customer=$(getConfig false "project_manager.projects.$escapedShortname.customer") +project=$(getConfig false "project_manager.projects.$escapedShortname.project") project_dir="$project_manager_dir/data/$customer/$project" config_path="$project_dir/etc/$plugin/config.json" diff --git a/src/commands/install-plugin b/src/commands/install-plugin index 712ad88..0be1813 100644 --- a/src/commands/install-plugin +++ b/src/commands/install-plugin @@ -18,8 +18,9 @@ source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/b shortname=$(getArgument "$1" "shortname required" true) plugin=$(getArgument "$2" "plugin required" true) -customer=$(getConfig false "project_manager.projects.$shortname.customer") -project=$(getConfig false "project_manager.projects.$shortname.project") +escapedShortname=${shortname//./\\.} +customer=$(getConfig false "project_manager.projects.$escapedShortname.customer") +project=$(getConfig false "project_manager.projects.$escapedShortname.project") project_dir="$project_manager_dir/data/$customer/$project" plugin_dir="$project_manager_dir/plugins/$plugin" source="$project_manager_dir/plugins/$plugin/etc/config.json" diff --git a/src/commands/list-projects b/src/commands/list-projects index 1f05bd6..ea56fce 100644 --- a/src/commands/list-projects +++ b/src/commands/list-projects @@ -24,8 +24,9 @@ function listProjects { echo >&2 for shortname in "${projects[@]}" do - customer="$(getConfig false "project_manager_dir.projects.$shortname.customer")" - project="$(getConfig false "project_manager_dir.projects.$shortname.project")" + escapedShortname=${shortname//./\\.} + customer="$(getConfig false "project_manager_dir.projects.$escapedShortname.customer")" + project="$(getConfig false "project_manager_dir.projects.$escapedShortname.project")" echo "- Shortname: $shortname Customer: $customer Project: $project" >&2 diff --git a/src/commands/remove-project b/src/commands/remove-project index 84f41a3..a4b004e 100644 --- a/src/commands/remove-project +++ b/src/commands/remove-project @@ -28,8 +28,9 @@ then exit fi -customer="$(getConfig false "project_manager.projects.$shortname.customer")" -project="$(getConfig false "project_manager.projects.$shortname.project")" +escapedShortname=${shortname//./\\.} +customer="$(getConfig false "project_manager.projects.$escapedShortname.customer")" +project="$(getConfig false "project_manager.projects.$escapedShortname.project")" workspaces_dir="$(getConfig false "project_manager.workspaces_dir")" workspace_dir="$workspaces_dir/$customer/$project" project_dir="$project_manager_dir/data/$customer/$project" diff --git a/src/includes/configure-json-file.php b/src/includes/configure-json-file.php index 7827c83..e6b846f 100644 --- a/src/includes/configure-json-file.php +++ b/src/includes/configure-json-file.php @@ -7,19 +7,28 @@ $configuration = []; $jsonFile = $_SERVER['argv'][1]; $configuration = json_decode(file_get_contents($jsonFile), true); -function readNext($config, &$current) { +function readNext($config, &$current, $keyPath = '') { foreach ($config as $key => $value) { if (is_array($value) && !isset($value[0])) { - readNext($value, $current[$key]); + readNext($value, $current[$key], "$keyPath.$key"); } else { - $prompt = "$key [default:$value]"; - $current[$key] = readline($prompt); + $default = $value; + if (is_bool($value)) { + $default = true === $value ? 'true' : 'false'; + } + if (null === $value) { + $default = null; + } + $prompt = "$keyPath.$key: " . ($default !== null ? "[$default]": ""); + $ret = readline($prompt); + $current[$key] = "" === $ret ? ($value === null ? null : $default) : $ret; } } } foreach ($configuration as $key => $config) { - readNext($config, $configuration[$key]); + readNext($config, $configuration[$key], $key); } -file_put_contents($jsonFile, json_encode($configuration, JSON_PRETTY_PRINT && JSON_ERROR_NONE && JSON_UNESCAPED_SLASHES)); \ No newline at end of file +file_put_contents($jsonFile, json_encode($configuration, + JSON_PRETTY_PRINT | JSON_ERROR_NONE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT)); \ No newline at end of file