Browse Source

Manager: Add new pattern logic to system config files

master
Riyyi 3 years ago
parent
commit
db22cc9f06
  1. 13
      README.org
  2. 8
      doc/manafiles.json
  3. 6
      src/config.cpp
  4. 12
      src/config.h
  5. 22
      src/dotfile.cpp
  6. 1
      src/dotfile.h
  7. 8
      test/unit/testdotfile.cpp

13
README.org

@ -58,14 +58,15 @@ The ignore patterns from the example config:
**** System config files **** 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 #+BEGIN_SRC javascript
"systemDirectories": [ "systemPatterns": [
"/boot", "/boot/",
"/etc", "/etc/",
"/usr/share" "/usr/share/"
] ]
#+END_SRC #+END_SRC

8
doc/manafiles.json

@ -7,9 +7,9 @@
"README.org", "README.org",
"screenshot.png" "screenshot.png"
], ],
"systemDirectories": [ "systemPatterns": [
"/boot", "/boot/",
"/etc", "/etc/",
"/usr/share" "/usr/share/"
] ]
} }

6
src/config.cpp

@ -76,7 +76,7 @@ void to_json(nlohmann::json& object, const Settings& settings)
{ {
object = nlohmann::json { object = nlohmann::json {
{ "ignorePatterns", settings.ignorePatterns }, { "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); object.at("ignorePatterns").get_to(settings.ignorePatterns);
} }
if (object.find("systemDirectories") != object.end()) { if (object.find("systemPatterns") != object.end()) {
object.at("systemDirectories").get_to(settings.systemDirectories); object.at("systemPatterns").get_to(settings.systemPatterns);
} }
} }

12
src/config.h

@ -25,10 +25,10 @@ struct Settings {
"README.org", "README.org",
"screenshot.png", "screenshot.png",
}; };
std::vector<std::filesystem::path> systemDirectories { std::vector<std::string> systemPatterns {
"/boot", "/boot/",
"/etc", "/etc/",
"/usr/share" "/usr/share/"
}; };
}; };
@ -37,12 +37,12 @@ public:
Config(s); Config(s);
virtual ~Config(); 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 setIgnorePatterns(const std::vector<std::string>& ignorePatterns) { m_settings.ignorePatterns = ignorePatterns; }
void setVerbose(bool verbose) { m_verbose = verbose; } void setVerbose(bool verbose) { m_verbose = verbose; }
const std::vector<std::string>& ignorePatterns() const { return m_settings.ignorePatterns; } 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; } const std::filesystem::path& workingDirectory() const { return m_workingDirectory; }
size_t workingDirectorySize() const { return m_workingDirectorySize; } size_t workingDirectorySize() const { return m_workingDirectorySize; }

22
src/dotfile.cpp

@ -51,7 +51,8 @@ void Dotfile::add(const std::vector<std::string>& targets)
continue; continue;
} }
if (isSystemTarget(targets.at(i))) { if (match(std::filesystem::directory_entry { targets.at(i) },
Config::the().systemPatterns())) {
systemIndices.push_back(i); systemIndices.push_back(i);
} }
else { else {
@ -243,7 +244,7 @@ void Dotfile::pullOrPush(SyncType type, const std::vector<std::string>& targets)
// Separate home and system targets // Separate home and system targets
forEachDotfile(targets, [&](const std::filesystem::directory_entry& path, size_t index) { forEachDotfile(targets, [&](const std::filesystem::directory_entry& path, size_t index) {
dotfiles.push_back(path.path().string()); dotfiles.push_back(path.path().string());
if (isSystemTarget(path.path().string())) { if (match(path, Config::the().systemPatterns())) {
systemIndices.push_back(index); systemIndices.push_back(index);
} }
else { else {
@ -500,20 +501,3 @@ void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std:
callback(path, index++); 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
src/dotfile.h

@ -42,7 +42,6 @@ private:
void selectivelyCommentOrUncomment(const std::string& path); 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); 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 #endif // DOTFILE_H

8
test/unit/testdotfile.cpp

@ -868,9 +868,9 @@ TEST_CASE(AddSystemDotfiles)
{ {
EXPECT(geteuid() == 0, return ); 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" }); 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("etc/group"));
EXPECT(std::filesystem::exists("usr/lib/os-release")); EXPECT(std::filesystem::exists("usr/lib/os-release"));
@ -885,9 +885,9 @@ TEST_CASE(PullSystemDotfiles)
createTestDotfiles({ "etc/group" }, { "" }, true); createTestDotfiles({ "etc/group" }, { "" }, true);
Config::the().setSystemDirectories({ "/etc" }); Config::the().setSystemPatterns({ "/etc/" });
Dotfile::the().pull({ "etc/group" }); Dotfile::the().pull({ "etc/group" });
Config::the().setSystemDirectories({}); Config::the().setSystemPatterns({});
Util::File lhs("/etc/group"); Util::File lhs("/etc/group");
Util::File rhs(Config::the().workingDirectory() / "etc/group"); Util::File rhs(Config::the().workingDirectory() / "etc/group");

Loading…
Cancel
Save