From 82c30f1daad87ea20a5180a51efef7b59546d9b5 Mon Sep 17 00:00:00 2001 From: j Date: Sun, 28 Dec 2025 12:45:51 +1300 Subject: [PATCH] create-config option --- source/src/commands/edit.cpp | 34 +++++++++++++++++++++++++++++----- source/src/config.cpp | 31 ++++++++++++++++--------------- source/src/config.hpp | 3 ++- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/source/src/commands/edit.cpp b/source/src/commands/edit.cpp index 10345aa..1d806d2 100644 --- a/source/src/commands/edit.cpp +++ b/source/src/commands/edit.cpp @@ -18,7 +18,7 @@ namespace dropshell { int edit_handler(const CommandContext& ctx); -static std::vector edit_name_list={"edit"}; +static std::vector edit_name_list={"edit","create-config"}; // Static registration struct EditCommandRegister { @@ -87,13 +87,27 @@ bool edit_file(const std::string &file_path, bool has_bb64) } } +int create_config() +{ + if (!gConfig().is_config_set()) + { + bool ok = gConfig().save_config(); // save defaults. + info << "Default dropshell.json created." << std::endl; + return (ok ? 0 : 1); + } + else + info << "Existing dropshell.json unchanged." << std::endl; + + return 0; +} + + // ------------------------------------------------------------------------------------------------ // edit config // ------------------------------------------------------------------------------------------------ int edit_config() { - if (!gConfig().is_config_set()) - gConfig().save_config(false); // save defaults. + create_config(); std::string config_file = localfile::dropshell_json(); if (!edit_file(config_file, false) || !std::filesystem::exists(config_file)) @@ -105,8 +119,9 @@ int edit_config() // Don't save_config after loading - it would rewrite the file the user just edited! // The config is already saved by the editor, we just validated it by loading it. + gConfig().create_aux_directories(); - std::cout << "Successfully edited config file at " << config_file << std::endl; + info << "Successfully edited config file at " << config_file << std::endl; return 0; } @@ -232,7 +247,16 @@ int edit_service_config(const std::string &server, const std::string &service) int edit_handler(const CommandContext& ctx) { // edit dropshell config if (ctx.args.size() < 1) - return edit_config(); + { + if (ctx.command=="create-config") + { + int rval = create_config(); + gConfig().create_aux_directories(); + return rval; + } + else + return edit_config(); + } // edit server config if (ctx.args.size() < 2) { diff --git a/source/src/config.cpp b/source/src/config.cpp index bbd6084..0b4637a 100644 --- a/source/src/config.cpp +++ b/source/src/config.cpp @@ -140,7 +140,7 @@ void _append(std::vector & a, const std::vector & b) { a.insert(std::end(a), std::begin(b), std::end(b)); } -bool config::save_config(bool create_aux_directories) +bool config::save_config() { std::string config_path = localfile::dropshell_json(); if (config_path.empty()) @@ -177,18 +177,6 @@ bool config::save_config(bool create_aux_directories) config_file << mConfig.dump(4); config_file.close(); - if (create_aux_directories) { - std::vector paths; - _append(paths, get_local_template_paths()); - _append(paths, get_local_server_definition_paths()); - for (auto & p : paths) - if (!std::filesystem::exists(p)) - { - std::cout << "Creating directory: " << p << std::endl; - std::filesystem::create_directories(p); - } - } - debug << "Config paths: " << std::endl; for (auto [key,value] : mConfig.items()) { debug << " " << key << ": " << value << std::endl; @@ -197,6 +185,19 @@ bool config::save_config(bool create_aux_directories) return true; } +bool config::create_aux_directories() +{ + std::vector paths; + _append(paths, get_local_template_paths()); + _append(paths, get_local_server_definition_paths()); + for (auto & p : paths) + if (!std::filesystem::exists(p)) + { + info << "Creating directory: " << p << std::endl; + std::filesystem::create_directories(p); + } +} + bool config::is_config_set() const { return mIsConfigSet; @@ -343,13 +344,13 @@ void config::set_server_disabled(const std::string &server_name, bool disabled) // Add to disabled list if not already there if (it == disabled_servers.end()) { disabled_servers.push_back(server_name); - save_config(false); // Save immediately + save_config(); // Save immediately } } else { // Remove from disabled list if present if (it != disabled_servers.end()) { disabled_servers.erase(it); - save_config(false); // Save immediately + save_config(); // Save immediately } } } diff --git a/source/src/config.hpp b/source/src/config.hpp index 5077bb8..022b276 100644 --- a/source/src/config.hpp +++ b/source/src/config.hpp @@ -27,7 +27,8 @@ class config { ~config(); bool load_config(); - bool save_config(bool create_aux_directories); + bool save_config(); + bool create_aux_directories(); bool is_config_set() const; static bool is_agent_installed();