Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit cd1ffc8

Browse files
author
Priya Wadhwa
committed
Fixed merge conflict
2 parents 4a0b8fe + 66d3837 commit cd1ffc8

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,42 @@ type MultiVersionInfo struct {
243243
}
244244
```
245245

246+
## User Customized Output
247+
Users can customize the format of the output of diffs with the`--format` flag. The flag takes a Go template string, which specifies the format the diff should be output in. This template string uses the structs described above, depending on the differ used, to format output. The default template strings container-diff uses can be found [here](https://github.com/GoogleCloudPlatform/container-diff/blob/master/util/template_utils.go).
248+
249+
An example using the pip package analyzer is shown below, in which only package names are printed (some are repeated because of version differences).
250+
251+
```shell
252+
$ container-diff analyze gcr.io/google-appengine/python:latest --type=pip --format='
253+
-----{{.AnalyzeType}}-----
254+
Packages found in {{.Image}}:{{if not .Analysis}} None{{else}}
255+
{{range .Analysis}}{{"\n"}}{{.Name}}{{end}}
256+
{{end}}
257+
'
258+
Retrieving image gcr.io/google-appengine/python:latest from source Cloud Registry
259+
Retrieving analyses
260+
261+
-----Pip-----
262+
Packages found in gcr.io/google-appengine/python:latest:
263+
264+
chardet
265+
colorama
266+
html5lib
267+
mercurial
268+
pip
269+
pip
270+
pip
271+
requests
272+
setuptools
273+
setuptools
274+
setuptools
275+
six
276+
urllib3
277+
virtualenv
278+
wheel
279+
wheel
280+
```
281+
246282
## Known issues
247283

248284
To run container-diff using image IDs, docker must be installed.
@@ -426,7 +462,7 @@ If using existing package tools, you should create the appropriate structs (e.g.
426462
```go
427463
type Result interface {
428464
OutputStruct() interface{}
429-
OutputText(resultType string) error
465+
OutputText(resultType string, format string) error
430466
}
431467
```
432468

util/format_utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,23 @@ func TemplateOutput(diff interface{}, templateType string) error {
7979
w.Flush()
8080
return nil
8181
}
82+
83+
func TemplateOutputFromFormat(diff interface{}, templateType string, format string) error {
84+
if format == "" {
85+
return TemplateOutput(diff, templateType)
86+
}
87+
funcs := template.FuncMap{"join": strings.Join}
88+
tmpl, err := template.New("tmpl").Funcs(funcs).Parse(format)
89+
if err != nil {
90+
logrus.Warningf("User specified format resulted in error, printing default output.")
91+
logrus.Error(err)
92+
return TemplateOutput(diff, templateType)
93+
}
94+
w := tabwriter.NewWriter(os.Stdout, 8, 8, 8, ' ', 0)
95+
err = tmpl.Execute(w, diff)
96+
if err != nil {
97+
return err
98+
}
99+
w.Flush()
100+
return nil
101+
}

0 commit comments

Comments
 (0)