Skip to content

Add support for STDIN and STDOUT to snmp_exporter_generator #1481

@nf-brentsaner

Description

@nf-brentsaner

This being 2025, I would expect that CLI programs that read non-interactive input and write non-interactive output (e.g. not done at an interactive prompt) to support STDIN and STDOUT respectively.

Granted, one can do e.g.:

cat generator.yml | \
snmp_exporter_generator generate \
  -m mibs \
  -g /dev/stdin \
  -o /dev/stdout \
2>/tmp/err.out

to accomplish this, and it of course does work (for now), but:

  • This is hacky at best (it's certainly inelegant)
  • It is prone to breakage if snmp_exporter_generator ever starts printing to STDOUT during execution
    • (If implemented via proper switch arg parsing/pipe detection, e.g. -o -, that can trigger a specific global boolean that would suppress writing to STDOUT)
  • I suspect non-seasoned/newbie Linux administrators to not even know the above is possible.

Thus, I suggest the following behavior instead:

  • -g somefile.yml (or --generator-path="somefile.yml") should use the defined somefile.yml, regardless of STDIN.
  • -g or --generator-path (no value provided) uses the default generator.yml, regardless of STDIN.
  • -g - (or --generator-path="-") forces reading from STDIN.
  • If no -g or --generator-path is specified, then:
    • If os.Stdin is a pipe (see here and here), use this as the input for the source generator file.
    • If it is not detected, use the default generator.yml.

Similarly, for for -o / --output-path:

  • -o somefile.yml (or --output-path="somefile.yml") should use the defined somefile.yml, regardless of STDOUT.
  • -o or --output-path (no value provided) uses the default snmp.yml, regardless of STDOUT.
  • -o - (or --output-path="-") forces writing to STDOUT.
  • If no -o or --output-path is specified, then:
    • If os.STDOUT is a pipe or redirect (see here -- basically the same as above should work, just operating on os.Stdout instead of os.Stdin), use STDOUT as the write destination.
    • If it is not detected, use the default snmp.yml.

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