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 json formatted output option #768

Open
crabdancing opened this issue Jan 7, 2024 · 5 comments · May be fixed by #783
Open

feat: add json formatted output option #768

crabdancing opened this issue Jan 7, 2024 · 5 comments · May be fixed by #783
Assignees

Comments

@crabdancing
Copy link

It's become somewhat popular to include JSON output as an option, to make it easier to combine multiple utilities reliably. Examples include the iproute utilities (e.g., ip --json addr) and systemd utilities (e.g., systemctl list-units -o json). Structured data is a very powerful technique for organizing scripts -- so much so that shells are being created built around the concept (e.g., Nushell).

@MartinFillon
Copy link
Contributor

Hey @alxpettit could you build this commit on your machine and tell me what you think of the output, and if it fits your need

@crabdancing
Copy link
Author

crabdancing commented Jan 11, 2024

Looks like a good start! :) I'm seeing a couple of problems with the output, though:

  1. Color should probably be disabled by default when printing JSON, as it's intended to be parsed by another program
  2. Erroneously prints malformed JSON expressions
  3. Does not store in table format, making it very hard to meaningfully look up data.

I'll go over them in detail here.

Color

The color one seems pretty self explanatory. If we color the text, programs for parsing JSON may be confused by the character codes and introduce garbage into their own data. (EDIT: Although it just occurs to me now that it might change its behavior as it's being piped in, so this is probably fine.)

Erroneously prints malformed JSON expressions

Here, let's use the directory structure of the project directory as an example. If I run it in this directory path like so: cargo run -- --json --color=never, it gives me:

{"files":["drwxr-xr-x    - ada 10 Jan 17:09 benches", ".rw-r--r-- 3.9k ada 10 Jan 17:09 build.rs", ".rw-r--r--  43k ada 10 Jan 17:09 Cargo.lock", ".rw-r--r-- 3.2k ada 10 Jan 17:09 Cargo.toml", ".rw-r--r--  31k ada 10 Jan 17:09 CHANGELOG.md", ".rw-r--r-- 2.5k ada 10 Jan 17:09 cliff.toml", ".rw-r--r-- 5.5k ada 10 Jan 17:09 CODE_OF_CONDUCT.md", "drwxr-xr-x    - ada 10 Jan 17:09 completions", ".rw-r--r-- 8.1k ada 10 Jan 17:09 CONTRIBUTING.md", ".rw-r--r-- 1.7k ada 10 Jan 17:09 deb.asc", ".rw-r--r--  12k ada 10 Jan 17:09 deny.toml", "drwxr-xr-x    - ada 10 Jan 17:09 devtools", "drwxr-xr-x    - ada 10 Jan 17:09 docs", ".rw-r--r-- 8.1k ada 10 Jan 17:09 flake.lock", ".rw-r--r-- 9.3k ada 10 Jan 17:09 flake.nix", ".rw-r--r-- 6.2k ada 10 Jan 17:09 INSTALL.md", ".rw-r--r-- 7.2k ada 10 Jan 17:09 Justfile", ".rw-r--r-- 1.1k ada 10 Jan 17:09 LICENCE", "drwxr-xr-x    - ada 10 Jan 17:09 man", ".rw-r--r-- 2.6k ada 10 Jan 17:09 powertest.yaml", ".rw-r--r-- 7.9k ada 10 Jan 17:09 README.md", ".rw-r--r--  146 ada 10 Jan 17:09 rust-toolchain.toml", ".rw-r--r--   34 ada 10 Jan 17:09 rustfmt.toml", ".rw-r--r--  505 ada 10 Jan 17:09 SECURITY.md", "drwxr-xr-x    - ada 10 Jan 17:09 snap", "drwxr-xr-x    - ada 10 Jan 17:09 src", "drwxr-xr-x    - ada 10 Jan 17:10 target", "drwxr-xr-x    - ada 10 Jan 17:09 tests", ".rw-r--r--  586 ada 10 Jan 17:09 treefmt.nix"]}}

This is super weird, because there's an extra } that shouldn't be there, given that the expression started with only one {. I tested it in Nushell, and it breaks eval when I try to import it as structured data.

Formatting

To make the structured data useful, the formatting should be in a table formation -- that's to say root-level list, with each entry being represented by key: value attribute relations. That way, programs know exactly what data they're trying to grab. I went ahead and formatted the project directory like so via Nushell (ls | to json | wl-copy, if you're curious). The result is here.

@MartinFillon
Copy link
Contributor

This is super weird, because there's an extra } that shouldn't be there, given that the expression started with only one {. I tested it in Nushell, and it breaks eval when I try to import it as structured data.

didnt catch that with jq well found

you understood color well

gonna try and implement the key one tho.

@MartinFillon
Copy link
Contributor

can you retry and tell me what do you think now, I made some updates

@MartinFillon
Copy link
Contributor

Hey just a heads up to everyone here

The recurse option is here 🎉

Can I have you all thought on it, and if you think --json --tree is needed

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 a pull request may close this issue.

2 participants