From 860bb6ec086ebd6594635c84abd757c2ad4ebf0e Mon Sep 17 00:00:00 2001 From: Sven Ullmann Date: Tue, 4 Jul 2023 22:51:52 +0200 Subject: [PATCH] refactoring --- src/commands/configure-plugin | 38 ++++++++++++++++++++++++++++ src/commands/create-project | 14 +++++----- src/commands/list-projects | 12 ++++----- src/commands/remove-project | 2 +- src/includes/config.php | 5 +++- src/includes/configure-json-file | 31 +++++++++++++++++++++++ src/includes/configure-json-file.php | 25 ++++++++++++++++++ src/includes/main_functions | 6 +++++ 8 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 src/commands/configure-plugin create mode 100644 src/includes/configure-json-file create mode 100644 src/includes/configure-json-file.php diff --git a/src/commands/configure-plugin b/src/commands/configure-plugin new file mode 100644 index 0000000..9bd2b65 --- /dev/null +++ b/src/commands/configure-plugin @@ -0,0 +1,38 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +function usage { + echo + echoMainTitle "Configures a plugin" + echo + echoSubTitle "Usage:" + echo + echo "configure-plugin [shortname] [plugin]" + echo + echo "--help Prints this message" + echo " " +} + +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/bash_header" + +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") +project_dir="$project_manager_dir/data/$customer/$project" +config_path="$project_dir/etc/$plugin/config.json" + +if [ ! -f "$config_path" ] +then + echo + echoError "Could not load configuration file: $config_path" + echo + exit +fi + +echo +echoMainTitle "Configure Plugin: $plugin" +echo + +configureJsonFile "$config_path" \ No newline at end of file diff --git a/src/commands/create-project b/src/commands/create-project index b768c82..20e01bb 100644 --- a/src/commands/create-project +++ b/src/commands/create-project @@ -23,10 +23,11 @@ function usage { source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/bash_header" -customer="$(getArgument "$1" "$(usageError "Invalid customer")" true)" -project="$(getArgument "$2" "$(usageError "Invalid project")" true)" -shortname="$(getArgument "$3" "$(usageError "Invalid shortname")" true)" +customer="$(getArgument "$1" "Invalid customer" true)" +project="$(getArgument "$2" "Invalid project" true)" +shortname="$(getArgument "$3" "Invalid shortname" true)" has_shortname="$(hasConfig false "project_manager.projects.$shortname")" +workspaces_dir="$(getConfig false "project_manager.workspaces_dir")" if [ "$has_shortname" == true ] then @@ -47,7 +48,7 @@ echo echoMainTitle "Create project" project_dir="$project_manager_dir/data/$customer/$project" -workspace_dir="$project_manager_workspaces_dir/$customer/$project" +workspace_dir="$workspaces_dir/$customer/$project" echo echoSubTitle "Please verify data" @@ -60,8 +61,9 @@ confirm cryptkey="$(randkey)" -setConfig false "project_manager.projects.$shortname.customer" "$customer" -setConfig false "project_manager.projects.$shortname.project" "$project" +escapedShortname=${shortname//./\\.} +setConfig false "project_manager.projects.$escapedShortname.customer" "$customer" +setConfig false "project_manager.projects.$escapedShortname.project" "$project" setConfig "$shortname" "project.cryptkey" "$cryptkey" runPostscripts "create-project" "$shortname" diff --git a/src/commands/list-projects b/src/commands/list-projects index b758957..1f05bd6 100644 --- a/src/commands/list-projects +++ b/src/commands/list-projects @@ -2,8 +2,6 @@ ### DO NOT EDIT THIS FILE -source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/bash_header" - function usage { echo echoMainTitle "List all projects" @@ -16,7 +14,9 @@ function usage { echo } -projects="$(getConfig "project_manager_dir.projects")" +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/bash_header" + +projects="$(getConfig false "project_manager_dir.projects")" function listProjects { echo >&2 @@ -24,8 +24,8 @@ function listProjects { echo >&2 for shortname in "${projects[@]}" do - customer="$(getConfig "project_manager_dir.projects.$shortname.customer")" - project="$(getConfig "project_manager_dir.projects.$shortname.project")" + customer="$(getConfig false "project_manager_dir.projects.$shortname.customer")" + project="$(getConfig false "project_manager_dir.projects.$shortname.project")" echo "- Shortname: $shortname Customer: $customer Project: $project" >&2 @@ -45,4 +45,4 @@ else listProjects fi -runPostscripts "list-projects" \ No newline at end of file +runPostscripts false "list-projects" \ No newline at end of file diff --git a/src/commands/remove-project b/src/commands/remove-project index d2ace27..84f41a3 100644 --- a/src/commands/remove-project +++ b/src/commands/remove-project @@ -18,7 +18,7 @@ function usage { source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/bash_header" -shortname="$(getArgument "$1" "$(usageError "shortname required")" true)" +shortname="$(getArgument "$1" "shortname required" true)" if [ "$(hasConfig false "project_manager.projects.$shortname")" == false ] then diff --git a/src/includes/config.php b/src/includes/config.php index 59196d6..1d69ebb 100755 --- a/src/includes/config.php +++ b/src/includes/config.php @@ -57,7 +57,7 @@ function loadProjectConfig() $dh = opendir($plugin_dir); if ($dh) { while (false !== ($plugin = readdir($dh))) { - if ('.' === $plugin || '..' === $plugin || !is_dir($plugin_dir / $plugin)) { + if ('.' === $plugin || '..' === $plugin || !is_dir("$plugin_dir/$plugin")) { continue; } $config_file = "$plugin_dir/$plugin/config.json"; @@ -83,7 +83,10 @@ function parsePath() { global $configPath; $parsedPath = ''; + $configPath = str_replace('\\.', '', $configPath); foreach (explode('.', $configPath) as $part) { + $part = str_replace('', '.', $part); + $part = str_replace("'", "\\'", $part); $parsedPath .= "['$part']"; } return $parsedPath; diff --git a/src/includes/configure-json-file b/src/includes/configure-json-file new file mode 100644 index 0000000..f1deaac --- /dev/null +++ b/src/includes/configure-json-file @@ -0,0 +1,31 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +function usage { + echo + echoMainTitle "Sumedia Project Manager Configure Configuration File" + echo + echoSubTitle "Usage:" + echo + echo "configure-json-file filePath" + echo + echo "--help Prints this message" + echo " " + exit +} + +source "$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)/bash_header" + +filePath="$(getArgument "$1" "Parameter filePath not given" true)" +if [ ! -f "$filePath" ] +then + echo + echoError "Could not find configuration file: $filePath" + echo + exit; +fi + +PHP=`which php` +script="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)/configure-json-file.php" +$PHP "$script" "$@" \ No newline at end of file diff --git a/src/includes/configure-json-file.php b/src/includes/configure-json-file.php new file mode 100644 index 0000000..7827c83 --- /dev/null +++ b/src/includes/configure-json-file.php @@ -0,0 +1,25 @@ + $value) { + if (is_array($value) && !isset($value[0])) { + readNext($value, $current[$key]); + } else { + $prompt = "$key [default:$value]"; + $current[$key] = readline($prompt); + } + } +} + +foreach ($configuration as $key => $config) { + readNext($config, $configuration[$key]); +} + +file_put_contents($jsonFile, json_encode($configuration, JSON_PRETTY_PRINT && JSON_ERROR_NONE && JSON_UNESCAPED_SLASHES)); \ No newline at end of file diff --git a/src/includes/main_functions b/src/includes/main_functions index 98b8157..4a33da7 100644 --- a/src/includes/main_functions +++ b/src/includes/main_functions @@ -339,4 +339,10 @@ then local configScript="$project_manager_dir/src/includes/json" echo "$(source "$configScript" has "$container" "$configPath")" } + + function configureJsonFile { + local jsonPath="$(getArgument "$1" "json file path required" true)" + local configScript="$project_manager_dir/src/includes/configure-json-file" + source "$configScript" "$jsonPath" + } fi \ No newline at end of file