👷 fetch initcpio first

This commit is contained in:
Vladislav Nepogodin 2022-08-11 22:33:43 +04:00
parent 6c6d098ff9
commit e84a766b4d
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
3 changed files with 49 additions and 4 deletions

View File

@ -3,6 +3,7 @@
#include <fstream> // for ofstream
#include <fmt/core.h>
#include <fmt/format.h>
#if defined(__clang__)
@ -62,4 +63,33 @@ bool Initcpio::write() const noexcept {
return true;
}
bool Initcpio::parse_file() noexcept {
auto&& file_content = utils::read_whole_file(m_file_path);
const auto& parse_line = [](auto&& line) -> std::vector<std::string> {
auto&& open_bracket_pos = line.find('(');
auto&& close_bracket = ranges::find(line, ')');
if (open_bracket_pos != std::string::npos && close_bracket != line.end()) {
const auto length = ranges::distance(line.begin() + static_cast<long>(open_bracket_pos), close_bracket - 1);
auto&& input_data = line.substr(open_bracket_pos + 1, static_cast<std::size_t>(length));
return input_data | ranges::views::split(' ') | ranges::to<std::vector<std::string>>();
}
return {};
};
auto&& file_content_lines = utils::make_multiline(file_content);
for (auto&& line : file_content_lines) {
if (line.starts_with("MODULES")) {
modules = parse_line(line);
} else if (line.starts_with("FILES")) {
files = parse_line(line);
} else if (line.starts_with("HOOKS")) {
hooks = parse_line(line);
}
}
return true;
}
} // namespace detail

View File

@ -11,6 +11,19 @@ class Initcpio {
public:
Initcpio(const std::string_view& file_path) noexcept : m_file_path(file_path) {}
bool parse_file() noexcept;
inline bool append_module(std::string&& module) noexcept {
if (!this->parse_file()) { return false; }
modules.emplace_back(std::move(module));
return this->write();
}
inline bool append_hook(std::string&& hook) noexcept {
if (!this->parse_file()) { return false; }
hooks.emplace_back(std::move(hook));
return this->write();
}
bool write() const noexcept;
std::vector<std::string> modules{};

View File

@ -28,7 +28,7 @@ HOOKS=(base udev autodetect modconf block filesystems keyboard fsck)
static constexpr auto MKINITCPIO_TEST = R"(
# MODULES
MODULES=(crc32c-intel)
MODULES=(radeon crc32c-intel)
# BINARIES
BINARIES=()
@ -37,7 +37,7 @@ BINARIES=()
FILES=()
# HOOKS
HOOKS=(base usr lvm2 zfs)
HOOKS=(base udev autodetect modconf block filesystems keyboard fsck btrfs usr lvm2 zfs)
)";
@ -55,15 +55,17 @@ int main() {
auto initcpio = detail::Initcpio{filename};
// Insert data.
initcpio.append_module("radeon");
initcpio.append_hook("btrfs");
initcpio.modules.insert(initcpio.modules.end(), {"crc32c-intel"});
initcpio.hooks.insert(initcpio.hooks.end(), {"base", "usr", "lvm2", "zfs"});
initcpio.hooks.insert(initcpio.hooks.end(), {"usr", "lvm2", "zfs"});
// Write data.
assert(initcpio.write());
// Check if file is equal to test data.
// "\n# MODULES\nMODULES=(crc32c-intel)\n\n# BINARIES\nBINARIES=()\n\n# FILES\nFILES=()\n\n# HOOKS\nHOOKS=(base usr lvm2 zfs)"\n
const auto&& file_content = utils::read_whole_file(filename);
const auto& file_content = utils::read_whole_file(filename);
assert(file_content == MKINITCPIO_TEST);
// Cleanup.