#include "cmd.h" #include "utils.h" #include "entry.h" #include #include namespace fs = std::filesystem; namespace cmd { std::string list(const fs::path& p) { std::stringstream text; if(p.empty()) { // Print read-only dirs for(std::string name : getVirtDirs()) { text << name << "/ (read only)" << std::endl; } } fs::path truePath = getTruePath(p); if(fs::directory_entry(truePath).is_regular_file()) { text << utils::instantiate(truePath)->getText() << std::endl; } else if(fs::directory_entry(truePath).is_directory()) { for(fs::directory_entry de : fs::directory_iterator(truePath)) { if(de.path().filename().string()[0] != '.') { if(de.is_directory()) { text << de.path().filename().string() << "/" << std::endl; } else { text << de.path().stem().string() << std::endl; } } } } else { text << "Unknown path " << p << std::endl; } return text.str(); } std::string list(std::vector args) { std::stringstream text; if(args.empty()) { text << list(""); } else { for(std::string dir : args) { text << list(dir); } } return text.str(); } std::string mkdir(std::vector args) { for(std::string s : args) { fs::create_directories(getTruePath(s)); } return ""; } void cp(fs::path src, fs::path dest) { if(fs::directory_entry(src).is_regular_file()) { utils::saveJson(utils::instantiate(src)->serialize(), dest); } else { mkdir({dest}); for(fs::directory_entry de : fs::directory_iterator(src)) { cp(de.path(), dest / de.path().filename()); } } } std::string cp(std::vector args) { // Operate by intantiating and saving // We do recursive! cp(getTruePath(args[0]), getTruePath(args[1])); return ""; } std::string mv(std::vector args) { fs::rename(getTruePath(args[0]), getTruePath(args[1])); return ""; } std::string rm(std::vector args) { for(std::string s : args) { fs::remove_all(getTruePath(s)); } return ""; } }