Add helpers::get_config_file_path().

And use it everywhere.
This commit is contained in:
tastytea 2021-08-24 21:39:47 +02:00
parent 6d3d5b94ad
commit 27e92a2c4b
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 29 additions and 43 deletions

View File

@ -7,7 +7,7 @@
namespace helpers
{
std::string get_env(std::string_view name)
std::string get_env(const std::string_view name)
{
const char *env{std::getenv(name.data())}; // NOLINT(concurrency-mt-unsafe)
if (env != nullptr)
@ -18,4 +18,24 @@ std::string get_env(std::string_view name)
return {};
}
std::string get_config_file_path(const std::string_view filename)
{
auto path{helpers::get_env("XDG_CONFIG_HOME")};
if (path.empty())
{
path = helpers::get_env("HOME");
if (!path.empty())
{
path += "/.config";
}
}
if (!path.empty())
{
path += "/";
}
path += filename;
return path;
}
} // namespace helpers

View File

@ -7,8 +7,12 @@
namespace helpers
{
//! returns environment variable or empty string.
std::string get_env(std::string_view name);
//! Returns the full path for a config file.
std::string get_config_file_path(std::string_view filename);
} // namespace helpers
#endif // CPPSCRIPTS_HELPERS_HPP

View File

@ -39,20 +39,7 @@ std::string get_url(int argc, char *argv[])
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(options).run(), vm);
const auto path{[]()
{
auto path{helpers::get_env("XDG_CONFIG_HOME")};
if (path.empty())
{
path = helpers::get_env("HOME");
if (!path.empty())
{
path += "/.config";
}
}
return path += "/statusip.cfg";
}()};
std::ifstream configfile(path);
std::ifstream configfile(helpers::get_config_file_path("statusip.cfg"));
po::store(po::parse_config_file(configfile, options, true), vm);
configfile.close();

View File

@ -37,20 +37,7 @@ std::string get_file_path(int argc, char *argv[])
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(options).run(), vm);
const auto path{[]()
{
auto path{helpers::get_env("XDG_CONFIG_HOME")};
if (path.empty())
{
path = helpers::get_env("HOME");
if (!path.empty())
{
path += "/.config";
}
}
return path += "/statustemp.cfg";
}()};
std::ifstream configfile(path);
std::ifstream configfile(helpers::get_config_file_path("statustemp.cfg"));
po::store(po::parse_config_file(configfile, options, true), vm);
configfile.close();

View File

@ -88,20 +88,8 @@ std::tuple<std::string, std::string> get_options()
// clang-format on
po::variables_map vm;
const auto path{[]()
{
auto path{helpers::get_env("XDG_CONFIG_HOME")};
if (path.empty())
{
path = helpers::get_env("HOME");
if (!path.empty())
{
path += "/.config";
}
}
return path += "/statusweather.cfg";
}()};
std::ifstream configfile(path);
std::ifstream configfile(
helpers::get_config_file_path("statusweather.cfg"));
po::store(po::parse_config_file(configfile, options, true), vm);
configfile.close();