Compare commits

...
8 Commits
13 changed files with 220 additions and 91 deletions
+3
View File
@@ -0,0 +1,3 @@
[submodule "vendor/json"]
path = vendor/json
url = https://github.com/nlohmann/json
+1
View File
@@ -56,6 +56,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include_directories(
"src"
"test"
"vendor/json/include"
)
# Define source files
+5
View File
@@ -18,3 +18,8 @@ install(
FILES ${CMAKE_BINARY_DIR}/manafiles.1.gz
DESTINATION ${CMAKE_INSTALL_MANDIR}/man1
CONFIGURATIONS Release)
install(
FILES ${CMAKE_CURRENT_LIST_DIR}/manafiles.json
DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples
CONFIGURATIONS Release)
+14
View File
@@ -0,0 +1,14 @@
{
"excludePaths" : {
".git": "directory",
".md": "endsWith",
"packages": "file",
"README.org": "endsWith",
"screenshot.png": "file"
},
"systemDirectories": [
"/boot",
"/etc",
"/usr/share"
]
}
+100
View File
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2022 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include <csignal> // raise
#include <cstdio> // fprintf
#include <filesystem> // current_path, recursive_directory
#include <fstream> // ifstream
#include <string>
#include <vector>
#include "nlohmann/json.hpp"
#include "config.h"
Config::Config(s)
: m_workingDirectory(std::filesystem::current_path())
, m_workingDirectorySize(m_workingDirectory.string().size())
{
findConfigFile();
parseConfigFile();
}
Config::~Config()
{
}
// -----------------------------------------
void Config::findConfigFile()
{
std::string configFileName = "manafiles.json";
for (const auto& path : std::filesystem::recursive_directory_iterator { m_workingDirectory }) {
const auto& file = path.path().string();
if (file.find(configFileName) + configFileName.size() == file.size()) {
m_config = file;
}
}
#ifndef NDEBUG
printf("Found config file @ %s\n", m_config.c_str() + m_workingDirectorySize + 1);
#endif
}
void Config::parseConfigFile()
{
if (m_config.empty()) {
return;
}
nlohmann::json json;
std::ifstream file(m_config);
if (!file.is_open()) {
return;
}
try {
file >> json;
}
catch (...) {
fprintf(stderr, "\033[31;1mConfig:\033[0m json syntax error\n");
raise(SIGABRT);
return;
}
m_settings = json.get<Settings>();
}
// -----------------------------------------
void to_json(nlohmann::json& object, const Settings& settings)
{
object = nlohmann::json {
{ "excludePaths", settings.excludePaths },
{ "systemDirectories", settings.systemDirectories }
};
}
void from_json(const nlohmann::json& object, Settings& settings)
{
if (object.find("excludePaths") != object.end()) {
object.at("excludePaths").get_to(settings.excludePaths);
}
if (object.find("systemDirectories") != object.end()) {
object.at("systemDirectories").get_to(settings.systemDirectories);
}
// Check for correct exclude type values
for (const auto& path : settings.excludePaths) {
if (path.second != "file" && path.second != "directory" && path.second != "endsWith") {
fprintf(stderr, "\033[31;1mConfig:\033[0m invalid exclude type '%s'\n", path.second.c_str());
raise(SIGABRT);
}
}
}
+49 -2
View File
@@ -7,19 +7,66 @@
#ifndef CONFIG_H
#define CONFIG_H
#include <cstddef> // size_t
#include <filesystem> // path
#include <map>
#include <string>
#include <vector>
#include "nlohmann/json.hpp"
#include "util/singleton.h"
struct Settings {
std::map<std::string, std::string> excludePaths {
{ ".git", "directory" },
{ ".md", "endsWith" },
{ "packages", "file" },
{ "README.org", "endsWith" },
{ "screenshot.png", "file" },
};
std::vector<std::filesystem::path> systemDirectories {
"/boot",
"/etc",
"/usr/share"
};
};
class Config : public Util::Singleton<Config> {
public:
Config(s) {}
virtual ~Config() {}
Config(s);
virtual ~Config();
void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { m_settings.systemDirectories = systemDirectories; }
void setExcludePaths(const std::map<std::string, std::string>& excludePaths) { m_settings.excludePaths = excludePaths; }
void setVerbose(bool verbose) { m_verbose = verbose; }
const std::map<std::string, std::string>& excludePaths() const { return m_settings.excludePaths; }
const std::vector<std::filesystem::path>& systemDirectories() const { return m_settings.systemDirectories; }
const std::filesystem::path& workingDirectory() const { return m_workingDirectory; }
size_t workingDirectorySize() const { return m_workingDirectorySize; }
bool verbose() const { return m_verbose; }
private:
void findConfigFile();
void parseConfigFile();
bool m_verbose { false };
std::filesystem::path m_workingDirectory {};
size_t m_workingDirectorySize { 0 };
std::filesystem::path m_config;
Settings m_settings;
};
// -----------------------------------------
// nlohmann::json arbitrary type conversion functions
void to_json(nlohmann::json& object, const Settings& settings);
void from_json(const nlohmann::json& object, Settings& settings);
#endif // CONFIG_H
+24 -26
View File
@@ -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<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");
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<std::string>& 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/<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;
},
[this](std::string* paths, const std::string& systemFile) {
[](std::string* paths, const std::string& systemFile) {
// systemFile = /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;
});
}
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/<user>/dotfiles/<file>
// copy: /home/<user>/dotfiles/<file> -> /home/<user>/<file>
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>
// copy: /home/<user>/dotfiles/<file> -> <file>
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<std::string>& targets, const std::function<void(const std::filesystem::directory_entry&, size_t)>& 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;
}
@@ -355,19 +353,19 @@ void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std:
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) {
for (auto& excludePath : Config::the().excludePaths()) {
if (excludePath.second == "file") {
if (path.string() == Config::the().workingDirectory() / excludePath.first) {
return true;
}
}
else if (excludePath.type == ExcludeType::Directory) {
if (path.string().find(m_workingDirectory / excludePath.path) == 0) {
else if (excludePath.second == "directory") {
if (path.string().find(Config::the().workingDirectory() / excludePath.first) == 0) {
return true;
}
}
else if (excludePath.type == ExcludeType::EndsWith) {
if (path.string().find(excludePath.path) == path.string().size() - excludePath.path.size()) {
else if (excludePath.second == "endsWith") {
if (path.string().find(excludePath.first) == path.string().size() - excludePath.first.size()) {
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)
{
for (const auto& target : targets) {
if (path.string().find(m_workingDirectory / target) == 0) {
if (path.string().find(Config::the().workingDirectory() / target) == 0) {
return true;
}
}
@@ -389,14 +387,14 @@ bool Dotfile::include(const std::filesystem::path& path, const std::vector<std::
bool Dotfile::isSystemTarget(const std::string& target)
{
for (const auto& systemDirectory : m_systemDirectories) {
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(m_workingDirectory.string() + systemDirectory.string()) == 0) {
if (target.find(Config::the().workingDirectory().string() + systemDirectory.string()) == 0) {
return true;
}
}
-26
View File
@@ -25,32 +25,11 @@ public:
Push,
};
enum class ExcludeType {
File,
Directory,
EndsWith,
};
struct ExcludePath {
ExcludeType type;
std::string path;
};
void add(const std::vector<std::string>& targets = {});
void list(const std::vector<std::string>& targets = {});
void pull(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 setExcludePaths(const std::vector<ExcludePath>& excludePaths) { m_excludePaths = excludePaths; }
const std::filesystem::path& workingDirectory() const { return m_workingDirectory; }
private:
void pullOrPush(SyncType type, const std::vector<std::string>& targets = {});
void sync(SyncType type,
@@ -63,11 +42,6 @@ private:
bool filter(const std::filesystem::path& path);
bool include(const std::filesystem::path& path, const std::vector<std::string>& targets);
bool isSystemTarget(const std::string& target);
std::vector<ExcludePath> m_excludePaths;
std::vector<std::filesystem::path> m_systemDirectories;
std::filesystem::path m_workingDirectory;
size_t m_workingDirectorySize { 0 };
};
#endif // DOTFILE_H
+4 -21
View File
@@ -54,21 +54,6 @@ int main(int argc, const char* argv[])
Config::the().setVerbose(verbose);
if (fileOperation) {
Dotfile::the().setExcludePaths({
{ Dotfile::ExcludeType::File, "dotfiles.sh" },
{ Dotfile::ExcludeType::File, "packages" },
{ Dotfile::ExcludeType::EndsWith, ".md" },
{ Dotfile::ExcludeType::EndsWith, "README.org" },
{ Dotfile::ExcludeType::Directory, ".git" },
{ Dotfile::ExcludeType::File, "screenshot.png" },
{ Dotfile::ExcludeType::Directory, ".cache" },
{ Dotfile::ExcludeType::Directory, "CMakeFiles" },
{ Dotfile::ExcludeType::Directory, "cppcheck-cppcheck-build-dir" },
});
Dotfile::the().setSystemDirectories({ "/boot", "/etc", "/usr/share" });
if (addOrAur) {
Dotfile::the().add(targets);
}
@@ -83,19 +68,17 @@ int main(int argc, const char* argv[])
}
}
else if (packageOperation) {
Package package;
if (addOrAur) {
package.aurInstall();
Package::the().aurInstall();
}
if (install) {
package.install();
Package::the().install();
}
if (pushOrStore) {
package.store();
Package::the().store();
}
if (!addOrAur && !install && !pushOrStore) {
package.list(targets);
Package::the().list(targets);
}
}
else if (helpOperation) {
+1 -1
View File
@@ -20,7 +20,7 @@
#include "util/shell.h"
#include "util/system.h"
Package::Package()
Package::Package(s)
{
}
+4 -2
View File
@@ -11,9 +11,11 @@
#include <string>
#include <vector>
class Package {
#include "util/singleton.h"
class Package : public Util::Singleton<Package> {
public:
Package();
Package(s);
virtual ~Package();
enum class Distro {
+14 -13
View File
@@ -10,6 +10,7 @@
#include <unistd.h> // geteuid, setegid, seteuid
#include <vector>
#include "config.h"
#include "dotfile.h"
#include "machine.h"
#include "macro.h"
@@ -208,13 +209,13 @@ TEST_CASE(PushDotfilesWithExcludePath)
createTestDotfiles(fileNames, { "", "", "", "" });
Dotfile::the().setExcludePaths({
{ Dotfile::ExcludeType::File, "__test-file-1" },
{ Dotfile::ExcludeType::Directory, "__subdir" },
{ Dotfile::ExcludeType::EndsWith, ".test" },
Config::the().setExcludePaths({
{ "__test-file-1", "file" },
{ "__subdir", "directory" },
{ ".test", "endsWith" },
});
Dotfile::the().push(fileNames);
Dotfile::the().setExcludePaths({});
Config::the().setExcludePaths({});
for (const auto& file : fileNames) {
EXPECT(!std::filesystem::exists(homeDirectory / file));
@@ -322,15 +323,15 @@ TEST_CASE(AddSystemDotfiles)
{
VERIFY(geteuid() == 0, return);
Dotfile::the().setSystemDirectories({ "/etc", "/usr/lib" });
Config::the().setSystemDirectories({ "/etc", "/usr/lib" });
Dotfile::the().add({ "/etc/group", "/usr/lib/os-release" });
Dotfile::the().setSystemDirectories({});
Config::the().setSystemDirectories({});
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)
@@ -339,13 +340,13 @@ TEST_CASE(PullSystemDotfiles)
createTestDotfiles({ "etc/group" }, { "" }, true);
Dotfile::the().setSystemDirectories({ "/etc" });
Config::the().setSystemDirectories({ "/etc" });
Dotfile::the().pull({ "etc/group" });
Dotfile::the().setSystemDirectories({});
Config::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");
}
Vendored Submodule
+1
Submodule vendor/json added at eec79d4e8a