Add helpers::get_config_file_path().
And use it everywhere.
This commit is contained in:
parent
6d3d5b94ad
commit
27e92a2c4b
22
helpers.cpp
22
helpers.cpp
@ -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
|
||||
|
@ -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
|
||||
|
15
statusip.cpp
15
statusip.cpp
@ -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();
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user