Skip to content

Commit

Permalink
[dnf5 plugin] config-manager: Support for creating missing directories
Browse files Browse the repository at this point in the history
New option:
--create-missing-dir - Allow to create missing directories

Without this option, an exception is thrown if the directory is missing.
  • Loading branch information
jrohel committed Nov 2, 2023
1 parent eaba0ad commit e0e3734
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 1 deletion.
12 changes: 11 additions & 1 deletion dnf5-plugins/config-manager_plugin/addrepo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ void ConfigManagerAddRepoCommand::set_argument_parser() {
});
cmd.register_named_arg(set_opt);

auto create_missing_dirs_opt = parser.add_new_named_arg("create-missing-dir");
create_missing_dirs_opt->set_long_name("create-missing-dir");
create_missing_dirs_opt->set_description("Allow creation of missing directories");
create_missing_dirs_opt->set_has_value(false);
create_missing_dirs_opt->set_parse_hook_func([this](cli::ArgumentParser::NamedArg *, const char *, const char *) {
create_missing_dirs = true;
return true;
});
cmd.register_named_arg(create_missing_dirs_opt);

auto overwrite_opt = parser.add_new_named_arg("overwrite");
overwrite_opt->set_long_name("overwrite");
overwrite_opt->set_description("Allow overwriting of existing repository configuration file");
Expand Down Expand Up @@ -248,8 +258,8 @@ void ConfigManagerAddRepoCommand::configure() {
if (repo_dirs.empty()) {
throw ConfigManagerError(M_("Missing path to repository configuration directory"));
}

std::filesystem::path dest_repo_dir = repo_dirs.front();
resolve_missing_dir(dest_repo_dir, create_missing_dirs);

if (source_repofile.location.empty()) {
create_repo(repo_id, repo_opts, dest_repo_dir);
Expand Down
1 change: 1 addition & 0 deletions dnf5-plugins/config-manager_plugin/addrepo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ConfigManagerAddRepoCommand : public Command {

SourceRepofile source_repofile; // Location of source repository configuration file.
std::string repo_id; // The user-defined ID of the newly created repository.
bool create_missing_dirs{false}; // Allows to create missing directories.
bool overwrite{false}; // Allows to overwrite an existing configuration file.
std::string save_filename; // User-defined name of newly saved configuration file.
std::map<std::string, std::string> repo_opts; // Options for the new repository.
Expand Down
13 changes: 13 additions & 0 deletions dnf5-plugins/config-manager_plugin/setopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ void ConfigManagerSetOptCommand::set_argument_parser() {
return true;
});
cmd.register_positional_arg(opts_vals);

auto create_missing_dirs_opt = parser.add_new_named_arg("create-missing-dir");
create_missing_dirs_opt->set_long_name("create-missing-dir");
create_missing_dirs_opt->set_description("Allow to create missing directories");
create_missing_dirs_opt->set_has_value(false);
create_missing_dirs_opt->set_parse_hook_func([this](cli::ArgumentParser::NamedArg *, const char *, const char *) {
create_missing_dirs = true;
return true;
});
cmd.register_named_arg(create_missing_dirs_opt);
}


Expand Down Expand Up @@ -170,6 +180,7 @@ void ConfigManagerSetOptCommand::configure() {
ConfigParser parser;

const auto & cfg_filepath = get_config_file_path(ctx.base.get_config());
resolve_missing_dir(cfg_filepath.parent_path(), create_missing_dirs);

const bool exists = std::filesystem::exists(cfg_filepath);
if (exists) {
Expand All @@ -187,6 +198,8 @@ void ConfigManagerSetOptCommand::configure() {
if (!matching_repos_setopts.empty()) {
ConfigParser parser;

resolve_missing_dir(libdnf5::REPOS_OVERRIDE_DIR, create_missing_dirs);

const bool exists = std::filesystem::exists(CFG_MANAGER_REPOS_OVERRIDE_FILEPATH);
if (exists) {
parser.read(CFG_MANAGER_REPOS_OVERRIDE_FILEPATH);
Expand Down
1 change: 1 addition & 0 deletions dnf5-plugins/config-manager_plugin/setopt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ConfigManagerSetOptCommand : public Command {
std::map<std::string, std::string> main_setopts;
std::map<std::string, std::map<std::string, std::string>> in_repos_setopts;
std::map<std::string, std::map<std::string, std::string>> matching_repos_setopts;
bool create_missing_dirs{false}; // Allows to create missing directories.
};

} // namespace dnf5
Expand Down
11 changes: 11 additions & 0 deletions dnf5-plugins/config-manager_plugin/setvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ void ConfigManagerSetVarCommand::set_argument_parser() {
return true;
});
cmd.register_positional_arg(vars_vals);

auto create_missing_dirs_opt = parser.add_new_named_arg("create-missing-dir");
create_missing_dirs_opt->set_long_name("create-missing-dir");
create_missing_dirs_opt->set_description("Allow to create missing directories");
create_missing_dirs_opt->set_has_value(false);
create_missing_dirs_opt->set_parse_hook_func([this](cli::ArgumentParser::NamedArg *, const char *, const char *) {
create_missing_dirs = true;
return true;
});
cmd.register_named_arg(create_missing_dirs_opt);
}


Expand All @@ -86,6 +96,7 @@ void ConfigManagerSetVarCommand::configure() {
if (vars_dir.empty()) {
throw ConfigManagerError(M_("Missing path to vars directory"));
}
resolve_missing_dir(vars_dir, create_missing_dirs);

for (const auto & [name, value] : setvars) {
const auto filepath = vars_dir / name;
Expand Down
1 change: 1 addition & 0 deletions dnf5-plugins/config-manager_plugin/setvar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ConfigManagerSetVarCommand : public Command {

private:
std::map<std::string, std::string> setvars;
bool create_missing_dirs{false}; // Allows to create missing directories.
};

} // namespace dnf5
Expand Down
14 changes: 14 additions & 0 deletions dnf5-plugins/config-manager_plugin/shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ struct ConfigManagerError : public libdnf5::Error {
const char * get_name() const noexcept override { return "ConfigManagerError"; }
};

// Checks if the `path` directory exists. If not, according to the `create_missing_dirs` argument,
// the directories (missing paths elements) are created or `ConfigManagerError` exception is thrown.
inline void resolve_missing_dir(const std::filesystem::path & path, bool create_missing_dirs) {
if (!std::filesystem::exists(path)) {
if (create_missing_dirs) {
std::filesystem::create_directories(path);
} else {
throw ConfigManagerError(
M_("Directory \"{}\" does not exist. Add \"--create-missing-dir\" to create missing directories."),
path.string());
}
}
}

inline void check_variable_name(const std::string & name) {
if (name.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
Expand Down

0 comments on commit e0e3734

Please sign in to comment.