Add --dry-run.
Do everything like normal, but don't post anything and don't update the config file.
This commit is contained in:
parent
99a843e0a2
commit
dfd32e5bbf
|
@ -2,7 +2,7 @@
|
||||||
:doctype: manpage
|
:doctype: manpage
|
||||||
:Author: tastytea
|
:Author: tastytea
|
||||||
:Email: tastytea@tastytea.de
|
:Email: tastytea@tastytea.de
|
||||||
:Date: 2020-10-29
|
:Date: 2020-11-21
|
||||||
:Revision: 0.0.0
|
:Revision: 0.0.0
|
||||||
:man source: mastorss
|
:man source: mastorss
|
||||||
:man manual: General Commands Manual
|
:man manual: General Commands Manual
|
||||||
|
@ -22,6 +22,11 @@ support Atom at the moment.
|
||||||
|
|
||||||
== OPTIONS
|
== OPTIONS
|
||||||
|
|
||||||
|
*--dry-run*::
|
||||||
|
Do everything like normal, but don't post anything and don't update the config
|
||||||
|
file. The initial config file is still created, if the profile doesn't
|
||||||
|
exist. The interval between posts is set to 1 second.
|
||||||
|
|
||||||
*--help*::
|
*--help*::
|
||||||
Show help message.
|
Show help message.
|
||||||
|
|
||||||
|
|
112
src/main.cpp
112
src/main.cpp
|
@ -34,8 +34,13 @@
|
||||||
|
|
||||||
namespace mastorss
|
namespace mastorss
|
||||||
{
|
{
|
||||||
|
|
||||||
using std::cerr;
|
using std::cerr;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
|
using std::runtime_error;
|
||||||
|
using std::string_view;
|
||||||
|
using std::chrono::seconds;
|
||||||
|
using std::this_thread::sleep_for;
|
||||||
|
|
||||||
namespace error
|
namespace error
|
||||||
{
|
{
|
||||||
|
@ -50,6 +55,7 @@ constexpr int unknown = 9;
|
||||||
|
|
||||||
void print_version();
|
void print_version();
|
||||||
void print_help(const string_view &command);
|
void print_help(const string_view &command);
|
||||||
|
int run(string_view profile_name, bool dry_run);
|
||||||
|
|
||||||
void print_version()
|
void print_version()
|
||||||
{
|
{
|
||||||
|
@ -67,51 +73,10 @@ void print_help(const string_view &command)
|
||||||
{
|
{
|
||||||
cerr << "Usage: " << command << " [--version|--help] <profile>\n";
|
cerr << "Usage: " << command << " [--version|--help] <profile>\n";
|
||||||
}
|
}
|
||||||
} // namespace mastorss
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int run(const string_view profile_name, const bool dry_run)
|
||||||
{
|
{
|
||||||
using namespace mastorss;
|
const string_view profilename{profile_name};
|
||||||
using std::cerr;
|
|
||||||
using std::getenv;
|
|
||||||
using std::runtime_error;
|
|
||||||
using std::string_view;
|
|
||||||
using std::vector;
|
|
||||||
using std::chrono::seconds;
|
|
||||||
using std::this_thread::sleep_for;
|
|
||||||
|
|
||||||
const vector<string_view> args(argv, argv + argc);
|
|
||||||
|
|
||||||
if (getenv("MASTORSS_DEBUG") == nullptr)
|
|
||||||
{
|
|
||||||
boost::log::core::get()->set_filter(boost::log::trivial::severity
|
|
||||||
>= boost::log::trivial::info);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
boost::log::core::get()->set_filter(boost::log::trivial::severity
|
|
||||||
>= boost::log::trivial::debug);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args.size() == 1)
|
|
||||||
{
|
|
||||||
print_help(args[0]);
|
|
||||||
return error::noprofile;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args.size() > 1)
|
|
||||||
{
|
|
||||||
if (args[1] == "--version")
|
|
||||||
{
|
|
||||||
print_version();
|
|
||||||
}
|
|
||||||
else if (args[1] == "--help")
|
|
||||||
{
|
|
||||||
print_help(args[0]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
const string_view profilename{args[1]};
|
|
||||||
BOOST_LOG_TRIVIAL(debug) << "Using profile: " << profilename;
|
BOOST_LOG_TRIVIAL(debug) << "Using profile: " << profilename;
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -125,15 +90,25 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
for (const auto &item : doc.new_items)
|
for (const auto &item : doc.new_items)
|
||||||
{
|
{
|
||||||
masto.post_item(item);
|
masto.post_item(item, dry_run);
|
||||||
if (item != *doc.new_items.rbegin())
|
if (item != *doc.new_items.rbegin())
|
||||||
{ // Don't sleep if this is the last item.
|
{ // Don't sleep if this is the last item.
|
||||||
|
if (!dry_run)
|
||||||
|
{
|
||||||
sleep_for(seconds(cfg.profiledata.interval));
|
sleep_for(seconds(cfg.profiledata.interval));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sleep_for(seconds(1));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!dry_run)
|
||||||
|
{
|
||||||
cfg.write();
|
cfg.write();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (const FileException &e)
|
catch (const FileException &e)
|
||||||
{
|
{
|
||||||
cerr << e.what() << '\n';
|
cerr << e.what() << '\n';
|
||||||
|
@ -164,6 +139,55 @@ int main(int argc, char *argv[])
|
||||||
cerr << e.what() << '\n';
|
cerr << e.what() << '\n';
|
||||||
return error::unknown;
|
return error::unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace mastorss
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
using namespace mastorss;
|
||||||
|
using std::getenv;
|
||||||
|
using std::string_view;
|
||||||
|
using std::vector;
|
||||||
|
|
||||||
|
const vector<string_view> args(argv, argv + argc);
|
||||||
|
|
||||||
|
if (getenv("MASTORSS_DEBUG") == nullptr)
|
||||||
|
{
|
||||||
|
boost::log::core::get()->set_filter(boost::log::trivial::severity
|
||||||
|
>= boost::log::trivial::info);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
boost::log::core::get()->set_filter(boost::log::trivial::severity
|
||||||
|
>= boost::log::trivial::debug);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.size() == 1)
|
||||||
|
{
|
||||||
|
print_help(args[0]);
|
||||||
|
return error::noprofile;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.size() > 1)
|
||||||
|
{
|
||||||
|
if (args[1] == "--version")
|
||||||
|
{
|
||||||
|
print_version();
|
||||||
|
}
|
||||||
|
else if (args[1] == "--help")
|
||||||
|
{
|
||||||
|
print_help(args[0]);
|
||||||
|
}
|
||||||
|
else if (args[1] == "--dry-run")
|
||||||
|
{
|
||||||
|
return run(args[2], true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return run(args[1], false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include <boost/log/trivial.hpp>
|
#include <boost/log/trivial.hpp>
|
||||||
#include <boost/regex.hpp>
|
#include <boost/regex.hpp>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
|
@ -36,7 +37,7 @@ MastoAPI::MastoAPI(ProfileData &data)
|
||||||
, _instance{_profile.instance, _profile.access_token}
|
, _instance{_profile.instance, _profile.access_token}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void MastoAPI::post_item(const Item &item)
|
void MastoAPI::post_item(const Item &item, bool dry_run)
|
||||||
{
|
{
|
||||||
string title = replacements_apply(item.title);
|
string title = replacements_apply(item.title);
|
||||||
string link = replacements_apply(item.link);
|
string link = replacements_apply(item.link);
|
||||||
|
@ -109,6 +110,8 @@ void MastoAPI::post_item(const Item &item)
|
||||||
BOOST_LOG_TRIVIAL(debug) << "Status length: " << status.size();
|
BOOST_LOG_TRIVIAL(debug) << "Status length: " << status.size();
|
||||||
BOOST_LOG_TRIVIAL(debug) << "Status: \"" << status << '"';
|
BOOST_LOG_TRIVIAL(debug) << "Status: \"" << status << '"';
|
||||||
|
|
||||||
|
if (!dry_run)
|
||||||
|
{
|
||||||
mastodonpp::parametermap params{{"status", status}};
|
mastodonpp::parametermap params{{"status", status}};
|
||||||
if (_profile.titles_as_cw)
|
if (_profile.titles_as_cw)
|
||||||
{
|
{
|
||||||
|
@ -125,6 +128,14 @@ void MastoAPI::post_item(const Item &item)
|
||||||
}
|
}
|
||||||
throw CURLException{ret.curl_error_code};
|
throw CURLException{ret.curl_error_code};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
using std::cout;
|
||||||
|
cout << " WOULD POST: \n";
|
||||||
|
cout << "Subject: " << title << '\n';
|
||||||
|
cout << "Status:\n" << status << '\n';
|
||||||
|
}
|
||||||
BOOST_LOG_TRIVIAL(debug) << "Posted status with GUID: " << item.guid;
|
BOOST_LOG_TRIVIAL(debug) << "Posted status with GUID: " << item.guid;
|
||||||
|
|
||||||
_profile.guids.push_back(item.guid);
|
_profile.guids.push_back(item.guid);
|
||||||
|
|
|
@ -33,7 +33,7 @@ class MastoAPI
|
||||||
public:
|
public:
|
||||||
explicit MastoAPI(ProfileData &data);
|
explicit MastoAPI(ProfileData &data);
|
||||||
|
|
||||||
void post_item(const Item &item);
|
void post_item(const Item &item, bool dry_run);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ProfileData &_profile;
|
ProfileData &_profile;
|
||||||
|
|
Loading…
Reference in New Issue