wagoodman / dive

A tool for exploring each layer in a docker image

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Truncate JSON output file

Foxlik opened this issue · comments

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.

file, err := filesystem.OpenFile(options.ExportFile, os.O_RDWR|os.O_CREATE, 0644)

Or am I missing something?

I can submit a PR if it makes sense.