diff options
author | Peter Rajnoha <prajnoha@redhat.com> | 2022-08-16 16:56:06 +0200 |
---|---|---|
committer | Peter Rajnoha <prajnoha@redhat.com> | 2022-08-17 10:47:24 +0200 |
commit | b4cc28c2ef72ee022c1a03819927e31775c18676 (patch) | |
tree | 2d027a817b5a26e53ce32e97dd8cdb9f835568af /lib/config | |
parent | 81839cc4ebf566ea9f3e6d819c7338b98cdec374 (diff) | |
download | lvm2-b4cc28c2ef72ee022c1a03819927e31775c18676.tar.gz |
lvmconfig: add --valuesonly option
The new --valuesonly option causes the lvmconfig output to contain only
values without keys for each config node. This is practical mainly in
case where we use lvmconfig in scripts and we want to assign the value
to a different custom key or simply output the value itself without the
key.
For example:
# lvmconfig --type full activation/raid_fault_policy
raid_fault_policy="warn"
# lvmconfig --type full activation/raid_fault_policy --valuesonly
"warn"
# my_var=$(lvmconfig --type full activation/raid_fault_policy --valuesonly)
# echo $my_var
"warn"
Diffstat (limited to 'lib/config')
-rw-r--r-- | lib/config/config.c | 22 | ||||
-rw-r--r-- | lib/config/config.h | 1 |
2 files changed, 22 insertions, 1 deletions
diff --git a/lib/config/config.c b/lib/config/config.c index f9614779a..1142ed345 100644 --- a/lib/config/config.c +++ b/lib/config/config.c @@ -1830,8 +1830,10 @@ static int _out_line_fn(const struct dm_config_node *cn, const char *line, void char summary[MAX_COMMENT_LINE+1]; char version[9]; int pos = 0; - size_t len; + int space_prefix_len; char *space_prefix; + const char *p; + size_t len; if ((out->tree_spec->type == CFG_DEF_TREE_DIFF) && (!(out->tree_spec->check_status[cn->id] & CFG_DIFF))) @@ -1865,9 +1867,24 @@ static int _out_line_fn(const struct dm_config_node *cn, const char *line, void /* Usual tree view with nodes and their values. */ + if (out->tree_spec->valuesonly && !(cfg_def->type & CFG_TYPE_SECTION)) { + if ((space_prefix_len = strspn(line, "\t "))) { + len = strlen(line); + p = line + space_prefix_len; + + /* copy space_prefix, skip key and '=', copy value */ + dm_pool_begin_object(out->mem, len); + dm_pool_grow_object(out->mem, line, space_prefix_len); + dm_pool_grow_object(out->mem, p + strcspn(p, "=") + 1, len + 1); + line = dm_pool_end_object(out->mem); + } else + line = strchr(line, '=') + 1; + } + if ((out->tree_spec->type != CFG_DEF_TREE_CURRENT) && (out->tree_spec->type != CFG_DEF_TREE_DIFF) && (out->tree_spec->type != CFG_DEF_TREE_FULL) && + !out->tree_spec->valuesonly && (cfg_def->flags & (CFG_DEFAULT_UNDEFINED | CFG_DEFAULT_COMMENTED))) { /* print with # at the front to comment out the line */ if (_should_print_cfg_with_undef_def_val(out, cfg_def, cn)) { @@ -1883,6 +1900,9 @@ static int _out_line_fn(const struct dm_config_node *cn, const char *line, void if (_should_print_cfg_with_undef_def_val(out, cfg_def, cn)) fprintf(out->fp, "%s\n", line); + if (out->tree_spec->valuesonly && !(cfg_def->type & CFG_TYPE_SECTION) && space_prefix_len) + dm_pool_free(out->mem, (char *) line); + return 1; } diff --git a/lib/config/config.h b/lib/config/config.h index c62ea20a5..3926b12f5 100644 --- a/lib/config/config.h +++ b/lib/config/config.h @@ -176,6 +176,7 @@ struct config_def_tree_spec { unsigned unconfigured:1; /* use unconfigured path strings */ unsigned withgeneralpreamble:1; /* include preamble for a general config file */ unsigned withlocalpreamble:1; /* include preamble for a local config file */ + unsigned valuesonly:1; /* print only values without keys */ uint8_t *check_status; /* status of last tree check (currently needed for CFG_DEF_TREE_MISSING only) */ }; |