From: Yehuda Sadeh Date: Fri, 14 Jan 2011 00:09:16 +0000 (-0800) Subject: cconf: fix usage parsing, add --resolve search X-Git-Tag: v0.25~321 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=ef8a60fd8577091352f4d9d0893d659ca7e28e58;p=ceph.git cconf: fix usage parsing, add --resolve search --resolve-search is used to resolve a search path result --- diff --git a/src/cconf.cc b/src/cconf.cc index ed495b0d5993..aee5fd904f3a 100644 --- a/src/cconf.cc +++ b/src/cconf.cc @@ -36,12 +36,14 @@ cconf \n\ \n\ ACTIONS\n\ -l|--list-sections List sections in prefix\n\ -\n\ --lookup [defval] Print a configuration setting to stdout.\n\ If the setting is not defined, and the\n\ optional argument defval is provide, it will\n\ be printed instead. variables in defval are\n\ interpolated.\n\ + -r|--resolve-search search for the first file that exists and\n\ + can be opened in the resulted comma\n\ + delimited search list.\n\ \n\ FLAGS\n\ -i id Set id\n\ @@ -59,7 +61,11 @@ List sections beginning with 'mon'.\n\ RETURN CODE\n\ Return code will be 0 on success; error code otherwise.\n\ "; +} +void error_exit() +{ + usage(); exit(1); } @@ -78,26 +84,37 @@ static int list_sections(const char *s) return 0; } +static void print_val(const char *val, bool resolve_search) +{ + if (!resolve_search) { + puts(val); + } else { + string search_path(val); + string result; + if (ceph_resolve_file_search(search_path, result)) + puts(result.c_str()); + } +} + static int lookup_impl(const deque §ions, - const char *key, const char *defval) + const char *key, const char *defval, + bool resolve_search) { char *val = NULL; ConfFile *cf = conf_get_conf_file(); if (!cf) return 2; conf_read_key(NULL, key, OPT_STR, (char **)&val, NULL); - if (val) { - puts(val); + print_val(val, resolve_search); free(val); return 0; } for (unsigned int i=0; iread(sections[i], key, (char **)&val, NULL); - if (val) { - puts(val); + print_val(val, resolve_search); free(val); return 0; } @@ -106,7 +123,7 @@ static int lookup_impl(const deque §ions, if (defval) { val = conf_post_process_val(defval); if (val) { - puts(val); + print_val(val, resolve_search); free(val); return 0; } @@ -119,7 +136,7 @@ static int lookup_impl(const deque §ions, char buf[1024]; memset(buf, 0, sizeof(buf)); if (ceph_def_conf_by_name(key, buf, sizeof(buf))) { - cout << buf << std::endl; + print_val(buf, resolve_search); return 0; } } @@ -128,21 +145,22 @@ static int lookup_impl(const deque §ions, } static int lookup(const deque §ions, - const vector &nargs, - vector::const_iterator n) + const vector &nargs, + vector::const_iterator n, + bool resolve_search) { const char *key = *n; ++n; if (n == nargs.end()) - return lookup_impl(sections, key, NULL); + return lookup_impl(sections, key, NULL, resolve_search); const char *defval = *n; ++n; if (n == nargs.end()) - return lookup_impl(sections, key, defval); + return lookup_impl(sections, key, defval, resolve_search); cerr << "lookup: Too many arguments. Expected only 1 or 2." << std::endl; - usage(); + error_exit(); return 1; } @@ -152,6 +170,10 @@ int main(int argc, const char **argv) vector args, nargs; deque sections; DEFINE_CONF_VARS(usage); + bool resolve_search = false; + bool do_help = false; + bool do_list = false; + bool do_lookup = false; argv_to_vec(argc, argv, args); env_to_vec(args); @@ -164,6 +186,14 @@ int main(int argc, const char **argv) } else if (CONF_ARG_EQ("section", 's')) { CONF_SAFE_SET_ARG_VAL(§ion, OPT_STR); sections.push_back(section); + } else if (CONF_ARG_EQ("resolve-search", 'r')) { + CONF_SAFE_SET_ARG_VAL(&resolve_search, OPT_BOOL); + } else if (CONF_ARG_EQ("help", 'h')) { + CONF_SAFE_SET_ARG_VAL(&do_help, OPT_BOOL); + } else if (CONF_ARG_EQ("list-sections", 'l')) { + CONF_SAFE_SET_ARG_VAL(&do_list, OPT_BOOL); + } else if (CONF_ARG_EQ("lookup", '\0')) { + CONF_SAFE_SET_ARG_VAL(&do_lookup, OPT_BOOL); } else { nargs.push_back(args[i]); @@ -174,24 +204,27 @@ int main(int argc, const char **argv) common_init(nargs, type, false); set_foreground_logging(); - if ((nargs.size() == 1) && (!strcmp(nargs[0], "-h"))) { + if (do_help) { usage(); + exit(0); } - else if ((nargs.size() == 2) && - (!strcmp(nargs[0], "--list_sections") || !strcmp(nargs[0], "-l"))) { - return list_sections(nargs[1]); - } - else if ((nargs.size() >= 2) && (!strcmp(nargs[0], "--lookup"))) { + if (!do_lookup && !do_list) + do_lookup = true; + + if (do_list) { + if (nargs.size() != 1) + error_exit(); + return list_sections(nargs[0]); + } else if (do_lookup) { + if (nargs.size() < 1 || nargs.size() > 2) + error_exit(); vector::const_iterator n = nargs.begin(); - ++n; - return lookup(sections, nargs, n); - } - else if ((nargs.size() >= 1) && (nargs[0][0] == '-')) { + return lookup(sections, nargs, n, resolve_search); + } else if ((nargs.size() >= 1) && (nargs[0][0] == '-')) { cerr << "Parse error at argument: " << nargs[0] << std::endl; - usage(); - } - else { + error_exit(); + } else { vector::const_iterator n = nargs.begin(); - return lookup(sections, nargs, n); + return lookup(sections, nargs, n, resolve_search); } }