From 12c11cff9e3b75238a2ecc79991aa126aeee9f5b Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 7 Feb 2022 21:26:50 +0100 Subject: [PATCH] Manager: Move working directory to the config class --- src/config.cpp | 19 +++++++++++++++++++ src/config.h | 12 ++++++++++-- src/dotfile.cpp | 38 ++++++++++++++++++-------------------- src/dotfile.h | 9 --------- test/unit/testdotfile.cpp | 9 +++++---- 5 files changed, 52 insertions(+), 35 deletions(-) create mode 100644 src/config.cpp diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..82ad2fa --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2022 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#include + +#include "config.h" + +Config::Config(s) + : m_workingDirectory(std::filesystem::current_path()) + , m_workingDirectorySize(m_workingDirectory.string().size()) +{ +} + +Config::~Config() +{ +} diff --git a/src/config.h b/src/config.h index 903c01f..7a324ae 100644 --- a/src/config.h +++ b/src/config.h @@ -7,19 +7,27 @@ #ifndef CONFIG_H #define CONFIG_H +#include // size_t +#include + #include "util/singleton.h" class Config : public Util::Singleton { public: - Config(s) {} - virtual ~Config() {} + Config(s); + virtual ~Config(); void setVerbose(bool verbose) { m_verbose = verbose; } + const std::filesystem::path& workingDirectory() const { return m_workingDirectory; } + size_t workingDirectorySize() const { return m_workingDirectorySize; } bool verbose() const { return m_verbose; } private: bool m_verbose { false }; + + std::filesystem::path m_workingDirectory {}; + size_t m_workingDirectorySize { 0 }; }; #endif // CONFIG_H diff --git a/src/dotfile.cpp b/src/dotfile.cpp index 677624a..a340eb3 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -21,8 +21,6 @@ #include "util/file.h" Dotfile::Dotfile(s) - : m_workingDirectory(std::filesystem::current_path()) - , m_workingDirectorySize(m_workingDirectory.string().size()) { } @@ -82,18 +80,18 @@ void Dotfile::add(const std::vector& targets) void Dotfile::list(const std::vector& targets) { - if (m_workingDirectory.empty()) { + if (Config::the().workingDirectory().empty()) { fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is unset\n"); return; } - if (!std::filesystem::is_directory(m_workingDirectory)) { + if (!std::filesystem::is_directory(Config::the().workingDirectory())) { fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is not a directory\n"); return; } - forEachDotfile(targets, [this](std::filesystem::directory_entry path, size_t) { - printf("%s\n", path.path().c_str() + m_workingDirectorySize + 1); + forEachDotfile(targets, [](std::filesystem::directory_entry path, size_t) { + printf("%s\n", path.path().c_str() + Config::the().workingDirectorySize() + 1); }); } @@ -129,33 +127,33 @@ void Dotfile::pullOrPush(SyncType type, const std::vector& targets) if (type == SyncType::Pull) { sync( type, dotfiles, homeIndices, systemIndices, - [this](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { + [](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { // homeFile = /home//dotfiles/ // copy: /home// -> /home//dotfiles/ - paths[0] = homeDirectory + homeFile.substr(m_workingDirectorySize); + paths[0] = homeDirectory + homeFile.substr(Config::the().workingDirectorySize()); paths[1] = homeFile; }, - [this](std::string* paths, const std::string& systemFile) { + [](std::string* paths, const std::string& systemFile) { // systemFile = /home//dotfiles/ // copy: -> /home//dotfiles/ - paths[0] = systemFile.substr(m_workingDirectorySize); + paths[0] = systemFile.substr(Config::the().workingDirectorySize()); paths[1] = systemFile; }); } else { sync( type, dotfiles, homeIndices, systemIndices, - [this](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { + [](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { // homeFile = /home//dotfiles/ // copy: /home//dotfiles/ -> /home// paths[0] = homeFile; - paths[1] = homeDirectory + homeFile.substr(m_workingDirectorySize); + paths[1] = homeDirectory + homeFile.substr(Config::the().workingDirectorySize()); }, - [this](std::string* paths, const std::string& systemFile) { + [](std::string* paths, const std::string& systemFile) { // systemFile = /home//dotfiles/ // copy: /home//dotfiles/ -> paths[0] = systemFile; - paths[1] = systemFile.substr(m_workingDirectorySize); + paths[1] = systemFile.substr(Config::the().workingDirectorySize()); }); } } @@ -169,7 +167,7 @@ void Dotfile::sync(SyncType type, if (!systemIndices.empty() && !root) { for (size_t i : systemIndices) { fprintf(stderr, "\033[31;1mDotfile:\033[0m need root privileges to copy system file '%s'\n", - paths.at(i).c_str() + m_workingDirectorySize); + paths.at(i).c_str() + Config::the().workingDirectorySize()); } return; } @@ -342,7 +340,7 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path) void Dotfile::forEachDotfile(const std::vector& targets, const std::function& callback) { size_t index = 0; - for (const auto& path : std::filesystem::recursive_directory_iterator { m_workingDirectory }) { + for (const auto& path : std::filesystem::recursive_directory_iterator { Config::the().workingDirectory() }) { if (path.is_directory() || filter(path)) { continue; } @@ -357,12 +355,12 @@ bool Dotfile::filter(const std::filesystem::path& path) { for (auto& excludePath : m_excludePaths) { if (excludePath.type == ExcludeType::File) { - if (path.string() == m_workingDirectory / excludePath.path) { + if (path.string() == Config::the().workingDirectory() / excludePath.path) { return true; } } else if (excludePath.type == ExcludeType::Directory) { - if (path.string().find(m_workingDirectory / excludePath.path) == 0) { + if (path.string().find(Config::the().workingDirectory() / excludePath.path) == 0) { return true; } } @@ -379,7 +377,7 @@ bool Dotfile::filter(const std::filesystem::path& path) bool Dotfile::include(const std::filesystem::path& path, const std::vector& targets) { for (const auto& target : targets) { - if (path.string().find(m_workingDirectory / target) == 0) { + if (path.string().find(Config::the().workingDirectory() / target) == 0) { return true; } } @@ -396,7 +394,7 @@ bool Dotfile::isSystemTarget(const std::string& target) } // 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(m_workingDirectory.string() + systemDirectory.string()) == 0) { + if (target.find(Config::the().workingDirectory().string() + systemDirectory.string()) == 0) { return true; } } diff --git a/src/dotfile.h b/src/dotfile.h index 03de1f4..dfec1f7 100644 --- a/src/dotfile.h +++ b/src/dotfile.h @@ -41,16 +41,9 @@ public: void pull(const std::vector& targets = {}); void push(const std::vector& targets = {}); - void setWorkingDirectory(std::filesystem::path directory) - { - m_workingDirectory = directory; - m_workingDirectorySize = directory.string().size(); - } void setSystemDirectories(const std::vector& systemDirectories) { m_systemDirectories = systemDirectories; } void setExcludePaths(const std::vector& excludePaths) { m_excludePaths = excludePaths; } - const std::filesystem::path& workingDirectory() const { return m_workingDirectory; } - private: void pullOrPush(SyncType type, const std::vector& targets = {}); void sync(SyncType type, @@ -66,8 +59,6 @@ private: std::vector m_excludePaths; std::vector m_systemDirectories; - std::filesystem::path m_workingDirectory; - size_t m_workingDirectorySize { 0 }; }; #endif // DOTFILE_H diff --git a/test/unit/testdotfile.cpp b/test/unit/testdotfile.cpp index 764f8ee..df8318d 100644 --- a/test/unit/testdotfile.cpp +++ b/test/unit/testdotfile.cpp @@ -10,6 +10,7 @@ #include // geteuid, setegid, seteuid #include +#include "config.h" #include "dotfile.h" #include "machine.h" #include "macro.h" @@ -329,8 +330,8 @@ TEST_CASE(AddSystemDotfiles) EXPECT(std::filesystem::exists("etc/group")); EXPECT(std::filesystem::exists("usr/lib/os-release")); - std::filesystem::remove_all(Dotfile::the().workingDirectory() / "etc"); - std::filesystem::remove_all(Dotfile::the().workingDirectory() / "usr"); + std::filesystem::remove_all(Config::the().workingDirectory() / "etc"); + std::filesystem::remove_all(Config::the().workingDirectory() / "usr"); } TEST_CASE(PullSystemDotfiles) @@ -344,8 +345,8 @@ TEST_CASE(PullSystemDotfiles) Dotfile::the().setSystemDirectories({}); Util::File lhs("/etc/group"); - Util::File rhs(Dotfile::the().workingDirectory() / "etc/group"); + Util::File rhs(Config::the().workingDirectory() / "etc/group"); EXPECT_EQ(lhs.data(), rhs.data()); - std::filesystem::remove_all(Dotfile::the().workingDirectory() / "etc"); + std::filesystem::remove_all(Config::the().workingDirectory() / "etc"); }