tycho-kirchner / shournal

Log shell-commands and used files. Snapshot executed scripts. Fully automatic.
GNU General Public License v3.0
173 stars 9 forks source link

"shournal -q --output-format json --history 3" doesn't seem to generate valid json #9

Closed uli-heller closed 10 months ago

uli-heller commented 10 months ago

An example using "jq" to pretty-print the json:

$ shournal -q --output-format json --history 3|jq .
parse error: Invalid numeric literal at line 1, column 7

Without "jq":

$ shournal -q --output-format json --history 3
HEADER:{"pathToReadFiles":"/home/uli/.local/share/shournal/readFiles"}
COMMAND:{"command":"shournal -q --history 3","endTime":"2023-12-24T09:51:36.102","fileReadEvents":[],"fileWriteEvents":[],"hashChunkSize":256,"hashMaxCountOfReads":3,"hostname":"ulicsl","id":47,"returnValue":0,"sessionUuid":"LH8mlKI5Ee6/4be4GFd0/A==","startTime":"2023-12-24T09:51:36.053","username":"uli","workingDir":"/home/uli"}
COMMAND:{"command":"shournal -q --history 300","endTime":"2023-12-24T09:51:43.702","fileReadEvents":[],"fileWriteEvents":[],"hashChunkSize":256,"hashMaxCountOfReads":3,"hostname":"ulicsl","id":48,"returnValue":0,"sessionUuid":"LH8mlKI5Ee6/4be4GFd0/A==","startTime":"2023-12-24T09:51:43.631","username":"uli","workingDir":"/home/uli"}
COMMAND:{"command":"shournal -q --history 300|grep google","endTime":"2023-12-24T09:52:00.366","fileReadEvents":[],"fileWriteEvents":[],"hashChunkSize":256,"hashMaxCountOfReads":3,"hostname":"ulicsl","id":49,"returnValue":0,"sessionUuid":"LH8mlKI5Ee6/4be4GFd0/A==","startTime":"2023-12-24T09:52:00.304","username":"uli","workingDir":"/home/uli"}
FOOTER:{"countOfRestoredFiles":0,"restorePath":null}

Maybe a format like this is better:

{
"HEADER":{"pathToReadFiles":"/home/uli/.local/share/shournal/readFiles"},
"COMMANDS": [
  {"command":"shournal -q --history 3","endTime":"2023-12-24T09:51:36.102","fileReadEvents":[],"fileWriteEvents":[],"hashChunkSize":256,"hashMaxCountOfReads":3,"hostname":"ulicsl","id":47,"returnValue":0,"sessionUuid":"LH8mlKI5Ee6/4be4GFd0/A==","startTime":"2023-12-24T09:51:36.053","username":"uli","workingDir":"/home/uli"},
  {"command":"shournal -q --history 300","endTime":"2023-12-24T09:51:43.702","fileReadEvents":[],"fileWriteEvents":[],"hashChunkSize":256,"hashMaxCountOfReads":3,"hostname":"ulicsl","id":48,"returnValue":0,"sessionUuid":"LH8mlKI5Ee6/4be4GFd0/A==","startTime":"2023-12-24T09:51:43.631","username":"uli","workingDir":"/home/uli"},
  {"command":"shournal -q --history 300|grep google","endTime":"2023-12-24T09:52:00.366","fileReadEvents":[],"fileWriteEvents":[],"hashChunkSize":256,"hashMaxCountOfReads":3,"hostname":"ulicsl","id":49,"returnValue":0,"sessionUuid":"LH8mlKI5Ee6/4be4GFd0/A==","startTime":"2023-12-24T09:52:00.304","username":"uli","workingDir":"/home/uli"}
],
"FOOTER":{"countOfRestoredFiles":0,"restorePath":null}
}
tycho-kirchner commented 10 months ago

Thanks for this suggestion. Back then, it was a design decision not to use a "wrapping" JSON structure around the commands to allow for immediate processing in the JSON-reader. Otherwise, on my machine, jq does not output anything until it has parsed shournal's total output. This can be slow, when a large history is queried for. Instead, I suggest the following:

shournal -q --output-format json --history 10000 | grep -F 'COMMAND:' | sed -n 's/COMMAND://p' | jq

In principle, of course it would also be possible to add a, e.g., cjson (for canonical json) output format, when speed is no priority.

uli-heller commented 10 months ago

OK, I understand this. Although the name "--output-format json" suggests to me that the complete output is valid json. Using the suggestion above and your evaluation command, this might produce similar output:

shournal -q --output-format json --history 10000 | grep -F '  {"command":'|jq
tycho-kirchner commented 10 months ago

I agree. However, people may rely on the current quirky format and usually I try to avoid interface-breaking changes. I keep that as a "maybe_todo" 🙂

uli-heller commented 10 months ago

No problem, thx.