Use fmt::format for string formatting where it improves clarity.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
tastytea 2020-10-28 10:52:16 +01:00
parent 59d8f33cad
commit c904108f4d
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 46 additions and 38 deletions

View File

@ -19,6 +19,8 @@
#include "json.hpp"
#include "types.hpp"
#include <fmt/format.h>
#include <algorithm>
#include <cstdint>
#include <cstdlib>
@ -32,6 +34,7 @@
namespace FediBlock::files
{
using fmt::format;
using std::getenv;
using std::ifstream;
using std::ofstream;
@ -70,7 +73,7 @@ void remove_tmpdir()
fs::path get_xdg_dir(const string &identifier)
{
const string xdg_variable{("XDG_" + identifier) += "_HOME"};
const string xdg_variable{format("XDG_{:s}_HOME", identifier)};
char *env{getenv(xdg_variable.c_str())};
if (env != nullptr)
{
@ -133,11 +136,12 @@ fs::path get_cachedir()
string get_access_token()
{
ifstream file(get_datadir() / "gitea_access_token");
const fs::path access_token_path{get_datadir() / "gitea_access_token"};
ifstream file(access_token_path);
if (!file.good())
{
throw runtime_error{"Could not read access token: "
+ (get_datadir() / "gitea_access_token").string()};
throw runtime_error{format("Could not read access token: {:s}",
access_token_path.string())};
}
stringstream ss;
ss << file.rdbuf();
@ -199,7 +203,7 @@ vector<entry_type> read_json_files(const bool cache)
fs::path get_lockfile()
{
#if defined(__linux__)
fs::path path{"/run/user/" + to_string(getuid())}; // pam_systemd
fs::path path{format("/run/user/{:d}", getuid())}; // pam_systemd
if (!fs::exists(path))
{
path = "/run/lock"; // Linux
@ -236,8 +240,8 @@ bool create_lockfile()
}
catch (const std::ofstream::failure &e)
{
const auto msg{string("Could not write lockfile: ") += e.what()};
throw std::runtime_error{msg.c_str()};
throw std::runtime_error{
format("Could not write lockfile: {:s}", e.what())};
}
return true;

View File

@ -20,6 +20,8 @@
#include "git.hpp"
#include "time.hpp"
#include <fmt/format.h>
#include <algorithm>
#include <chrono>
#include <cstdint>
@ -35,6 +37,7 @@
namespace FediBlock::rss
{
using fmt::format;
using std::find;
using std::getenv;
using std::none_of;
@ -149,11 +152,11 @@ void write_rss(ostream &out, const vector<entry_type> &entries,
"FediBlock: " + entry.report_time + " " + entry.instance);
write_line(out, 6, "pubDate",
time::to_string(entry.report_time, rss_time_format));
write_line(out, 6, "link", baseurl + '#' += entry.instance);
write_line(out, 6, "link",
format("{:s}#{:s}", baseurl, entry.instance));
string item_description{"<p>" + cgi::text2html(entry.description)
+ "</p>"};
item_description += "<p><strong>Tags:</strong> ";
string item_description{format("<p>{:s}</p><p><strong>Tags:</strong> ",
cgi::text2html(entry.description))};
for (const auto &tag : entry.tags)
{
if (tag != *entry.tags.begin())
@ -162,25 +165,23 @@ void write_rss(ostream &out, const vector<entry_type> &entries,
}
item_description += tag;
}
item_description += "</p>";
item_description += "<strong>Receipts:</strong><ul>";
item_description += "</p><strong>Receipts:</strong><ul>";
for (const auto &receipt : entry.receipts)
{
((((item_description += "<li><a href=\"") += receipt) += "\">") +=
receipt) += "</a></li>";
item_description +=
format("<li><a href=\"{0:s}\">{0:s}</a></li>", receipt);
}
item_description += "</ul>";
if (!entry.screenshot_filepath.empty())
{
item_description += "<p><strong>Screenshot:</strong><br>";
((item_description += "<a href=\"") += baseurl) +=
entry.screenshot_filepath;
item_description += "\"><img src=\"";
(item_description += baseurl) += entry.screenshot_filepath;
(item_description += R"(" height="200"></a></p>)") += '\n';
item_description +=
format("<a href=\"{0:s}{1:s}\"><img "
"src=\"{0:s}{1:s}\" height=\"200\"></a></p>\n",
baseurl, entry.screenshot_filepath);
}
write_line(out, 6, "description",
"<![CDATA[" + item_description + "]]>");
format("<![CDATA[{:s}]]>", item_description));
out << " </item>\n";
}

View File

@ -21,6 +21,7 @@
#include "gitea.hpp"
#include "json.hpp"
#include <fmt/format.h>
#include <git2.h>
#include <chrono>
@ -46,6 +47,7 @@
namespace FediBlock::git
{
using fmt::format;
using std::ofstream;
using std::runtime_error;
using std::string;
@ -57,9 +59,8 @@ using std::this_thread::sleep_for;
using namespace std::chrono_literals;
git_repository *_repo{nullptr};
const string _clone_url{
(((((string{"git@"} += config::forge_domain) += ":") +=
config::forge_org) += "/") += config::forge_repo_data) += ".git"};
const string _clone_url{format("git@{:s}:{:s}/{:s}.git", config::forge_domain,
config::forge_org, config::forge_repo_data)};
fs::path _repo_dir{};
void init(const bool cache)
@ -145,7 +146,8 @@ void commit(const entry_type &entry)
ofstream file(basename + ".json");
if (!file.good())
{
throw runtime_error{"Could not create file: " + basename + ".json"};
throw runtime_error{
format("Could not create file: {:s}.json", basename)};
}
file << json::to_json(entry);
file.close();

View File

@ -22,6 +22,7 @@
#include "types.hpp"
#include <curl/curl.h>
#include <fmt/format.h>
#include <nlohmann/json.hpp>
#include <stdexcept>
@ -31,6 +32,7 @@
namespace FediBlock::gitea
{
using fmt::format;
using std::runtime_error;
using std::string;
using std::string_view;
@ -91,7 +93,7 @@ string api_request(const http_method method, const string_view path,
const string_view body)
{
CURLcode code{};
string url{(string("https://") += config::forge_domain) += path};
string url{format("https://{:s}{:s}", config::forge_domain, path)};
_curl_buffer_body.clear();
switch (method)
@ -122,13 +124,13 @@ string api_request(const http_method method, const string_view path,
code = curl_easy_setopt(_connection, CURLOPT_URL, url.c_str());
if (code != CURLE_OK)
{
throw runtime_error{"Couldn't set URL: " + to_string(code)};
throw runtime_error{format("Couldn't set URL: {:d}", code)};
}
code = curl_easy_perform(_connection);
if (code != CURLE_OK)
{
throw runtime_error{"libcurl error: " + to_string(code)};
throw runtime_error{format("libcurl error: {:d}", code)};
}
long http_status{0}; // NOLINT(google-runtime-int)
@ -136,7 +138,7 @@ string api_request(const http_method method, const string_view path,
curl_easy_getinfo(_connection, CURLINFO_RESPONSE_CODE, &http_status);
if (http_status < 200 || http_status >= 300)
{
throw runtime_error{"HTTP error: " + to_string(http_status)};
throw runtime_error{format("HTTP error: {:d}", http_status)};
}
return _curl_buffer_body;
@ -145,8 +147,8 @@ string api_request(const http_method method, const string_view path,
void pull_request(const string_view branch, const entry_type &entry)
{
api_request(http_method::POST,
(((string("/api/v1/repos/") += config::forge_org) += "/") +=
config::forge_repo_data) += "/pulls",
format("/api/v1/repos/{:s}/{:s}/pulls", config::forge_org,
config::forge_repo_data),
json::pull_request_body(branch, entry));
++_last_pr_number;
}
@ -155,11 +157,11 @@ uint64_t get_last_pr_number()
{
if (_last_pr_number == 0)
{
const auto answer{api_request(
http_method::GET,
(((string("/api/v1/repos/") += config::forge_org) += "/") +=
config::forge_repo_data) += "/pulls",
"?sort=newest&limit=1")};
const auto answer{
api_request(http_method::GET,
format("/api/v1/repos/{:s}/{:s}/pulls",
config::forge_org, config::forge_repo_data),
"?sort=newest&limit=1")};
if (answer == "[]")
{
return 0;

View File

@ -54,8 +54,7 @@ string to_string(const string_view timestring, const string_view format)
std::tm tm{};
tm.tm_isdst = -1; // Detect daylight saving time.
stringstream ss(timestring.data());
ss >> get_time(&tm,
"%Y-%m-%dT%T"); // Assume time is UTC (%F doesn't work).
ss >> get_time(&tm, "%Y-%m-%dT%T"); // Assume time is UTC (%F doesn't work).
return to_string(system_clock::from_time_t(timegm(&tm)), format);
}