Manager: Add new pattern logic to system config files
This commit is contained in:
+7
-6
@@ -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
|
||||
|
||||
|
||||
+4
-4
@@ -7,9 +7,9 @@
|
||||
"README.org",
|
||||
"screenshot.png"
|
||||
],
|
||||
"systemDirectories": [
|
||||
"/boot",
|
||||
"/etc",
|
||||
"/usr/share"
|
||||
"systemPatterns": [
|
||||
"/boot/",
|
||||
"/etc/",
|
||||
"/usr/share/"
|
||||
]
|
||||
}
|
||||
|
||||
+3
-3
@@ -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
@@ -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
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user