From 27435a1e564b664466ab06a9b294092b67111009 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Fri, 29 May 2015 16:48:02 -0700 Subject: [PATCH] no not print empty keys in docker info Signed-off-by: Victor Vieux --- ioutils/fmt.go | 14 ++++++++++++++ ioutils/fmt_test.go | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 ioutils/fmt.go create mode 100644 ioutils/fmt_test.go diff --git a/ioutils/fmt.go b/ioutils/fmt.go new file mode 100644 index 0000000..801132f --- /dev/null +++ b/ioutils/fmt.go @@ -0,0 +1,14 @@ +package ioutils + +import ( + "fmt" + "io" +) + +// FprintfIfNotEmpty prints the string value if it's not empty +func FprintfIfNotEmpty(w io.Writer, format, value string) (int, error) { + if value != "" { + return fmt.Fprintf(w, format, value) + } + return 0, nil +} diff --git a/ioutils/fmt_test.go b/ioutils/fmt_test.go new file mode 100644 index 0000000..8968863 --- /dev/null +++ b/ioutils/fmt_test.go @@ -0,0 +1,17 @@ +package ioutils + +import "testing" + +func TestFprintfIfNotEmpty(t *testing.T) { + wc := NewWriteCounter(&NopWriter{}) + n, _ := FprintfIfNotEmpty(wc, "foo%s", "") + + if wc.Count != 0 || n != 0 { + t.Errorf("Wrong count: %v vs. %v vs. 0", wc.Count, n) + } + + n, _ = FprintfIfNotEmpty(wc, "foo%s", "bar") + if wc.Count != 6 || n != 6 { + t.Errorf("Wrong count: %v vs. %v vs. 6", wc.Count, n) + } +}