diff --git a/src/app.cpp b/src/app.cpp index 5b7a26b..7a6f740 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -604,7 +604,7 @@ void App::handleSavePost(const httplib::Request& req, httplib::Response& res) } E value = data->getValueWithDefault( - "pause_update_time", false); + "pause-update-time", false); if(!value.has_value()) { res.status = 500; diff --git a/src/main.cpp b/src/main.cpp index e09fb07..71453d4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,7 +24,11 @@ int main(int argc, char** argv) cmd_options.add_options() ("c,config", "Config file", cxxopts::value()->default_value("/etc/planck-blog.yaml")) - ("legacy-migration", "Migrate the legacy posts from a directory", + ("legacy-migration", "Migrate the legacy posts from a directory and exit", + cxxopts::value()) + ("set", "Set a runtime setting and exit. Example:" + " --set pause-update-time=true. The only setting available right now" + " is pause-update-time.", cxxopts::value()) ("h,help", "Print this message."); auto opts = cmd_options.parse(argc, argv); @@ -60,6 +64,44 @@ int main(int argc, char** argv) } } + if(opts.count("set") == 1) + { + std::string _ = opts["set"].as(); + std::string_view keyvalue(_); + auto index = keyvalue.find('='); + std::string_view key = strip(keyvalue.substr(0, index)); + if(key.empty()) + { + spdlog::error("Invalid key"); + return 1; + } + nlohmann::json value = parseJSON(strip(keyvalue.substr(index+1))); + if(value.is_discarded()) + { + spdlog::error("Invalid value"); + return 1; + } + auto data_source = DataSourceSqlite::fromFile( + (std::filesystem::path(conf->data_dir) / "data.db").string()); + if(!data_source.has_value()) + { + spdlog::error("Failed to create data source: {}", + errorMsg(data_source.error())); + return 2; + } + auto ok_maybe = (*data_source)->setValue(std::string(key), + std::move(value)); + if(ok_maybe) + { + return 0; + } + else + { + spdlog::error(errorMsg(ok_maybe.error())); + return 1; + } + } + auto url_prefix = URL::fromStr(conf->base_url); if(!url_prefix.has_value()) {