Skip to content

jsonnetfmt cli tool could support a canonical mode #721

@netomi

Description

@netomi

We use jsonnet to support an infrastructure as code approach to provision resources on GitHub.
For that purpose, a jsonnet file can be created that defines all the resources an organization should have on GitHub using some template functions to define defaults that can be overridden as needed.

Here is an example of such a configuration: https://github.com/adoptium/.eclipsefdn/blob/main/otterdog/adoptium.jsonnet

Now we also want a way to compare such a user edited configuration with a canonical version to highlight to the user which settings can be omitted (as they are already defined in the template function) and are thus redundant.

However, a simple diff will also outline irrelevant differences, e.g. due to different ordering of object fields or array elements. As a workaround for our relatively simple jsonnet files, we implemented a hacky way to produce a canonical version of such a jsonnet file.

An idea to make this cleaner would be to utilize the existing jsonnetfmt tool and adding an additional AST pass that sorts object fields and array elements according to some criteria, while outputting anything just like always.

The criteria that we came up so far was the following:

  • keep locals and asserts in objects untouched and stable and make sure they are before any object fields
  • convert each object field and array element into its string representation by stripping all fodder when sorting them

That approach was working well with our type of jsonnet files, which do not use that many jsonnet features.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions