SlHelpers
Loading...
Searching...
No Matches
RPMConfig.h
1// SPDX-License-Identifier: GPL-2.0-only
2
3#pragma once
4
5#include <optional>
6#include <string_view>
7#include <unordered_map>
8
9#include "../git/Repo.h"
10#include "../git/Tree.h"
11#include "../helpers/String.h"
12
13namespace SlKernCVS {
14
18class RPMConfig {
19 using ConfigMap = std::unordered_map<std::string, std::string, SlHelpers::String::Hash,
21public:
22 RPMConfig() = delete;
23
25 RPMConfig(std::string_view rpmConfig) noexcept {
26 SlHelpers::GetLine g(rpmConfig);
27 while (auto l = g.get()) {
28 auto trimmed = SlHelpers::String::trim(*l);
29 if (trimmed.empty() || trimmed.front() == '#')
30 continue;
31 auto eq = trimmed.find('=');
32 if (eq == trimmed.npos)
33 continue;
34 auto key = SlHelpers::String::trim(trimmed.substr(0, eq));
35 auto val = SlHelpers::String::trim(trimmed.substr(eq + 1));
36 if (val.size() >= 2 && val.front() == '"' && val.back() == '"')
37 val = val.substr(1, val.size() - 2);
38
39 m_config[std::string(key)] = val;
40 }
41 }
42
44 static std::optional<RPMConfig> create(const SlGit::Tree &tree) noexcept {
45 if (const auto config = tree.catFile("rpm/config.sh"))
46 return RPMConfig(*config);
47
48 return std::nullopt;
49 }
50
52 static std::optional<RPMConfig> create(const SlGit::Repo &repo,
53 const std::string &branch) noexcept {
54 if (const auto config = repo.catFile("origin/" + branch, "rpm/config.sh"))
55 return RPMConfig(*config);
56
57 return std::nullopt;
58 }
59
61 bool contains(std::string_view key) const noexcept { return m_config.contains(key); }
62
64 std::optional<std::reference_wrapper<const std::string>>
65 get(std::string_view key) const noexcept {
66 auto it = m_config.find(key);
67 if (it == m_config.end())
68 return std::nullopt;
69 return std::cref(it->second);
70 }
71
73 const auto &operator[](std::string_view key) const noexcept {
74 return m_config.find(key)->second;
75 }
76private:
77 ConfigMap m_config;
78};
79
80}
The most important Git class.
Definition Repo.h:45
Tree is a representation of a git tree.
Definition Tree.h:27
Parses a string view into lines.
Definition String.h:25
std::optional< std::string_view > get() noexcept
Read one line.
Definition String.h:37
static constexpr T trim(const T &line) noexcept
Trim string (remove surrounding whitespace).
Definition String.h:158
RPMConfig(std::string_view rpmConfig) noexcept
Create a new RPMConfig from the rpmConfig.
Definition RPMConfig.h:25
bool contains(std::string_view key) const noexcept
Test whether key exists in the config.
Definition RPMConfig.h:61
static std::optional< RPMConfig > create(const SlGit::Repo &repo, const std::string &branch) noexcept
Create a new RPMConfig from the branch in the repo.
Definition RPMConfig.h:52
static std::optional< RPMConfig > create(const SlGit::Tree &tree) noexcept
Create a new RPMConfig from the tree.
Definition RPMConfig.h:44
const auto & operator[](std::string_view key) const noexcept
Find key in the config and return its value.
Definition RPMConfig.h:73
std::optional< std::reference_wrapper< const std::string > > get(std::string_view key) const noexcept
Find key in the config and return its value if found (or nullopt).
Definition RPMConfig.h:65
Equality test for string and string_view to be used in containers.
Definition String.h:250
Hash for string and string_view to be used in hashing containers.
Definition String.h:223