Skip to content

Commit

Permalink
Add the command line option to set runtime settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
MetroWind committed Sep 30, 2024
1 parent e09f0ef commit c93d29a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ void App::handleSavePost(const httplib::Request& req, httplib::Response& res)
}

E<nlohmann::json> value = data->getValueWithDefault(
"pause_update_time", false);
"pause-update-time", false);
if(!value.has_value())
{
res.status = 500;
Expand Down
44 changes: 43 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ int main(int argc, char** argv)
cmd_options.add_options()
("c,config", "Config file",
cxxopts::value<std::string>()->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<std::string>())
("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<std::string>())
("h,help", "Print this message.");
auto opts = cmd_options.parse(argc, argv);
Expand Down Expand Up @@ -60,6 +64,44 @@ int main(int argc, char** argv)
}
}

if(opts.count("set") == 1)
{
std::string _ = opts["set"].as<std::string>();
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())
{
Expand Down

0 comments on commit c93d29a

Please sign in to comment.