Skip to content
This repository has been archived by the owner on Nov 11, 2022. It is now read-only.

Commit

Permalink
markdown: tablewriter integration
Browse files Browse the repository at this point in the history
* olekukonko/tablewriter: render the result a bit of cleaner/clearer way.
* senseyeio/diligent: update go.mod to use upstream version.
  • Loading branch information
MarioCarrion committed Jun 21, 2020
1 parent c1dc207 commit bf0ccd6
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 188 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ issues:
- path: _test\.go
linters:
- funlen
- lll
50 changes: 34 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ versions <full path to 1 go.mod> <full path to 2 go.mod> <full path to N go.mod>

## Example

:warning: New outputs are currently in development, at the moment Flavored Markdown is the only supported.
:warning: New outputs are currently in development, at the moment Flavored Markdown is the only supported one.

Using
Using:

```
versions ~/Repositories/versions/go.mod ~/Repositories/nit/go.mod
Expand All @@ -46,24 +46,42 @@ versions ~/Repositories/versions/go.mod ~/Repositories/nit/go.mod
The following output will be generated:

```
| | github.com/MarioCarrion/nit | github.com/MarioCarrion/versions |
|--- | :---: | :---: |
| :white_check_mark: Go version| 1.13| 1.13
| github.com/golangci/golangci-lint | v1.23.3 | v1.23.2 |
| :white_check_mark: github.com/google/go-cmp | v0.2.0 | |
| :white_check_mark: github.com/pkg/errors | v0.8.1 | |
| :white_check_mark: golang.org/x/mod | | v0.2.0 |
| | github.com/MarioCarrion/nit | github.com/MarioCarrion/versions |
|--------------------------------------------------------------|-----------------------------------|--------------------------------------------------------------------------------------------------------|
| :white_check_mark: Go | 1.14 | 1.14 |
| :white_check_mark: github.com/MarioCarrion/nit | | v0.6.5 |
| :white_check_mark: github.com/go-enry/go-license-detector/v4 | | v4.0.0<br>Apache License 2.0 permissive |
| :white_check_mark: github.com/golangci/golangci-lint | v1.23.8 | v1.23.8 |
| :white_check_mark: github.com/google/go-cmp | v0.4.0<br>BSD-3-Clause permissive | v0.4.0<br>BSD-3-Clause permissive |
| :white_check_mark: github.com/olekukonko/tablewriter | | v0.0.4<br>MIT License permissive |
| :white_check_mark: github.com/pkg/errors | v0.9.1<br>BSD-2-Clause permissive | |
| :white_check_mark: github.com/senseyeio/diligent | | v0.0.0-20191014201558-431d9a760f2d github.com/MarioCarrion/diligent v0.0.0-20200617184744-03fbc970a7f7 |
| :white_check_mark: golang.org/x/mod | | v0.2.0<br>BSD-3-Clause permissive |
```

Which renders like this in Markdown

| | github.com/MarioCarrion/nit | github.com/MarioCarrion/versions |
|--- | :---: | :---: |
| :white_check_mark: Go version| 1.13| 1.13
| github.com/golangci/golangci-lint | v1.23.3 | v1.23.2 |
| :white_check_mark: github.com/google/go-cmp | v0.2.0 | |
| :white_check_mark: github.com/pkg/errors | v0.8.1 | |
| :white_check_mark: golang.org/x/mod | | v0.2.0 |
| | github.com/MarioCarrion/nit | github.com/MarioCarrion/versions |
|--------------------------------------------------------------|-----------------------------------|--------------------------------------------------------------------------------------------------------|
| :white_check_mark: Go | 1.14 | 1.14 |
| :white_check_mark: github.com/MarioCarrion/nit | | v0.6.5 |
| :white_check_mark: github.com/go-enry/go-license-detector/v4 | | v4.0.0<br>Apache License 2.0 permissive |
| :white_check_mark: github.com/golangci/golangci-lint | v1.23.8 | v1.23.8 |
| :white_check_mark: github.com/google/go-cmp | v0.4.0<br>BSD-3-Clause permissive | v0.4.0<br>BSD-3-Clause permissive |
| :white_check_mark: github.com/olekukonko/tablewriter | | v0.0.4<br>MIT License permissive |
| :white_check_mark: github.com/pkg/errors | v0.9.1<br>BSD-2-Clause permissive | |
| :white_check_mark: github.com/senseyeio/diligent | | v0.0.0-20191014201558-431d9a760f2d github.com/MarioCarrion/diligent v0.0.0-20200617184744-03fbc970a7f7 |
| :white_check_mark: golang.org/x/mod | | v0.2.0<br>BSD-3-Clause permissive |

## Features

* [X] Packages: license support.
* [ ] Packages: update availables support.
* [ ] Merge Requests creation for Gitlab.
* [ ] Pull Requests creation for Github.
* [ ] Packages: efferent and afferent metrics support.
* [ ] Output: Graphviz .
* [ ] Output: JSON.

## Development requirements

Expand Down
2 changes: 1 addition & 1 deletion cmd/versions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func main() {
os.Exit(1)
}

// md := template.NewMarkdown(gomods, template.WithPackagesSorting(template.MarkdownPackagesSortingAlphabetically))
md := markdown.NewMarkdown(gomods,
markdown.WithModulesSorting(markdown.ModulesSortingAlphabetically),
markdown.WithPackagesSorting(markdown.PackagesSortingAlphabeticallySupported),
markdown.WithPackagesLicense(true))

Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ require (
github.com/go-enry/go-license-detector/v4 v4.0.0
github.com/golangci/golangci-lint v1.23.8
github.com/google/go-cmp v0.4.0
github.com/senseyeio/diligent v0.0.0-20191014201558-431d9a760f2d
github.com/olekukonko/tablewriter v0.0.4
github.com/senseyeio/diligent v0.0.0-20200618092025-134592e3dea7
golang.org/x/mod v0.2.0
)

replace github.com/senseyeio/diligent => github.com/MarioCarrion/diligent v0.0.0-20200617184744-03fbc970a7f7
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/MarioCarrion/diligent v0.0.0-20200617184744-03fbc970a7f7 h1:nNURpLkriAFSeo4UtT5/PbK8eZGMSpiGHwgJ6LJK/NA=
github.com/MarioCarrion/diligent v0.0.0-20200617184744-03fbc970a7f7/go.mod h1:6ByW93XxJz+TbIDtCtqo729IzEO0ucg4eNblSipDuqY=
github.com/MarioCarrion/nit v0.6.5 h1:Rd2/dtg5i87jfOHzJLAfAqQm3WPf5BEWLJV97gexp8Q=
github.com/MarioCarrion/nit v0.6.5/go.mod h1:am3sU2DxXpNY7XjkVl95x9siikCzGQ/zrNW7Ao0dOcU=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down Expand Up @@ -220,6 +218,8 @@ github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaa
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand All @@ -240,6 +240,8 @@ github.com/neurosnap/sentences v1.0.6/go.mod h1:pg1IapvYpWCJJm/Etxeh0+gtMf1rI1ST
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.11.0 h1:JAKSXpt1YjtLA7YpPiqO9ss6sNXEsPfSGdwN0UHqzrw=
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down Expand Up @@ -278,6 +280,8 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/securego/gosec v0.0.0-20200103095621-79fbf3af8d83 h1:AtnWoOvTioyDXFvu96MWEeE8qj4COSQnJogzLy/u41A=
github.com/securego/gosec v0.0.0-20200103095621-79fbf3af8d83/go.mod h1:vvbZ2Ae7AzSq3/kywjUDxSNq2SJ27RxCz2un0H3ePqE=
github.com/senseyeio/diligent v0.0.0-20200618092025-134592e3dea7 h1:oB+QlIUSmpoami8aRZPFV+hbIM/RJV6s6MwVLf9rBVQ=
github.com/senseyeio/diligent v0.0.0-20200618092025-134592e3dea7/go.mod h1:6ByW93XxJz+TbIDtCtqo729IzEO0ucg4eNblSipDuqY=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada/go.mod h1:WWnYX4lzhCH5h/3YBfyVA3VbLYjlMZZAQcW9ojMexNc=
Expand Down
40 changes: 17 additions & 23 deletions markdown/header_modules.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package markdown

import (
"fmt"
"sort"
"strings"

"github.com/MarioCarrion/versions"
)
Expand Down Expand Up @@ -37,38 +37,32 @@ func newHeader(sorting ModulesSorting, same bool, values []versions.Module) head
}
}

func (h header) String() string {
var str strings.Builder
func (h header) GoVersions() []string {
res := make([]string, len(h.modules)+1)

// Modules header
var str string

str.WriteString("|")

for _, mod := range h.modules {
str.WriteString(" | ")
str.WriteString(string(mod.Name))
if h.same {
str = ":white_check_mark: "
}

str.WriteString(" |\n")

// Modules Go Versions

str.WriteString("| ")
res[0] = fmt.Sprintf("%sGo", str)

if h.same {
str.WriteString(":white_check_mark: ")
for i, mod := range h.modules {
res[i+1] = string(mod.GoVersion)
}

str.WriteString("Go")
return res
}

func (h header) Names() []string {
res := make([]string, len(h.modules)+1)

for _, mod := range h.modules {
str.WriteString(" | ")
str.WriteString(string(mod.GoVersion))
for i, mod := range h.modules {
res[i+1] = string(mod.Name)
}

str.WriteString(" |\n")

return str.String()
return res
}

func (m modules) Len() int {
Expand Down
36 changes: 25 additions & 11 deletions markdown/header_modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Test_module(t *testing.T) {

//- header

func Test_header_String(t *testing.T) {
func Test_header(t *testing.T) {
t.Parallel()

type (
Expand All @@ -80,12 +80,17 @@ func Test_header_String(t *testing.T) {
same bool
values []versions.Module
}

expected struct {
goVersions []string
names []string
}
)

tests := []struct {
name string
input input
expected string
expected expected
}{
{
"OK: ModulesSortingAsInput and Same",
Expand All @@ -112,9 +117,10 @@ func Test_header_String(t *testing.T) {
},
},
},
`| | fixture.com/new_module_replace | fixture.com/new_module_indirect | fixture.com/new_module_simple |
| :white_check_mark: Go | 1.14 | 1.14 | 1.14 |
`,
expected{
goVersions: []string{":white_check_mark: Go", "1.14", "1.14", "1.14"},
names: []string{"", "fixture.com/new_module_replace", "fixture.com/new_module_indirect", "fixture.com/new_module_simple"},
},
},
{
"OK: ModulesSortingAlphabetically and Different",
Expand All @@ -141,9 +147,10 @@ func Test_header_String(t *testing.T) {
},
},
},
`| | fixture.com/new_module_indirect | fixture.com/new_module_replace | fixture.com/new_module_simple |
| Go | 1.15 | 1.14 | 1.13 |
`,
expected{
goVersions: []string{"Go", "1.15", "1.14", "1.13"},
names: []string{"", "fixture.com/new_module_indirect", "fixture.com/new_module_replace", "fixture.com/new_module_simple"},
},
},
}

Expand All @@ -152,9 +159,16 @@ func Test_header_String(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

got := newHeader(test.input.sorting, test.input.same, test.input.values).String()
if got != test.expected {
t.Fatalf("expected %s, got %s", test.expected, got)
header := newHeader(test.input.sorting, test.input.same, test.input.values)

goVersions := header.GoVersions()
if !cmp.Equal(goVersions, test.expected.goVersions) {
t.Fatalf("expected values do not match: %s", cmp.Diff(goVersions, test.expected.goVersions))
}

names := header.Names()
if !cmp.Equal(names, test.expected.names) {
t.Fatalf("expected values do not match: %s", cmp.Diff(names, test.expected.names))
}
})
}
Expand Down
23 changes: 18 additions & 5 deletions markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
package markdown

import (
"strings"
"os"

"github.com/olekukonko/tablewriter"

"github.com/MarioCarrion/versions"
)
Expand Down Expand Up @@ -102,10 +104,21 @@ func (m Markdown) String() string {
header := newHeader(m.modulesSortBy, m.versions.GoVersions.IsSame(), mods)
pkgs := newPackages(m.versions, header.modules, m.packagesSortBy, m.packagesShowLicense)

var str strings.Builder
var data [][]string

data = append(data, header.GoVersions())
data = append(data, pkgs.Values()...)

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header.Names())
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetAutoFormatHeaders(false)
table.SetReflowDuringAutoWrap(false)
table.SetAutoWrapText(false)

str.WriteString(header.String())
str.WriteString(pkgs.String())
table.AppendBulk(data)
table.Render()

return str.String()
return ""
}
Loading

0 comments on commit bf0ccd6

Please sign in to comment.