This commit is contained in:
@ -7,86 +7,85 @@
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace dropshell {
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
namespace localfile
|
||||
{
|
||||
|
||||
namespace localfile {
|
||||
|
||||
std::string dropshell_json() {
|
||||
// Try ~/.config/dropshell/dropshell.json
|
||||
std::string homedir = localpath::current_user_home();
|
||||
if (!homedir.empty()) {
|
||||
fs::path user_path = fs::path(homedir) / ".config" / "dropshell" / filenames::dropshell_json;
|
||||
return user_path.string();
|
||||
std::string dropshell_json()
|
||||
{
|
||||
return localpath::dropshell_dir() + "/" + filenames::dropshell_json;
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::string server_json(const std::string &server_name) {
|
||||
std::string serverpath = localpath::server(server_name);
|
||||
return (serverpath.empty() ? "" : (fs::path(serverpath) / filenames::server_json).string());
|
||||
}
|
||||
std::string server_json(const std::string &server_name)
|
||||
{
|
||||
std::string serverpath = localpath::server(server_name);
|
||||
return (serverpath.empty() ? "" : (fs::path(serverpath) / filenames::server_json).string());
|
||||
}
|
||||
|
||||
std::string service_env(const std::string &server_name, const std::string &service_name) {
|
||||
std::string servicepath = localpath::service(server_name, service_name);
|
||||
return (servicepath.empty() ? "" : (fs::path(servicepath) / filenames::service_env).string());
|
||||
}
|
||||
std::string service_env(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::string servicepath = localpath::service(server_name, service_name);
|
||||
return (servicepath.empty() ? "" : (fs::path(servicepath) / filenames::service_env).string());
|
||||
}
|
||||
|
||||
std::string template_info_env(const std::string &server_name, const std::string &service_name)
|
||||
std::string template_info_env(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::string servicepath = localpath::service(server_name, service_name);
|
||||
return (servicepath.empty() ? "" : (fs::path(servicepath) / filenames::template_info_env).string());
|
||||
}
|
||||
|
||||
std::string template_example()
|
||||
{
|
||||
return localpath::agent_local() + "/template_example";
|
||||
}
|
||||
|
||||
std::string bb64()
|
||||
{
|
||||
return localpath::agent_local() + "/bb64";
|
||||
}
|
||||
|
||||
} // namespace localfile
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
|
||||
namespace localpath
|
||||
{
|
||||
std::string servicepath = localpath::service(server_name, service_name);
|
||||
return (servicepath.empty() ? "" : (fs::path(servicepath) / filenames::template_info_env).string());
|
||||
}
|
||||
|
||||
std::string template_example()
|
||||
{
|
||||
return localpath::agent_local() + "/template_example";
|
||||
}
|
||||
std::string dropshell_dir()
|
||||
{
|
||||
return current_user_home() + "/.dropshell";
|
||||
}
|
||||
|
||||
std::string bb64()
|
||||
{
|
||||
return localpath::agent_local() + "/bb64";
|
||||
}
|
||||
|
||||
} // namespace localfile
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
|
||||
namespace localpath {
|
||||
std::string server(const std::string &server_name) {
|
||||
for (std::filesystem::path dir : gConfig().get_local_server_definition_paths())
|
||||
std::string server(const std::string &server_name)
|
||||
{
|
||||
for (std::filesystem::path dir : gConfig().get_local_server_definition_paths())
|
||||
if (fs::exists(dir / server_name))
|
||||
return dir / server_name;
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string service(const std::string &server_name, const std::string &service_name) {
|
||||
std::string service(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::string serverpath = localpath::server(server_name);
|
||||
return ((serverpath.empty() || service_name.empty()) ? "" : (serverpath+"/"+service_name));
|
||||
return ((serverpath.empty() || service_name.empty()) ? "" : (serverpath + "/" + service_name));
|
||||
}
|
||||
|
||||
std::string remote_versions(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::string template_cache_path = localpath::template_cache();
|
||||
return ((template_cache_path.empty() || service_name.empty()) ? "" :
|
||||
(template_cache_path+"/remote_versions/"+service_name+".json"));
|
||||
}
|
||||
std::string agent_local()
|
||||
{
|
||||
return current_user_home()+"/.local/dropshell_agent/agent-local";
|
||||
return dropshell_dir() + "/agent-local";
|
||||
}
|
||||
std::string agent_remote()
|
||||
{
|
||||
return current_user_home() + "/.local/dropshell_agent/agent-remote";
|
||||
return dropshell_dir() + "/agent-remote";
|
||||
}
|
||||
std::string current_user_home()
|
||||
{
|
||||
char * homedir = std::getenv("HOME");
|
||||
char *homedir = std::getenv("HOME");
|
||||
if (homedir)
|
||||
{
|
||||
std::filesystem::path homedir_path(homedir);
|
||||
@ -96,37 +95,32 @@ namespace localpath {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::string dropshell_files()
|
||||
{
|
||||
return current_user_home() + "/.local/dropshell_files";
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::string backups()
|
||||
{
|
||||
return dropshell_files() + "/backups";
|
||||
if (!gConfig().is_config_set())
|
||||
return "";
|
||||
return gConfig().get_backups_path();
|
||||
}
|
||||
|
||||
std::string temp_files()
|
||||
{
|
||||
return dropshell_files() + "/temp_files";
|
||||
return dropshell_dir() + "/temp_files";
|
||||
}
|
||||
|
||||
std::string template_cache()
|
||||
{
|
||||
return dropshell_files() + "template_cache";
|
||||
return dropshell_dir() + "/template_cache";
|
||||
}
|
||||
|
||||
bool create_directories()
|
||||
{
|
||||
std::vector<std::filesystem::path> paths = {
|
||||
dropshell_files(),
|
||||
dropshell_dir(),
|
||||
agent_local(),
|
||||
agent_remote(),
|
||||
template_cache(),
|
||||
backups(),
|
||||
temp_files()
|
||||
};
|
||||
temp_files()};
|
||||
for (auto &p : gConfig().get_local_server_definition_paths())
|
||||
paths.push_back(p);
|
||||
|
||||
@ -139,7 +133,7 @@ namespace localpath {
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace localpath
|
||||
} // namespace localpath
|
||||
|
||||
//------------------------------------------------------------------------------------------------
|
||||
// remote paths
|
||||
@ -159,21 +153,26 @@ namespace localpath {
|
||||
// |-- service.env (default service config)
|
||||
// |-- (other config files for specific server&service)
|
||||
|
||||
|
||||
remotefile::remotefile(const std::string &server_name, const std::string &user) :
|
||||
mServer_name(server_name), mUser(user) {}
|
||||
remotefile::remotefile(const std::string &server_name, const std::string &user) : mServer_name(server_name), mUser(user) {}
|
||||
|
||||
std::string remotefile::service_env(const std::string &service_name) const
|
||||
{
|
||||
return remotepath(mServer_name,mUser).service_config(service_name) + "/" + filenames::service_env;
|
||||
return remotepath(mServer_name, mUser).service_config(service_name) + "/" + filenames::service_env;
|
||||
}
|
||||
|
||||
|
||||
remotepath::remotepath(const std::string &server_name, const std::string &user) : mServer_name(server_name), mUser(user) {}
|
||||
|
||||
std::string remotepath::DROPSHELL_DIR() const
|
||||
{
|
||||
return ServerConfig(mServer_name).get_user_dir(mUser);
|
||||
{
|
||||
try
|
||||
{
|
||||
return ServerConfig(mServer_name).get_user_dir(mUser);
|
||||
} catch (const std::exception &e)
|
||||
{
|
||||
error << "Failed to get remote dropshell directory for " << mServer_name << "@" << mUser << std::endl;
|
||||
error << "Exception: " << e.what() << std::endl;
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
std::string remotepath::services() const
|
||||
@ -218,22 +217,21 @@ namespace localpath {
|
||||
return (dsp.empty() ? "" : (dsp + "/agent"));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
// Utility functions
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
// Utility functions
|
||||
std::string get_parent(const std::filesystem::path path)
|
||||
{
|
||||
if (path.empty())
|
||||
return std::string();
|
||||
return path.parent_path().string();
|
||||
}
|
||||
|
||||
std::string get_parent(const std::filesystem::path path)
|
||||
{
|
||||
if (path.empty())
|
||||
return std::string();
|
||||
return path.parent_path().string();
|
||||
}
|
||||
|
||||
std::string get_child(const std::filesystem::path path)
|
||||
{
|
||||
if (path.empty())
|
||||
return std::string();
|
||||
return path.filename().string();
|
||||
}
|
||||
std::string get_child(const std::filesystem::path path)
|
||||
{
|
||||
if (path.empty())
|
||||
return std::string();
|
||||
return path.filename().string();
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
||||
|
Reference in New Issue
Block a user