From 9e1330b726c5453755bcaffd1b746d5982357861 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 21 May 2018 11:48:23 +0200 Subject: Implement config-add and config-create subcommands --- bdep/config.cxx | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 115 insertions(+), 8 deletions(-) (limited to 'bdep/config.cxx') diff --git a/bdep/config.cxx b/bdep/config.cxx index 8da6211..796f534 100644 --- a/bdep/config.cxx +++ b/bdep/config.cxx @@ -157,12 +157,6 @@ namespace bdep return r; } - static int - cmd_config_add (const cmd_config_options&, cli::scanner&) - { - fail << "@@ TODO" << endf; - } - shared_ptr cmd_config_create (const common_options& co, const configuration_add_options& ao, @@ -200,9 +194,96 @@ namespace bdep } static int - cmd_config_create (const cmd_config_options&, cli::scanner&) + cmd_config_add (const cmd_config_options& o, cli::scanner& args) { - fail << "@@ TODO" << endf; + tracer trace ("config_add"); + + optional name; + optional id; + cmd_config_validate_add (o, "config add", name, id); + + string arg; + if (args.more ()) + arg = args.next (); + else if (name) + { + // Reverse into the shortcut form expected by translate_path_name(). + // + arg = '@' + *name; + name = nullopt; + } + else + fail << "configuration directory argument expected"; + + dir_path path; + try + { + path = dir_path (move (arg)); + } + catch (const invalid_path& e) + { + fail << "invalid configuration directory '" << arg << "'"; + } + + dir_path prj (find_project (o)); + database db (open (prj, trace)); + + cmd_config_add (o, + prj, + db, + move (path), + move (name), + move (id)); + return 0; + } + + static int + cmd_config_create (const cmd_config_options& o, cli::scanner& args) + { + tracer trace ("config_create"); + + optional name; + optional id; + cmd_config_validate_add (o, "config create", name, id); + + // Note that the shortcut will only work if there are no cfg-args which + // is not very likely. Oh, well. + // + string arg; + if (args.more ()) + arg = args.next (); + else if (name) + { + // Reverse into the shortcut form expected by translate_path_name(). + // + arg = '@' + *name; + name = nullopt; + } + else + fail << "configuration directory argument expected"; + + dir_path path; + try + { + path = dir_path (move (arg)); + } + catch (const invalid_path& e) + { + fail << "invalid configuration directory '" << arg << "'"; + } + + dir_path prj (find_project (o)); + database db (open (prj, trace)); + + cmd_config_create (o, + o, + prj, + db, + move (path), + args, + move (name), + move (id)); + return 0; } static int @@ -250,6 +331,32 @@ namespace bdep o.wipe () ? "--wipe" : nullptr); } + void + cmd_config_validate_add (const configuration_name_options& o, + const char* what, + optional& name, + optional id) + { + name = nullopt; + id = nullopt; + + if (size_t n = o.config_name ().size ()) + { + if (n > 1) + fail << "multiple configuration names specified for " << what; + + name = o.config_name ()[0]; + } + + if (size_t n = o.config_id ().size ()) + { + if (n > 1) + fail << "multiple configuration ids specified for " << what; + + id = o.config_id ()[0]; + } + } + int cmd_config (const cmd_config_options& o, cli::scanner& scan) { -- cgit v1.1