wagoodman / dive

A tool for exploring each layer in a docker image
MIT License
44.53k stars 1.69k forks source link

Truncate JSON output file #502

Open Foxlik opened 5 months ago

Foxlik commented 5 months ago

The JSON output file is not truncated at the moment. So in case where I write to an existing JSON file I get a broken JSON if the new contents is shorter than the previous.

$ dive --json dive.json image_a:latest
Image Source: image_a:latest
Fetching image... (this can take a while for large images)
Analyzing image...
Exporting image to 'dive.json'...
$ jq . dive.json > /dev/null
$ dive --json dive.json image_b:latest
Image Source: image_b:latest
Fetching image... (this can take a while for large images)
Analyzing image...
Exporting image to 'dive.json'...
$ jq . dive.json > /dev/null
parse error: Invalid numeric literal at line 161, column 6
$ cat dive.json | nl
     1  {
     2    "layer": [
     3      {
... TRUNCATED ...
   154        {
   155          "count": 2,
   156          "sizeBytes": 0,
   157          "file": "/tmp"
   158        }
   159      ]
   160    }
   161  }ve_${DIVE_VERSION}_linux_amd64.tar.gz       | tar -zxf /dev/stdin -C /dive -T/dive.list     \u0026\u0026 rm -f /dive.list \u0026\u0026 echo \"${DIVE_SHA256} /dive/dive\" | sha256sum -c"
   162      }
   163    ],
   164    "image": {
   165      "sizeBytes": 106007465,
   166      "inefficientBytes": 0,
   167      "efficiencyScore": 1,
   168      "fileReference": []
   169    }
   170  }
$ rm dive.json
$ dive --json dive.json image_b:latest
Image Source: image_b:latest
Fetching image... (this can take a while for large images)
Analyzing image...
Exporting image to 'dive.json'...
$ jq . dive.json > /dev/null
$

I think the issue is the JSON output file should be opened with O_TRUNC as well. https://github.com/wagoodman/dive/blob/3ef1dd2c74c6952064d866adc2b225048d939f83/runtime/run.go#L61

Or am I missing something?

I can submit a PR if it makes sense.