This repository was archived by the owner on Mar 27, 2024. It is now read-only.
Enhancement - save to file #279
Merged
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
This pull request will close #275, specifically allowing the user to specify a file to write to. Notes are included below.
What is the entrypoint for the user?
The application flow beings with the addition of a new variable,
outputFileIt's a string, the default being empty, that if not empty, will serve two purposes:
With any write to file it goes without saying we don't want to overwrite by default, but we can give the user a
--forcevariable to push it.Variable Choices
I chose
outputfor the command line flag (--output) because it reads nicely into a sentence, e.g., "--output=/path/to/file.json" reads to output goes to this file path. I also chosewfor the single character (well, o was taken) but also becausewcan also stand for "write," sort of like thegofmt -w <file>command. So "-w /path/to/file.json" reads to "write to this file path).What is the Client Flow?
The flag is revealed as an option
--outputif we doanalyzeordiff. Note that I also added aforceoption in the case that the file exists. The user can choose to overwrite it with--force.And here is for diff, it's mostly the same.
I specifically didn't add a short hand for "force." If a user wants to overwrite a file, it's a pretty big deal, and if they might mess up the specification of one letter (and accidentally overwrite) I don't want to risk this.
Manual Testing
We can first test that, without any specification of
--output, it still works (and prints to stdout).$ ./out/container-diff analyze ubuntu -----Size----- Analysis for ubuntu: IMAGE DIGEST SIZE ubuntu sha256:acd85db6e4b18aafa7fcde5480872909bd8e6d5fbd4e5e790ecc09acc06a8b78 68.6MNow let's write to a text file.
$ cat /tmp/file.txt -----Size----- Analysis for ubuntu: IMAGE DIGEST SIZE ubuntu sha256:acd85db6e4b18aafa7fcde5480872909bd8e6d5fbd4e5e790ecc09acc06a8b78 68.6MWe can then try to write to the same file, it shouldn't allow us!
...unless we tell it to force! (Use the force, logrus)
(Note no error messages).
We can do all of the above but specify json output.
$ ./out/container-diff analyze --output /tmp/file.json --json ubuntu $ cat /tmp/file.json [ { "Image": "ubuntu", "AnalyzeType": "Size", "Analysis": [ { "Name": "ubuntu", "Digest": "sha256:acd85db6e4b18aafa7fcde5480872909bd8e6d5fbd4e5e790ecc09acc06a8b78", "Size": 71945016 } ] } ]I think that's about it! I'm guessing you will want some tests? Let's have (high level discussion) on what we want to test (and recommendations for doing things like writing temporary files, organization and naming, etc.) and then I can give a GO at that too (pun, harhar).