Skip to content

Commit

Permalink
dump: only sort keys if explicitly requested
Browse files Browse the repository at this point in the history
The yaml and json formatter differed w.r.t sorting the keys during
serialization (json kept the order, yaml sorted). As pointed out in #118
the users expect to maintain the order of the keys. By that, we make
this the new default and add an CLI option "--sort" to sort the output.

Closes: #118
Signed-off-by: Felix Moessbauer <[email protected]>
Signed-off-by: Jan Kiszka <[email protected]>
  • Loading branch information
fmoessbauer authored and jan-kiszka committed Dec 21, 2024
1 parent 6c534f0 commit df267f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
16 changes: 13 additions & 3 deletions kas/plugins/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
- the dumped config is semantically identical but not bit-by-bit identical
- all referenced repositories are checked out to resolve cross-repo configs
- all branches are resolved before patches are applied
- the ordering of the keys is kept unless ``--sort`` is used. If you intend
to store the flattened configs for comparison, it is recommended to sort
the keys.
For example, to get a single config representing the final build config of
``kas-project.yml:target-override.yml`` you could run::
Expand Down Expand Up @@ -152,6 +155,10 @@ def setup_parser_format_args(parser):
type=int,
default=4,
help='Line indent (# of spaces, default: 4)')
parser.add_argument('--sort',
action='store_true',
default=False,
help='Alphanumerically sort keys in output')

@classmethod
def setup_parser(cls, parser):
Expand Down Expand Up @@ -179,18 +186,20 @@ def setup_parser(cls, parser):
help=argparse.SUPPRESS)

@staticmethod
def dump_config(config: dict, target: IoTarget, format: str, indent: int):
def dump_config(config: dict, target: IoTarget, format: str, indent: int,
sorted: bool):
"""
Dump the configuration to the target in the specified format.
"""
with IoTargetMonitor(target) as f:
if format == 'json':
json.dump(config, f, indent=indent)
json.dump(config, f, indent=indent, sort_keys=sorted)
f.write('\n')
elif format == 'yaml':
yaml.dump(
config, f,
indent=indent,
sort_keys=sorted,
Dumper=Dump.KasYamlDumper)
else:
raise OutputFormatError(format)
Expand Down Expand Up @@ -264,7 +273,8 @@ def _filter_local(repos):
if args.resolve_env and 'env' in config_expanded:
config_expanded['env'] = ctx.config.get_environment()

self.dump_config(config_expanded, output, args.format, args.indent)
self.dump_config(config_expanded, output, args.format, args.indent,
args.sort)


__KAS_PLUGINS__ = [Dump]
3 changes: 2 additions & 1 deletion kas/plugins/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def _filter_enabled(repos):
output = IoTarget(target=lockfile, managed=True)
format = "json" if lockfile.suffix == '.json' else "yaml"

Dump.dump_config(config_expanded, output, format, args.indent)
Dump.dump_config(config_expanded, output, format,
args.indent, args.sort)
logging.info('Lockfile created: %s',
os.path.relpath(lockfile, os.getcwd()))

Expand Down

0 comments on commit df267f4

Please sign in to comment.