Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for html table formatting #47

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
46 changes: 44 additions & 2 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
package runner

import (
_ "embed"
"encoding/json"
"html/template"
"io"
"os"
"strconv"
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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"})

Expand All @@ -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
}
23 changes: 23 additions & 0 deletions internal/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"testing"

"github.com/psampaz/go-mod-outdated/internal/mod"
"github.com/psampaz/go-mod-outdated/internal/runner"
)

Expand Down Expand Up @@ -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())
}
}
74 changes: 74 additions & 0 deletions internal/runner/templates/table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Raleway:wght@300&display=swap" rel="stylesheet">
<style type="text/css">
table {
table-layout: fixed;
width: 100%;
border-collapse: collapse;
border: 3px solid #85A7C4;
}
thead th:nth-child(1) {
width: 30%;
}
thead th:nth-child(2) {
width: 20%;
}
thead th:nth-child(3) {
width: 20%;
}
thead th:nth-child(4) {
width: 15%;
}
thead th:nth-child(5) {
width: 15%;
}
th, td {
padding: 20px;
}
html {
font-family: 'helvetica neue', helvetica, arial, sans-serif;
}
thead th, tfoot th {
font-family: 'Raleway', cursive;
}
th {
letter-spacing: 2px;
}
td {
letter-spacing: 1px;
border-bottom: 2px solid black;
}
tbody td {
text-align: center;
}
tfoot td {
text-align: right;
}
tbody tr:nth-child(odd) {
background-color: #70D9A1;
}
tbody tr:nth-child(even) {
background-color: #70D9D6;
}
</style>
<table>
<caption>Outdated Dependencies</caption>
<thead>
<tr>
<th scope="col" >Module</th>
<th scope="col" >Version</th>
<th scope="col" >New Version</th>
<th scope="col" >Direct</th>
<th scope="col" >Valid Timestamps</th>
</tr>
</thead>
<tbody>{{range .}}
<tr>
<td>{{.Path}}</td>
<td>{{.CurrentVersion}}</td>
<td>{{.NewVersion}}</td>
<td>{{.Direct}}</td>
<td>{{.ValidTimestamp}}</td>
</tr>{{end}}
</tbody>
</table>
74 changes: 74 additions & 0 deletions internal/runner/testdata/expected_table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Raleway:wght@300&display=swap" rel="stylesheet">
<style type="text/css">
table {
table-layout: fixed;
width: 100%;
border-collapse: collapse;
border: 3px solid #85A7C4;
}
thead th:nth-child(1) {
width: 30%;
}
thead th:nth-child(2) {
width: 20%;
}
thead th:nth-child(3) {
width: 20%;
}
thead th:nth-child(4) {
width: 15%;
}
thead th:nth-child(5) {
width: 15%;
}
th, td {
padding: 20px;
}
html {
font-family: 'helvetica neue', helvetica, arial, sans-serif;
}
thead th, tfoot th {
font-family: 'Raleway', cursive;
}
th {
letter-spacing: 2px;
}
td {
letter-spacing: 1px;
border-bottom: 2px solid black;
}
tbody td {
text-align: center;
}
tfoot td {
text-align: right;
}
tbody tr:nth-child(odd) {
background-color: #70D9A1;
}
tbody tr:nth-child(even) {
background-color: #70D9D6;
}
</style>
<table>
<caption>Outdated Dependencies</caption>
<thead>
<tr>
<th scope="col" >Module</th>
<th scope="col" >Version</th>
<th scope="col" >New Version</th>
<th scope="col" >Direct</th>
<th scope="col" >Valid Timestamps</th>
</tr>
</thead>
<tbody>
<tr>
<td>github.com/mattn/go-runewidth</td>
<td>v0.0.10</td>
<td>v0.0.12</td>
<td>false</td>
<td>true</td>
</tr>
</tbody>
</table>