Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for formatting jsonnet with substation cli #250

Merged
merged 3 commits into from
Oct 4, 2024

Conversation

brittonhayes
Copy link
Contributor

@brittonhayes brittonhayes commented Oct 3, 2024

Description

This change adds support for running substation fmt against jsonnet and libsonnet files, automatically normalizing the format of substation configurations.

Motivation and Context

This change was added to continue making substation more approachable and easy to work with by enhancing the CLI that is used to manage and develop configurations. Right now the CLI supports building and testing. The addition of substation fmt adds a very small piece of that puzzle which is ensuring that all configuration is formatted the same way - this improves quality of life for teams managing many configs that are written by multiple developers.

How Has This Been Tested?

The CLI has been locally built and tested on example jsonnet and libsonnet files to verify functionality.

When running substation fmt, the output styling mimics that of go fmt, for consistency with the stylistic choices in substation test.

Write formatted changes to stdout.

> substation fmt examples/transform/aggregate/sample/config.jsonnet
// This example samples data by aggregating events into an array, then
// selecting the first event in the array as a sample. The sampling rate
// is 1/N, where N is the count of events in the buffer.
local sub = import '../../../../substation.libsonnet';

{
  tests: [
    {
      name: 'sample',
      transforms: [
        sub.tf.test.message({ value: { a: 'b' } }),
        sub.tf.test.message({ value: { c: 'd' } }),
        sub.tf.test.message({ value: { e: 'f' } }),
        sub.tf.test.message({ value: { g: 'h' } }),
        sub.tf.test.message({ value: { i: 'j' } }),
        sub.tf.test.message({ value: { k: 'l' } }),
        sub.tf.test.message({ value: { m: 'n' } }),
        sub.tf.test.message({ value: { o: 'p' } }),
        sub.tf.test.message({ value: { q: 'r' } }),
        sub.tf.test.message({ value: { s: 't' } }),
        sub.tf.test.message({ value: { u: 'v' } }),
        sub.tf.test.message({ value: { w: 'x' } }),
        sub.tf.test.message({ value: { y: 'z' } }),
        sub.tf.test.message({ value: ' ' }),
        sub.tf.send.stdout(),
      ],
      // Asserts that the message is '{"c":"d"}'.
      condition: sub.cnd.num.len.greater_than({ value: 0 }),
    },
  ],
  transforms: [
    // Events are aggregated into an array. This example has a sample
    // rate of up to 1/5. By default, the sample rate will be lower if
    // fewer than 5 events are processed by Substation.
    sub.tf.aggregate.to.array({ object: { target_key: 'meta sample' }, batch: { count: 5 } }),
    // A strict sample rate can be enforced by dropping any events that
    // contain the `sample` key, but do not have a length of 5.
    sub.tf.meta.switch(settings={ cases: [
      {
        condition: sub.cnd.num.len.eq({ object: { source_key: 'meta sample' }, value: 5 }),
        transforms: [
          sub.tf.object.copy({ object: { source_key: 'meta sample.0' } }),
        ],
      },
      {
        condition: sub.cnd.num.len.gt({ object: { source_key: 'meta sample' }, value: 0 }),
        transforms: [
          sub.tf.util.drop(),
        ],
      },
    ] }),
    sub.tf.obj.cp({ object: { source_key: 'meta sample.0' } }),
    sub.tf.send.stdout(),
  ],
}

Write formatted changes to the file.

> substation fmt -w examples/transform/aggregate/sample/config.jsonnet
examples/transform/aggregate/sample/config.jsonnet

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@brittonhayes brittonhayes marked this pull request as ready for review October 3, 2024 22:23
@brittonhayes brittonhayes requested a review from a team as a code owner October 3, 2024 22:23
@jshlbrd jshlbrd self-assigned this Oct 3, 2024
Copy link
Collaborator

@jshlbrd jshlbrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks nice and straightforward. I tested it on our internal config repo and it formatted 600+ files in under 1 second.

I added a change for the short description to match the style of the other commands, but other than that this looks ready to merge.

cmd/substation/fmt.go Outdated Show resolved Hide resolved
cmd/substation/fmt.go Outdated Show resolved Hide resolved
@jshlbrd jshlbrd self-requested a review October 4, 2024 00:23
@jshlbrd jshlbrd merged commit 6cae3cd into brexhq:main Oct 4, 2024
4 checks passed
@brittonhayes brittonhayes deleted the feat/substation-fmt-cmd branch October 4, 2024 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants