Skip to content

Commit

Permalink
(wip) feature: add diagnostic data to --version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
DSiekmeier committed Nov 7, 2023
1 parent 9f361b3 commit d015207
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ add_library(${PROJECT_NAME}-lib OBJECT
src/details.cc
src/library.cc
src/statistics.cc
src/sysinfoaggregator.cc
src/utilities.cc
)

Expand Down
18 changes: 17 additions & 1 deletion src/cli.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cli.h"
#include "sysinfoaggregator.h"

using namespace booktrack_cli;

Expand All @@ -10,7 +11,22 @@ void AddMainOptions(CLI::App& app, CliOptions& opt) {
version_string << BOOKTRACK_CLI_VERSION_MAJOR << "."
<< BOOKTRACK_CLI_VERSION_MINOR << "."
<< BOOKTRACK_CLI_VERSION_PATCH;
return "booktrack-cli v" + version_string.str();
const auto booktrack_version =
"booktrack-cli v" + version_string.str() + '\n';

SysInfo system_information{"<invalid value>", "<invalid value>"};
try {
SysInfoAggregator sysinfo("/etc/os-release");
system_information = sysinfo.GetSysInfo();
} catch (const std::runtime_error& ex) {
std::cout << ex.what() << '\n';
}
const auto os_release_name =
"OS name : " + system_information.os_release_name + '\n';
const auto os_release_version =
"OS version: " + system_information.os_release_version + '\n';

return booktrack_version + '\n' + os_release_name + os_release_version;
});

app.add_option("-l,--library,", opt.library_file_path,
Expand Down
42 changes: 42 additions & 0 deletions src/sysinfoaggregator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <exception>
#include <fstream>

#include "sysinfoaggregator.h"
using namespace booktrack_cli;

SysInfo SysInfoAggregator::GetSysInfo() {
SysInfo value;
std::ifstream input_file{"/etc/os-release"};

if (input_file.good()) {
std::string line;
while (getline(input_file, line)) {
auto splitted = split_entries(line);
if (splitted[0] == "NAME") {
value.os_release_name = splitted[1];
}
if (splitted[0] == "VERSION") {
value.os_release_version = splitted[1];
}
}
input_file.close();
} else {
throw std::runtime_error("Could not open file.");
}

return value;
}

std::vector<std::string> SysInfoAggregator::split_entries(
const std::string& s) {
std::vector<std::string> result;
std::string::size_type start{0};
std::string::size_type position;
do {
position = s.find_first_of("=", start);
result.push_back(s.substr(start, position - start));
start = position + 1;
} while (position != std::string::npos);

return result;
}
37 changes: 37 additions & 0 deletions src/sysinfoaggregator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef BOOKTRACK_CLI_SYSINFOAGGREGATOR_H
#define BOOKTRACK_CLI_SYSINFOAGGREGATOR_H

#include <filesystem>
#include <string>
#include <vector>

namespace fs = std::filesystem;

namespace booktrack_cli {

struct SysInfo {
std::string os_release_name;
std::string os_release_version;
};

class SysInfoAggregator {
public:
explicit SysInfoAggregator(fs::path release_file)
: release_file_{release_file} {};

/**
* @brief Get the Sys Info object
*
* @return SysInfo
* @throws std::runtime_error
*/
SysInfo GetSysInfo();

private:
std::vector<std::string> split_entries(const std::string& s);

fs::path release_file_;
};
} // namespace booktrack_cli

#endif

0 comments on commit d015207

Please sign in to comment.