From d35f167bda5c110a140213087d2d1762526bbbf7 Mon Sep 17 00:00:00 2001 From: lotusirous <5105237+lotusirous@users.noreply.github.com> Date: Tue, 14 Dec 2021 10:46:45 +0900 Subject: [PATCH] Add row printer --- main.go => fp.go | 24 ++++++++------------- printer.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ printer_test.go | 21 ++++++++++++++++++ 3 files changed, 85 insertions(+), 15 deletions(-) rename main.go => fp.go (85%) create mode 100644 printer.go create mode 100644 printer_test.go diff --git a/main.go b/fp.go similarity index 85% rename from main.go rename to fp.go index dac24b8..871d379 100644 --- a/main.go +++ b/fp.go @@ -11,7 +11,6 @@ import ( "io" "os" "path/filepath" - "sort" ) // DigestGroup hashes a file for a given path @@ -41,18 +40,6 @@ func DigestGroup(hg map[string]hash.Hash, loc string) (map[string]string, error) return out, nil } -func writeTo(values map[string]string, w io.Writer) { - keys := make([]string, 0, len(values)) - for k := range values { - keys = append(keys, k) - } - sort.Strings(keys) - - for _, name := range keys { - fmt.Fprintf(w, "%s: %s\n", name, values[name]) - } -} - // ReadFileInfo gets the abs path and follows the link func ReadFileInfo(path string) (string, os.FileInfo, error) { abs, err := filepath.Abs(path) @@ -98,7 +85,13 @@ func main() { os.Exit(1) } - fmt.Fprintf(fd, "PATH: %s\n", abs) + rr := NewRowRender(os.Stdout) + if fi.IsDir() { + rr.AddRow("DIR", abs) + } else { + rr.AddRow("PATH", abs) + } + if *verbose && !fi.IsDir() { hg := make(map[string]hash.Hash) hg["MD5"] = md5.New() @@ -110,7 +103,8 @@ func main() { fmt.Fprintf(fd, "Cannot digest group: %v", err) os.Exit(1) } - writeTo(values, fd) + rr.AddRowMap(values) } + rr.Render() } diff --git a/printer.go b/printer.go new file mode 100644 index 0000000..6f66b96 --- /dev/null +++ b/printer.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "io" + "sort" +) + +// RowRenderer render the row to a file descriptor. +type RowRenderer interface { + // AddRow adds a row to writer. + AddRow(field, value string) + // AddRowMap appends the row by map input. + AddRowMap(values map[string]string) + // Render the rows. + Render() +} + +// NewRowRender creates the printer. +func NewRowRender(w io.Writer) RowRenderer { + return &pw{w, make([][]string, 0)} +} + +type pw struct { + w io.Writer + rows [][]string +} + +// AddRow adds a row to writer. +func (p *pw) AddRow(field, value string) { + r := []string{field, value} + p.rows = append(p.rows, r) +} + +// AddRowMap appends the row from map. It sorts the map by default. +func (p *pw) AddRowMap(values map[string]string) { + keys := make([]string, 0, len(values)) + for k := range values { + keys = append(keys, k) + } + sort.Strings(keys) + + for _, name := range keys { + r := []string{name, values[name]} + p.rows = append(p.rows, r) + } + +} + +// Render the rows to output +func (p *pw) Render() { + for _, l := range p.rows { + fmt.Fprintf(p.w, "%s: %s\n", l[0], l[1]) + } +} diff --git a/printer_test.go b/printer_test.go new file mode 100644 index 0000000..4f59442 --- /dev/null +++ b/printer_test.go @@ -0,0 +1,21 @@ +package main + +import ( + "bytes" + "testing" +) + +func TestRowPrinter(t *testing.T) { + buf := new(bytes.Buffer) + pw := &pw{buf, make([][]string, 0)} + pw.AddRow("foo", "bar") + pw.Render() + + got := buf.String() + want := "foo: bar\n" + + if got != want { + t.Errorf("Got: %s - want: %s", buf, want) + } + +}