diff --git a/src/dotfile.cpp b/src/dotfile.cpp index bdf6aed..56077e2 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -100,38 +100,17 @@ void Dotfile::list(const std::vector& targets) void Dotfile::pull(const std::vector& targets) { - std::vector dotfiles; - std::vector homeIndices; - std::vector systemIndices; - - // 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())) { - systemIndices.push_back(index); - } - else { - homeIndices.push_back(index); - } - }); - - sync( - dotfiles, homeIndices, systemIndices, - [](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { - // homeFile = /home//dotfiles/ - // copy: /home// -> /home//dotfiles/ - paths[0] = homeDirectory + homeFile.substr(s_workingDirectorySize); - paths[1] = homeFile; - }, - [](std::string* paths, const std::string& systemFile) { - // systemFile = /home//dotfiles/ - // copy: -> /home//dotfiles/ - paths[0] = systemFile.substr(s_workingDirectorySize); - paths[1] = systemFile; - }); + pullOrPush(SyncType::Pull, targets); } void Dotfile::push(const std::vector& targets) +{ + pullOrPush(SyncType::Push, targets); +} + +// ----------------------------------------- + +void Dotfile::pullOrPush(SyncType type, const std::vector& targets) { std::vector dotfiles; std::vector homeIndices; @@ -148,24 +127,40 @@ void Dotfile::push(const std::vector& targets) } }); - sync( - dotfiles, homeIndices, systemIndices, - [](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(s_workingDirectorySize); - }, - [](std::string* paths, const std::string& systemFile) { - // systemFile = /home//dotfiles/ - // copy: /home//dotfiles/ -> - paths[0] = systemFile; - paths[1] = systemFile.substr(s_workingDirectorySize); - }); + if (type == SyncType::Pull) { + sync( + dotfiles, homeIndices, systemIndices, + [](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { + // homeFile = /home//dotfiles/ + // copy: /home// -> /home//dotfiles/ + paths[0] = homeDirectory + homeFile.substr(s_workingDirectorySize); + paths[1] = homeFile; + }, + [](std::string* paths, const std::string& systemFile) { + // systemFile = /home//dotfiles/ + // copy: -> /home//dotfiles/ + paths[0] = systemFile.substr(s_workingDirectorySize); + paths[1] = systemFile; + }); + } + else { + sync( + dotfiles, homeIndices, systemIndices, + [](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(s_workingDirectorySize); + }, + [](std::string* paths, const std::string& systemFile) { + // systemFile = /home//dotfiles/ + // copy: /home//dotfiles/ -> + paths[0] = systemFile; + paths[1] = systemFile.substr(s_workingDirectorySize); + }); + } } -// ----------------------------------------- - void Dotfile::sync(const std::vector& paths, const std::vector& homeIndices, const std::vector& systemIndices, const std::function& generateHomePaths, const std::function& generateSystemPaths) diff --git a/src/dotfile.h b/src/dotfile.h index 2f73276..042c696 100644 --- a/src/dotfile.h +++ b/src/dotfile.h @@ -18,6 +18,11 @@ public: Dotfile(); virtual ~Dotfile(); + enum class SyncType { + Pull, + Push, + }; + enum class ExcludeType { File, Directory, @@ -43,9 +48,11 @@ public: static void setExcludePaths(const std::vector& excludePaths) { s_excludePaths = excludePaths; } private: + void pullOrPush(SyncType type, const std::vector& targets = {}); void sync(const std::vector& paths, const std::vector& homeIndices, const std::vector& systemIndices, const std::function& generateHomePaths, const std::function& generateSystemPaths); + void forEachDotfile(const std::vector& targets, const std::function& callback); bool filter(const std::filesystem::path& path); bool include(const std::filesystem::path& path, const std::vector& targets);