From 713f9a11ddba1acb02d5ad3a619543af5f842b4b Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Tue, 22 Feb 2011 09:00:21 -0800 Subject: [PATCH] cconf: remove second argument to cconf --lookup Everyone uses get_conf to get configuration values. So the logic for defaulting to some value if we can't find the requested key should live there. Also fix a case in cconf where we could encounter a usage error and keep on going. Signed-off-by: Colin McCabe --- src/cconf.cc | 81 ++++++++++------------------------------------ src/ceph_common.sh | 4 +-- 2 files changed, 19 insertions(+), 66 deletions(-) diff --git a/src/cconf.cc b/src/cconf.cc index 24935de5dbad3..cb25aadb67134 100644 --- a/src/cconf.cc +++ b/src/cconf.cc @@ -37,11 +37,9 @@ cconf \n\ \n\ ACTIONS\n\ -l|--list-sections List sections in prefix\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\ + --lookup Print a configuration setting to stdout.\n\ + Returns 0 (success) if the configuration setting is\n\ + found; 1 otherwise.\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\ @@ -62,11 +60,6 @@ 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); } @@ -96,9 +89,8 @@ static void print_val(const char *val, bool resolve_search) } } -static int lookup_impl(const deque §ions, - const char *key, const char *defval, - bool resolve_search) +static int lookup(const deque §ions, + const char *key, bool resolve_search) { char *val = NULL; if (!g_conf.cf) @@ -119,49 +111,6 @@ static int lookup_impl(const deque §ions, } } - if (defval) { - val = conf_post_process_val(defval); - if (val) { - print_val(val, resolve_search); - free(val); - return 0; - } - } - - { - // TODO: document exactly what we are doing here? - std::vector empty_args; - bool force_fg_logging = false; - parse_startup_config_options(empty_args, type, - STARTUP_FLAG_FORCE_FG_LOGGING, &force_fg_logging); - char buf[1024]; - memset(buf, 0, sizeof(buf)); - if (ceph_def_conf_by_name(key, buf, sizeof(buf))) { - print_val(buf, resolve_search); - return 0; - } - } - - return 1; -} - -static int lookup(const deque §ions, - 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, resolve_search); - const char *defval = *n; - ++n; - if (n == nargs.end()) - return lookup_impl(sections, key, defval, resolve_search); - - cerr << "lookup: Too many arguments. Expected only 1 or 2." - << std::endl; - error_exit(); return 1; } @@ -213,18 +162,22 @@ int main(int argc, const char **argv) if (do_list) { if (nargs.size() != 1) - error_exit(); + usage(); return list_sections(nargs[0]); } else if (do_lookup) { - if (nargs.size() < 1 || nargs.size() > 2) - error_exit(); - vector::const_iterator n = nargs.begin(); - return lookup(sections, nargs, n, resolve_search); + if (nargs.size() != 1) { + cerr << "lookup: expected exactly one argument" << std::endl; + usage(); + } + return lookup(sections, nargs[0], resolve_search); } else if ((nargs.size() >= 1) && (nargs[0][0] == '-')) { cerr << "Parse error at argument: " << nargs[0] << std::endl; - error_exit(); + usage(); } else { - vector::const_iterator n = nargs.begin(); - return lookup(sections, nargs, n, resolve_search); + if (nargs.size() != 1) { + cerr << "lookup: expected exactly one argument" << std::endl; + usage(); + } + return lookup(sections, nargs[0], resolve_search); } } diff --git a/src/ceph_common.sh b/src/ceph_common.sh index f8567f8b269ce..99c6e8f54560d 100644 --- a/src/ceph_common.sh +++ b/src/ceph_common.sh @@ -146,8 +146,8 @@ get_conf() { key=$3 shift; shift; shift - [ $verbose -eq 1 ] && echo "$CCONF -c $conf -i $id -t $type $tmp \"$key\" \"$def\"" - eval "$var=\"`$CCONF -c $conf -i $id -t $type $tmp \"$key\" \"$def\"`\"" + [ "$verbose" -eq 1 ] && echo "$CCONF -c $conf -i $id -t $type \"$key\"" + eval "$var=\"`$CCONF -c $conf -i $id -t $type \"$key\" || eval echo -n \"$def\"`\"" } get_conf_bool() { -- 2.39.5