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 diff --git a/internal/runner/runner.go b/internal/runner/runner.go index dce80eac..797e69ff 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -2,7 +2,9 @@ package runner import ( + _ "embed" "encoding/json" + "html/template" "io" "os" "strconv" @@ -15,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 @@ -28,7 +33,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 +62,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 +86,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(tableTemplate) + 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..2d4fbfbd 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,25 @@ 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) + } + expectedBytes, err := ioutil.ReadFile("testdata/expected_table.html") + expectedOutput := bytes.NewBuffer(expectedBytes) + + 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/templates/table.html b/internal/runner/templates/table.html new file mode 100644 index 00000000..eaaa8c8f --- /dev/null +++ b/internal/runner/templates/table.html @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + {{range .}} + + + + + + + {{end}} + +
Outdated Dependencies
ModuleVersionNew VersionDirectValid Timestamps
{{.Path}}{{.CurrentVersion}}{{.NewVersion}}{{.Direct}}{{.ValidTimestamp}}
diff --git a/internal/runner/testdata/expected_table.html b/internal/runner/testdata/expected_table.html new file mode 100644 index 00000000..ec3c67e8 --- /dev/null +++ b/internal/runner/testdata/expected_table.html @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + +
Outdated Dependencies
ModuleVersionNew VersionDirectValid Timestamps
github.com/mattn/go-runewidthv0.0.10v0.0.12falsetrue