Skip to content

Commit

Permalink
ksconf merge: add '--in-place' option
Browse files Browse the repository at this point in the history
  • Loading branch information
lowell80 committed Oct 3, 2023
1 parent 32d8015 commit c5c1c02
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ Ksconf v0.12.1 (DRAFT)
Use ``--attr-matches`` and/or ``--attr-not-matches`` to match specific attribute and value combinations for stanza matching.
This can be used to find props with a specific ``KV_MODE``, find saved search containing a specific search command, or list indexes not using ``volume:`` designation.
See the `ksconf_cmd_filter` docs for example usage.
* Add ``--in-place`` processing behavior for :ref:`ksconf_cmd_merge` to simplify the process of merging new content into an existing conf file.
* Fixed documentation generation bug that prevented command line options from showing up in the docs.
* Fixed some CLI file handling bug that resulted in broken use of ``-`` (stdin) and/or fancy shell commands involving ``<(some command)`` syntax, which can be a helpful trick to reduce the number of temporary files.



Expand Down
16 changes: 15 additions & 1 deletion ksconf/commands/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ksconf.commands import ConfFileProxy, ConfFileType, KsconfCmd, dedent
from ksconf.conf.merge import merge_conf_files
from ksconf.conf.parser import PARSECONF_MID, PARSECONF_STRICT
from ksconf.consts import EXIT_CODE_SUCCESS
from ksconf.consts import EXIT_CODE_BAD_ARGS, EXIT_CODE_SUCCESS
from ksconf.util.completers import conf_files_completer


Expand Down Expand Up @@ -44,6 +44,12 @@ def register_args(self, parser):
parser.add_argument("--ignore-missing", "-s", default=False, action="store_true",
help="Silently ignore any missing CONF files.")

parser.add_argument("--in-place", "-i", default=False, action="store_true",
help=dedent("""
Enable in-place update mode. When selected, the target file will also be considered as
the base of the merge operation. All conf files will be merged with target. When disabled,
any existing content within target is ignored and overwritten."""))

parser.add_argument("--dry-run", "-D", default=False, action="store_true", help=dedent("""\
Enable dry-run mode.
Instead of writing to TARGET, preview changes in 'diff' format.
Expand All @@ -52,6 +58,14 @@ def register_args(self, parser):
A banner or warning comment added to the top of the TARGET file.
Used to discourage Splunk admins from editing an auto-generated file."""))

def pre_run(self, args):
if args.in_place:
if args.target.name == "<stdout>":
self.stderr.write("In-place mode require '--target=FILE'.\n")
return EXIT_CODE_BAD_ARGS
# Insert target as the first source CONF file to parse
args.conf.insert(0, args.target.name)

def run(self, args):
''' Merge multiple configuration files into one '''
self.parse_profile = PARSECONF_MID
Expand Down

0 comments on commit c5c1c02

Please sign in to comment.