From 17571b2926123110d02a420cf400533fe8d7e84e Mon Sep 17 00:00:00 2001 From: Deseao Date: Wed, 28 Apr 2021 15:58:07 -0500 Subject: [PATCH 1/5] Add support for html table formatting --- internal/runner/runner.go | 42 ++++++++++++++++++++++++++++++++-- internal/runner/runner_test.go | 22 ++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index dce80eac..b6b7cfc4 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -3,6 +3,7 @@ package runner import ( "encoding/json" + "html/template" "io" "os" "strconv" @@ -28,7 +29,10 @@ func Run(in io.Reader, out io.Writer, update, direct, exitWithNonZero bool, styl if err != nil { if err == io.EOF { filteredModules := mod.FilterModules(modules, update, direct) - renderTable(out, filteredModules, style) + tableErr := renderTable(out, filteredModules, style) + if tableErr != nil { + return tableErr + } if hasOutdated(filteredModules) && exitWithNonZero { OsExit(1) @@ -54,7 +58,10 @@ func hasOutdated(filteredModules []mod.Module) bool { return false } -func renderTable(writer io.Writer, modules []mod.Module, style string) { +func renderTable(writer io.Writer, modules []mod.Module, style string) error { + if style == "html" { + return RenderHTMLTable(writer, modules) + } table := tablewriter.NewWriter(writer) table.SetHeader([]string{"Module", "Version", "New Version", "Direct", "Valid Timestamps"}) @@ -75,4 +82,35 @@ func renderTable(writer io.Writer, modules []mod.Module, style string) { } table.Render() + return nil +} + +func RenderHTMLTable(writer io.Writer, modules []mod.Module) error { + type htmlTemplateData struct { + Path string + CurrentVersion string + NewVersion string + Direct bool + ValidTimestamp bool + } + + tableTemplate, err := template.New("dependencies").Parse(`{{range .}}{{end}}
ModuleVersionNew VersionDirectValid Timestamps
{{.Path}}{{.CurrentVersion}}{{.NewVersion}}{{.Direct}}{{.ValidTimestamp}}
`) + if err != nil { + return err + } + data := make([]htmlTemplateData, len(modules), len(modules)) + for i, module := range modules { + data[i] = htmlTemplateData{ + Path: module.Path, + CurrentVersion: module.CurrentVersion(), + NewVersion: module.NewVersion(), + Direct: !module.Indirect, + ValidTimestamp: !module.InvalidTimestamp(), + } + } + err = tableTemplate.Execute(writer, data) + if err != nil { + return err + } + return nil } diff --git a/internal/runner/runner_test.go b/internal/runner/runner_test.go index cf7143eb..85579945 100644 --- a/internal/runner/runner_test.go +++ b/internal/runner/runner_test.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "testing" + "github.com/psampaz/go-mod-outdated/internal/mod" "github.com/psampaz/go-mod-outdated/internal/runner" ) @@ -114,3 +115,24 @@ func TestRunExitWithNonZeroIndirectsOnly(t *testing.T) { t.Errorf("Expected exit code: %d, got: %d", exp, got) } } + +func TestHTMLTable(t *testing.T) { + var actualOutput bytes.Buffer + moduleInput := []mod.Module{mod.Module{ + Path: "github.com/mattn/go-runewidth", + Version: "v0.0.10", + Update: &mod.Module{ + Version: "v0.0.12", + }, + Indirect: true, + }} + err := runner.RenderHTMLTable(&actualOutput, moduleInput) + if err != nil { + t.Errorf("Error should be nil, got %w", err) + } + + const expectedOutput string = `
ModuleVersionNew VersionDirectValid Timestamps
github.com/mattn/go-runewidthv0.0.10v0.0.12falsetrue
` + if actualOutput.String() != expectedOutput { + t.Errorf("Expected table output to match \n%s, but got \n%s", expectedOutput, actualOutput.String()) + } +} From 08d3a7424f38191ea8ddaaf14b8b4735001bc127 Mon Sep 17 00:00:00 2001 From: Deseao Date: Thu, 29 Apr 2021 10:31:59 -0500 Subject: [PATCH 2/5] Upgrade to go 1.16 to use embed The dockerfile is already built using 1.16.3 so it should be fine --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 189d88aa..3054866a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/psampaz/go-mod-outdated -go 1.14 +go 1.16 require ( github.com/mattn/go-runewidth v0.0.10 // indirect From abdd4bfda4c563966abdb909488331aa05024016 Mon Sep 17 00:00:00 2001 From: Deseao Date: Thu, 29 Apr 2021 10:32:24 -0500 Subject: [PATCH 3/5] Use embed to parse the html template --- internal/runner/runner.go | 6 +++++- internal/runner/templates/table.html | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 internal/runner/templates/table.html diff --git a/internal/runner/runner.go b/internal/runner/runner.go index b6b7cfc4..797e69ff 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -2,6 +2,7 @@ package runner import ( + _ "embed" "encoding/json" "html/template" "io" @@ -16,6 +17,9 @@ import ( // OsExit is use here in order to simplify testing var OsExit = os.Exit +//go:embed templates/table.html +var tableTemplate string + // Run converts the the json output of go list -u -m -json all to table format func Run(in io.Reader, out io.Writer, update, direct, exitWithNonZero bool, style string) error { var modules []mod.Module @@ -94,7 +98,7 @@ func RenderHTMLTable(writer io.Writer, modules []mod.Module) error { ValidTimestamp bool } - tableTemplate, err := template.New("dependencies").Parse(`{{range .}}{{end}}
ModuleVersionNew VersionDirectValid Timestamps
{{.Path}}{{.CurrentVersion}}{{.NewVersion}}{{.Direct}}{{.ValidTimestamp}}
`) + tableTemplate, err := template.New("dependencies").Parse(tableTemplate) if err != nil { return err } diff --git a/internal/runner/templates/table.html b/internal/runner/templates/table.html new file mode 100644 index 00000000..9ddfeeb0 --- /dev/null +++ b/internal/runner/templates/table.html @@ -0,0 +1,16 @@ + + + + + + + + {{range .}} + + + + + + + {{end}} +
ModuleVersionNew VersionDirectValid Timestamps
{{.Path}}{{.CurrentVersion}}{{.NewVersion}}{{.Direct}}{{.ValidTimestamp}}
From 8c187d0ffdab9075f064b6ad1e7f9717f773758d Mon Sep 17 00:00:00 2001 From: Deseao Date: Thu, 29 Apr 2021 10:52:35 -0500 Subject: [PATCH 4/5] Update unit tests to support template file --- internal/runner/runner_test.go | 7 ++++--- internal/runner/testdata/expected_table.html | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 internal/runner/testdata/expected_table.html diff --git a/internal/runner/runner_test.go b/internal/runner/runner_test.go index 85579945..2d4fbfbd 100644 --- a/internal/runner/runner_test.go +++ b/internal/runner/runner_test.go @@ -130,9 +130,10 @@ func TestHTMLTable(t *testing.T) { if err != nil { t.Errorf("Error should be nil, got %w", err) } + expectedBytes, err := ioutil.ReadFile("testdata/expected_table.html") + expectedOutput := bytes.NewBuffer(expectedBytes) - const expectedOutput string = `
ModuleVersionNew VersionDirectValid Timestamps
github.com/mattn/go-runewidthv0.0.10v0.0.12falsetrue
` - if actualOutput.String() != expectedOutput { - t.Errorf("Expected table output to match \n%s, but got \n%s", expectedOutput, actualOutput.String()) + if actualOutput.String() != expectedOutput.String() { + t.Errorf("Expected table output to match \n%v, but got \n%v", expectedOutput, actualOutput.String()) } } diff --git a/internal/runner/testdata/expected_table.html b/internal/runner/testdata/expected_table.html new file mode 100644 index 00000000..d1080f55 --- /dev/null +++ b/internal/runner/testdata/expected_table.html @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + +
ModuleVersionNew VersionDirectValid Timestamps
github.com/mattn/go-runewidthv0.0.10v0.0.12falsetrue
From 202108276d069bdded1e05bd38564f9602a0e294 Mon Sep 17 00:00:00 2001 From: Deseao Date: Thu, 29 Apr 2021 11:26:52 -0500 Subject: [PATCH 5/5] Add some styles to the table --- internal/runner/templates/table.html | 70 ++++++++++++++++++-- internal/runner/testdata/expected_table.html | 68 +++++++++++++++++-- 2 files changed, 127 insertions(+), 11 deletions(-) diff --git a/internal/runner/templates/table.html b/internal/runner/templates/table.html index 9ddfeeb0..eaaa8c8f 100644 --- a/internal/runner/templates/table.html +++ b/internal/runner/templates/table.html @@ -1,11 +1,68 @@ + + + + + - - - - - - {{range .}} + + + + + + + + {{range .}} @@ -13,4 +70,5 @@ {{end}} +
Outdated Dependencies
ModuleVersionNew VersionDirectValid Timestamps
ModuleVersionNew VersionDirectValid Timestamps
{{.Path}} {{.CurrentVersion}}{{.Direct}} {{.ValidTimestamp}}
diff --git a/internal/runner/testdata/expected_table.html b/internal/runner/testdata/expected_table.html index d1080f55..ec3c67e8 100644 --- a/internal/runner/testdata/expected_table.html +++ b/internal/runner/testdata/expected_table.html @@ -1,11 +1,68 @@ + + + + + - - - - - + + + + + + + @@ -13,4 +70,5 @@ +
Outdated Dependencies
ModuleVersionNew VersionDirectValid TimestampsModuleVersionNew VersionDirectValid Timestamps
github.com/mattn/go-runewidth v0.0.10false true