Browse Source

Manager: Move working directory to the config class

master
Riyyi 3 years ago
parent
commit
12c11cff9e
  1. 19
      src/config.cpp
  2. 12
      src/config.h
  3. 38
      src/dotfile.cpp
  4. 9
      src/dotfile.h
  5. 9
      test/unit/testdotfile.cpp

19
src/config.cpp

@ -0,0 +1,19 @@
/*
* Copyright (C) 2022 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include <string>
#include "config.h"
Config::Config(s)
: m_workingDirectory(std::filesystem::current_path())
, m_workingDirectorySize(m_workingDirectory.string().size())
{
}
Config::~Config()
{
}

12
src/config.h

@ -7,19 +7,27 @@
#ifndef CONFIG_H #ifndef CONFIG_H
#define CONFIG_H #define CONFIG_H
#include <cstddef> // size_t
#include <filesystem>
#include "util/singleton.h" #include "util/singleton.h"
class Config : public Util::Singleton<Config> { class Config : public Util::Singleton<Config> {
public: public:
Config(s) {} Config(s);
virtual ~Config() {} virtual ~Config();
void setVerbose(bool verbose) { m_verbose = verbose; } 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; } bool verbose() const { return m_verbose; }
private: private:
bool m_verbose { false }; bool m_verbose { false };
std::filesystem::path m_workingDirectory {};
size_t m_workingDirectorySize { 0 };
}; };
#endif // CONFIG_H #endif // CONFIG_H

38
src/dotfile.cpp

@ -21,8 +21,6 @@
#include "util/file.h" #include "util/file.h"
Dotfile::Dotfile(s) 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<std::string>& targets)
void Dotfile::list(const std::vector<std::string>& targets) void Dotfile::list(const std::vector<std::string>& targets)
{ {
if (m_workingDirectory.empty()) { if (Config::the().workingDirectory().empty()) {
fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is unset\n"); fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is unset\n");
return; 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"); fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is not a directory\n");
return; return;
} }
forEachDotfile(targets, [this](std::filesystem::directory_entry path, size_t) { forEachDotfile(targets, [](std::filesystem::directory_entry path, size_t) {
printf("%s\n", path.path().c_str() + m_workingDirectorySize + 1); printf("%s\n", path.path().c_str() + Config::the().workingDirectorySize() + 1);
}); });
} }
@ -129,33 +127,33 @@ void Dotfile::pullOrPush(SyncType type, const std::vector<std::string>& targets)
if (type == SyncType::Pull) { if (type == SyncType::Pull) {
sync( sync(
type, dotfiles, homeIndices, systemIndices, 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/<user>/dotfiles/<file> // homeFile = /home/<user>/dotfiles/<file>
// copy: /home/<user>/<file> -> /home/<user>/dotfiles/<file> // copy: /home/<user>/<file> -> /home/<user>/dotfiles/<file>
paths[0] = homeDirectory + homeFile.substr(m_workingDirectorySize); paths[0] = homeDirectory + homeFile.substr(Config::the().workingDirectorySize());
paths[1] = homeFile; paths[1] = homeFile;
}, },
[this](std::string* paths, const std::string& systemFile) { [](std::string* paths, const std::string& systemFile) {
// systemFile = /home/<user>/dotfiles/<file> // systemFile = /home/<user>/dotfiles/<file>
// copy: <file> -> /home/<user>/dotfiles/<file> // copy: <file> -> /home/<user>/dotfiles/<file>
paths[0] = systemFile.substr(m_workingDirectorySize); paths[0] = systemFile.substr(Config::the().workingDirectorySize());
paths[1] = systemFile; paths[1] = systemFile;
}); });
} }
else { else {
sync( sync(
type, dotfiles, homeIndices, systemIndices, 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/<user>/dotfiles/<file> // homeFile = /home/<user>/dotfiles/<file>
// copy: /home/<user>/dotfiles/<file> -> /home/<user>/<file> // copy: /home/<user>/dotfiles/<file> -> /home/<user>/<file>
paths[0] = homeFile; 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/<user>/dotfiles/<file> // systemFile = /home/<user>/dotfiles/<file>
// copy: /home/<user>/dotfiles/<file> -> <file> // copy: /home/<user>/dotfiles/<file> -> <file>
paths[0] = systemFile; 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) { if (!systemIndices.empty() && !root) {
for (size_t i : systemIndices) { for (size_t i : systemIndices) {
fprintf(stderr, "\033[31;1mDotfile:\033[0m need root privileges to copy system file '%s'\n", 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; return;
} }
@ -342,7 +340,7 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std::function<void(const std::filesystem::directory_entry&, size_t)>& callback) void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std::function<void(const std::filesystem::directory_entry&, size_t)>& callback)
{ {
size_t index = 0; 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)) { if (path.is_directory() || filter(path)) {
continue; continue;
} }
@ -357,12 +355,12 @@ bool Dotfile::filter(const std::filesystem::path& path)
{ {
for (auto& excludePath : m_excludePaths) { for (auto& excludePath : m_excludePaths) {
if (excludePath.type == ExcludeType::File) { if (excludePath.type == ExcludeType::File) {
if (path.string() == m_workingDirectory / excludePath.path) { if (path.string() == Config::the().workingDirectory() / excludePath.path) {
return true; return true;
} }
} }
else if (excludePath.type == ExcludeType::Directory) { 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; 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<std::string>& targets) bool Dotfile::include(const std::filesystem::path& path, const std::vector<std::string>& targets)
{ {
for (const auto& target : 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; 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, // FIXME: The second filesystem::path cant have a "/" as the first character,
// as it will think the path is at the root. // 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; return true;
} }
} }

9
src/dotfile.h

@ -41,16 +41,9 @@ public:
void pull(const std::vector<std::string>& targets = {}); void pull(const std::vector<std::string>& targets = {});
void push(const std::vector<std::string>& targets = {}); void push(const std::vector<std::string>& targets = {});
void setWorkingDirectory(std::filesystem::path directory)
{
m_workingDirectory = directory;
m_workingDirectorySize = directory.string().size();
}
void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { m_systemDirectories = systemDirectories; } void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { m_systemDirectories = systemDirectories; }
void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { m_excludePaths = excludePaths; } void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { m_excludePaths = excludePaths; }
const std::filesystem::path& workingDirectory() const { return m_workingDirectory; }
private: private:
void pullOrPush(SyncType type, const std::vector<std::string>& targets = {}); void pullOrPush(SyncType type, const std::vector<std::string>& targets = {});
void sync(SyncType type, void sync(SyncType type,
@ -66,8 +59,6 @@ private:
std::vector<ExcludePath> m_excludePaths; std::vector<ExcludePath> m_excludePaths;
std::vector<std::filesystem::path> m_systemDirectories; std::vector<std::filesystem::path> m_systemDirectories;
std::filesystem::path m_workingDirectory;
size_t m_workingDirectorySize { 0 };
}; };
#endif // DOTFILE_H #endif // DOTFILE_H

9
test/unit/testdotfile.cpp

@ -10,6 +10,7 @@
#include <unistd.h> // geteuid, setegid, seteuid #include <unistd.h> // geteuid, setegid, seteuid
#include <vector> #include <vector>
#include "config.h"
#include "dotfile.h" #include "dotfile.h"
#include "machine.h" #include "machine.h"
#include "macro.h" #include "macro.h"
@ -329,8 +330,8 @@ TEST_CASE(AddSystemDotfiles)
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"));
std::filesystem::remove_all(Dotfile::the().workingDirectory() / "etc"); std::filesystem::remove_all(Config::the().workingDirectory() / "etc");
std::filesystem::remove_all(Dotfile::the().workingDirectory() / "usr"); std::filesystem::remove_all(Config::the().workingDirectory() / "usr");
} }
TEST_CASE(PullSystemDotfiles) TEST_CASE(PullSystemDotfiles)
@ -344,8 +345,8 @@ TEST_CASE(PullSystemDotfiles)
Dotfile::the().setSystemDirectories({}); Dotfile::the().setSystemDirectories({});
Util::File lhs("/etc/group"); 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()); EXPECT_EQ(lhs.data(), rhs.data());
std::filesystem::remove_all(Dotfile::the().workingDirectory() / "etc"); std::filesystem::remove_all(Config::the().workingDirectory() / "etc");
} }

Loading…
Cancel
Save