Skip to content

Commit

Permalink
Add row printer
Browse files Browse the repository at this point in the history
  • Loading branch information
lotusirous committed Dec 14, 2021
1 parent 12d25a4 commit d35f167
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 15 deletions.
24 changes: 9 additions & 15 deletions main.go → fp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"
"os"
"path/filepath"
"sort"
)

// DigestGroup hashes a file for a given path
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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()
}
55 changes: 55 additions & 0 deletions printer.go
Original file line number Diff line number Diff line change
@@ -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])
}
}
21 changes: 21 additions & 0 deletions printer_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

}

0 comments on commit d35f167

Please sign in to comment.