fix: handle missing files

This commit is contained in:
2025-12-08 13:40:07 +03:00
parent 33bca75990
commit 264fc6ce54
8 changed files with 44 additions and 28 deletions

View File

@@ -1,4 +1,5 @@
#include "config.hpp"
#include "core/utils.hpp"
#include <core/palette/color.hpp>
#include <filesystem>
@@ -23,35 +24,34 @@ void config::initialize(std::unique_ptr<clrsync::core::io::file> file)
std::filesystem::path config::get_user_config_dir()
{
auto home = expand_user("~");
#ifdef _WIN32
if (const char *appdata = std::getenv("APPDATA"))
return std::filesystem::path(appdata) / "clrsync";
else
return std::filesystem::path("C:/clrsync");
return home + "\\.config\\clrsync";
#else
if (const char *xdg = std::getenv("XDG_CONFIG_HOME"))
return std::filesystem::path(xdg) / "clrsync";
else if (const char *home = std::getenv("HOME"))
return std::filesystem::path(home) / ".config/clrsync";
else
return std::filesystem::path("/tmp/clrsync");
return home + "/.config/clrsync";
#endif
}
void config::copy_default_configs()
{
std::filesystem::path user_config = get_user_config_dir();
std::filesystem::path default_dir = CLRSYNC_DATADIR;
if (!std::filesystem::exists(user_config))
{
std::filesystem::create_directories(user_config);
std::filesystem::path default_dir = CLRSYNC_DATADIR;
std::filesystem::copy(default_dir / "config.toml", user_config / "config.toml");
std::filesystem::copy(default_dir / "templates", user_config / "templates",
std::filesystem::copy_options::recursive);
std::filesystem::copy(default_dir / "palettes", user_config / "palettes",
std::filesystem::copy_options::recursive);
return;
}
if (!std::filesystem::exists(user_config / "config.toml"))
{
std::filesystem::copy(default_dir / "config.toml", user_config / "config.toml");
}
}

View File

@@ -2,6 +2,7 @@
#define CLRSYNC_CORE_PALETTE_PALETTE_MANAGER_HPP
#include "core/utils.hpp"
#include <iostream>
#include <string>
#include <unordered_map>
@@ -19,6 +20,11 @@ template <typename FileType> class palette_manager
void load_palettes_from_directory(const std::string &directory_path)
{
auto directory_path_expanded = expand_user(directory_path);
if (!std::filesystem::exists(directory_path_expanded))
{
std::cerr << "Palettes directory does not exist\n" ;
return;
}
for (const auto &entry : std::filesystem::directory_iterator(directory_path_expanded))
{
if (entry.is_regular_file())

View File

@@ -1,8 +1,8 @@
#include "theme_template.hpp"
#include "core/utils.hpp"
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
namespace clrsync::core
{
@@ -45,6 +45,11 @@ void theme_template::set_output_path(const std::string &path)
void theme_template::load_template()
{
if (!std::filesystem::exists(m_template_path))
{
std::cerr << "Template file '" << m_template_path << "' is missing\n";
return;
}
std::ifstream input(m_template_path, std::ios::binary);
if (!input)
throw std::runtime_error("Failed to open template file: " + m_template_path);

View File

@@ -13,16 +13,12 @@ void print_color_keys()
std::string get_default_config_path()
{
auto home = expand_user("~");
#ifdef _WIN32
const char *appdata = std::getenv("APPDATA"); // "C:\Users\<User>\AppData\Roaming"
if (!appdata)
throw std::runtime_error("APPDATA environment variable not set");
return std::string(appdata) + "\\clrsync\\config.toml";
return home + "\\.config\\clrsync\\config.toml";
#else
const char *home = std::getenv("HOME");
if (!home)
throw std::runtime_error("HOME environment variable not set");
return std::string(home) + "/.config/clrsync/config.toml";
return home + "/.config/clrsync/config.toml";
#endif
}