From db22cc9f06503c43cf1ae3003316ff40d0f455c1 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Tue, 8 Mar 2022 00:03:02 +0100 Subject: [PATCH] Manager: Add new pattern logic to system config files --- README.org | 13 +++++++------ doc/manafiles.json | 8 ++++---- src/config.cpp | 6 +++--- src/config.h | 12 ++++++------ src/dotfile.cpp | 22 +++------------------- src/dotfile.h | 1 - test/unit/testdotfile.cpp | 8 ++++---- 7 files changed, 27 insertions(+), 43 deletions(-) diff --git a/README.org b/README.org index cd7da4e..80ccd66 100644 --- a/README.org +++ b/README.org @@ -58,14 +58,15 @@ The ignore patterns from the example config: **** System config files -Directories in this list will be pushed to the root ~/~ of the system, instead of ~$HOME~. +Everything in this list will be pushed to the root ~/~ of the system, instead of ~$HOME~. \\ +These support the same file matching as explained in the [[#ignore-patterns][Ignore patterns]] section. -The system directories from the example config: +The system patterns from the example config: #+BEGIN_SRC javascript -"systemDirectories": [ - "/boot", - "/etc", - "/usr/share" +"systemPatterns": [ + "/boot/", + "/etc/", + "/usr/share/" ] #+END_SRC diff --git a/doc/manafiles.json b/doc/manafiles.json index 8103dc5..05d075e 100644 --- a/doc/manafiles.json +++ b/doc/manafiles.json @@ -7,9 +7,9 @@ "README.org", "screenshot.png" ], - "systemDirectories": [ - "/boot", - "/etc", - "/usr/share" + "systemPatterns": [ + "/boot/", + "/etc/", + "/usr/share/" ] } diff --git a/src/config.cpp b/src/config.cpp index 951b9af..5fa0d78 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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); } } diff --git a/src/config.h b/src/config.h index 785f03b..e64c3ae 100644 --- a/src/config.h +++ b/src/config.h @@ -25,10 +25,10 @@ struct Settings { "README.org", "screenshot.png", }; - std::vector systemDirectories { - "/boot", - "/etc", - "/usr/share" + std::vector systemPatterns { + "/boot/", + "/etc/", + "/usr/share/" }; }; @@ -37,12 +37,12 @@ public: Config(s); virtual ~Config(); - void setSystemDirectories(const std::vector& systemDirectories) { m_settings.systemDirectories = systemDirectories; } + void setSystemPatterns(const std::vector& systemPatterns) { m_settings.systemPatterns = systemPatterns; } void setIgnorePatterns(const std::vector& ignorePatterns) { m_settings.ignorePatterns = ignorePatterns; } void setVerbose(bool verbose) { m_verbose = verbose; } const std::vector& ignorePatterns() const { return m_settings.ignorePatterns; } - const std::vector& systemDirectories() const { return m_settings.systemDirectories; } + const std::vector& systemPatterns() const { return m_settings.systemPatterns; } const std::filesystem::path& workingDirectory() const { return m_workingDirectory; } size_t workingDirectorySize() const { return m_workingDirectorySize; } diff --git a/src/dotfile.cpp b/src/dotfile.cpp index 87c11aa..a40a5fe 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -51,7 +51,8 @@ void Dotfile::add(const std::vector& 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& 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& 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; -} diff --git a/src/dotfile.h b/src/dotfile.h index 6ec2869..b94c701 100644 --- a/src/dotfile.h +++ b/src/dotfile.h @@ -42,7 +42,6 @@ private: void selectivelyCommentOrUncomment(const std::string& path); void forEachDotfile(const std::vector& targets, const std::function& callback); - bool isSystemTarget(const std::string& target); }; #endif // DOTFILE_H diff --git a/test/unit/testdotfile.cpp b/test/unit/testdotfile.cpp index fb08365..f7ccc49 100644 --- a/test/unit/testdotfile.cpp +++ b/test/unit/testdotfile.cpp @@ -868,9 +868,9 @@ TEST_CASE(AddSystemDotfiles) { EXPECT(geteuid() == 0, return ); - Config::the().setSystemDirectories({ "/etc", "/usr/lib" }); + Config::the().setSystemPatterns({ "/etc/", "/usr/lib/" }); Dotfile::the().add({ "/etc/group", "/usr/lib/os-release" }); - Config::the().setSystemDirectories({}); + Config::the().setSystemPatterns({}); EXPECT(std::filesystem::exists("etc/group")); EXPECT(std::filesystem::exists("usr/lib/os-release")); @@ -885,9 +885,9 @@ TEST_CASE(PullSystemDotfiles) createTestDotfiles({ "etc/group" }, { "" }, true); - Config::the().setSystemDirectories({ "/etc" }); + Config::the().setSystemPatterns({ "/etc/" }); Dotfile::the().pull({ "etc/group" }); - Config::the().setSystemDirectories({}); + Config::the().setSystemPatterns({}); Util::File lhs("/etc/group"); Util::File rhs(Config::the().workingDirectory() / "etc/group");