Program-Options

main
Peter Fischer 2025-08-16 13:46:50 +02:00
parent 2241571356
commit e7e83723e6
2 changed files with 52 additions and 6 deletions

View File

@ -4,5 +4,5 @@ project(zfsbackupcleaner VERSION 0.1.0 LANGUAGES C CXX)
find_package(Boost COMPONENTS filesystem system date_time program_options process REQUIRED)
add_executable(zfsbackupcleaner main.cpp)
target_link_libraries(zfsbackupcleaner boost_process boost_filesystem)
target_link_libraries(zfsbackupcleaner boost_process boost_filesystem boost_program_options)

View File

@ -2,6 +2,10 @@
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/date_time/posix_time/time_formatters.hpp>
#include <boost/date_time/posix_time/time_parsers.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/positional_options.hpp>
#include <boost/program_options/variables_map.hpp>
#include <iostream>
#include <boost/process.hpp>
@ -12,10 +16,15 @@
#include <boost/date_time/posix_time/posix_time.hpp>
#include <set>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
namespace bp = boost::process;
namespace asio = boost::asio;
namespace po = boost::program_options;
namespace fs = boost::filesystem;
bool dry_run = true;
bool dry_run = false;
template<int keep_every_x_days,int till_y_day>std::set<boost::posix_time::ptime>::reverse_iterator handle_backups_after(std::string & snapshotbase, std::set<boost::posix_time::ptime> & times, std::set<boost::posix_time::ptime>::reverse_iterator ct, boost::posix_time::ptime now)
{
@ -118,13 +127,51 @@ std::set<boost::posix_time::ptime>::reverse_iterator handle_backups_after_365_da
}
int main(int, char**){
int main(int argc, char** argv){
std::cout << "Hello, from zfsbackupcleaner!\n";
po::options_description options("Argumente");
options.add_options()
("help", "Hilfe anzeigen")
("dry-run,n", "Test-Modus; keine Backups entfernen")
("backupsystem", po::value<std::string>(), "Computer, dessen Backups bereinigt werden sollen")
;
po::positional_options_description p;
p.add("backupsystem", 1);
po::variables_map vm;
try {
po::store(po::command_line_parser(argc,argv).options(options).positional(p).run(), vm);
po::notify(vm);
} catch (const boost::program_options::invalid_command_line_syntax & error) {
std::cout << "Error parsing commandline-arguments:" << std::endl;
std::cout << error.what() << std::endl;
return -1;
}
if(vm.count("help") || !vm.count("backupsystem"))
{
std::cout << "Benutzung: zfsbackupcleaner [Optionen] <Backupsystem>" << std::endl << std::endl;
std::cout << options << std::endl;
return 0;
}
if(vm.count("dry-run"))
dry_run = true;
std::string backupsystem = vm["backupsystem"].as<std::string>();
if(backupsystem != "workstation" && ! fs::exists("/backup/"+backupsystem))
{
std::cout << "Für das System >" << backupsystem << "< gibt es keine Backups." << std::endl;
return -1;
}
asio::io_context ctx;
asio::readable_pipe out(ctx);
bp::process proc(ctx, bp::environment::find_executable("zfs"), {"list", "-t", "snapshot", "/backup/workstation"}, bp::process_stdio{{/* in to default*/}, out, {/* err to default */}});
bp::process proc(ctx, bp::environment::find_executable("zfs"), {"list", "-t", "snapshot", "/backup/"+backupsystem}, bp::process_stdio{{/* in to default*/}, out, {/* err to default */}});
std::string output;
@ -139,7 +186,6 @@ int main(int, char**){
std::set<boost::posix_time::ptime> times;
std::string snapshotbase;
for(std::string line; std::getline(lines, line);)
@ -174,7 +220,7 @@ int main(int, char**){
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
std::cout << "NOW " << now << std::endl;
std::cout << "Gehe aus von aktueller Zeit: " << now << std::endl << std::endl;
boost::posix_time::time_duration dur;