Manager: Add new pattern logic to system config files

This commit is contained in:
Riyyi
2022-03-08 00:06:01 +01:00
parent 3b4ea3f42d
commit db22cc9f06
7 changed files with 27 additions and 43 deletions
+3 -3
View File
@@ -76,7 +76,7 @@ void to_json(nlohmann::json& object, const Settings& settings)
{
object = nlohmann::json {
{ "ignorePatterns", settings.ignorePatterns },
{ "systemDirectories", settings.systemDirectories }
{ "systemPatterns", settings.systemPatterns }
};
}
@@ -86,7 +86,7 @@ void from_json(const nlohmann::json& object, Settings& settings)
object.at("ignorePatterns").get_to(settings.ignorePatterns);
}
if (object.find("systemDirectories") != object.end()) {
object.at("systemDirectories").get_to(settings.systemDirectories);
if (object.find("systemPatterns") != object.end()) {
object.at("systemPatterns").get_to(settings.systemPatterns);
}
}
+6 -6
View File
@@ -25,10 +25,10 @@ struct Settings {
"README.org",
"screenshot.png",
};
std::vector<std::filesystem::path> systemDirectories {
"/boot",
"/etc",
"/usr/share"
std::vector<std::string> systemPatterns {
"/boot/",
"/etc/",
"/usr/share/"
};
};
@@ -37,12 +37,12 @@ public:
Config(s);
virtual ~Config();
void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { m_settings.systemDirectories = systemDirectories; }
void setSystemPatterns(const std::vector<std::string>& systemPatterns) { m_settings.systemPatterns = systemPatterns; }
void setIgnorePatterns(const std::vector<std::string>& ignorePatterns) { m_settings.ignorePatterns = ignorePatterns; }
void setVerbose(bool verbose) { m_verbose = verbose; }
const std::vector<std::string>& ignorePatterns() const { return m_settings.ignorePatterns; }
const std::vector<std::filesystem::path>& systemDirectories() const { return m_settings.systemDirectories; }
const std::vector<std::string>& systemPatterns() const { return m_settings.systemPatterns; }
const std::filesystem::path& workingDirectory() const { return m_workingDirectory; }
size_t workingDirectorySize() const { return m_workingDirectorySize; }
+3 -19
View File
@@ -51,7 +51,8 @@ void Dotfile::add(const std::vector<std::string>& targets)
continue;
}
if (isSystemTarget(targets.at(i))) {
if (match(std::filesystem::directory_entry { targets.at(i) },
Config::the().systemPatterns())) {
systemIndices.push_back(i);
}
else {
@@ -243,7 +244,7 @@ void Dotfile::pullOrPush(SyncType type, const std::vector<std::string>& targets)
// Separate home and system targets
forEachDotfile(targets, [&](const std::filesystem::directory_entry& path, size_t index) {
dotfiles.push_back(path.path().string());
if (isSystemTarget(path.path().string())) {
if (match(path, Config::the().systemPatterns())) {
systemIndices.push_back(index);
}
else {
@@ -500,20 +501,3 @@ void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std:
callback(path, index++);
}
}
bool Dotfile::isSystemTarget(const std::string& target)
{
for (const auto& systemDirectory : Config::the().systemDirectories()) {
if (target.find(systemDirectory) == 0) {
return true;
}
// FIXME: The second filesystem::path cant have a "/" as the first character,
// as it will think the path is at the root.
if (target.find(Config::the().workingDirectory().string() + systemDirectory.string()) == 0) {
return true;
}
}
return false;
}
-1
View File
@@ -42,7 +42,6 @@ private:
void selectivelyCommentOrUncomment(const std::string& path);
void forEachDotfile(const std::vector<std::string>& targets, const std::function<void(const std::filesystem::directory_entry&, size_t)>& callback);
bool isSystemTarget(const std::string& target);
};
#endif // DOTFILE_H