Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasepe committed Sep 12, 2020
0 parents commit c47a7f3
Show file tree
Hide file tree
Showing 15 changed files with 576 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .gitignore
@@ -0,0 +1,39 @@
## Intellij
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/encodings.xml
.idea/**/compiler.xml
.idea/**/misc.xml
.idea/**/modules.xml
.idea/**/vcs.xml

## VSCode
.vscode/

## File-based project format:
*.iws
*.iml
.idea/

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
*.dat
*.DS_Store

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Goreleaser builds
**/dist/**

# This is my wip ideas folder
experiments/**

banner.jpg
27 changes: 27 additions & 0 deletions LICENSE
@@ -0,0 +1,27 @@
Copyright (c) 2020, Luca Sepe
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 changes: 27 additions & 0 deletions README.md
@@ -0,0 +1,27 @@
# modgv

Converts 'go mod graph' output into Graphviz's DOT language.

- takes no options or arguments
- it reads the output generated by “go mod graph” on stdin
- generates a DOT language and writes to stdout

## Usage:

```bash
go mod graph | modgv | dot -Tpng -o graph.png
```

For each module:
- the node representing the greatest version (i.e., the version chosen by Go's MVS algorithm) is colored green
- other nodes, which aren't in the final build list, are colored grey

## Installation

```bash
go get github.com/lucasepe/modgv/modgv
```

## Sample output

![](./graph.png)
5 changes: 5 additions & 0 deletions go.mod
@@ -0,0 +1,5 @@
module github.com/lucasepe/modgv

go 1.14

require golang.org/x/mod v0.3.0
13 changes: 13 additions & 0 deletions go.sum
@@ -0,0 +1,13 @@
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
92 changes: 92 additions & 0 deletions graph.go
@@ -0,0 +1,92 @@
// This is a modified version of Modgraphviz created by the Go authors.
// Original Modgraphviz resides in the experimental repository.
// https://github.com/golang/exp/tree/master/cmd/modgraphviz

package modgv

import (
"bufio"
"fmt"
"io"
"sort"
"strings"

"golang.org/x/mod/semver"
)

type edge struct{ from, to string }
type graph struct {
root string
edges []edge
mvsPicked []string
mvsUnpicked []string
}

// convert reads “go mod graph” output from r and returns a graph, recording
// MVS picked and unpicked nodes along the way.
func convert(r io.Reader) (*graph, error) {
scanner := bufio.NewScanner(r)
var g graph
seen := map[string]bool{}
mvsPicked := map[string]string{} // module name -> module version

for scanner.Scan() {
l := scanner.Text()
if l == "" {
continue
}

parts := strings.Fields(l)
if len(parts) != 2 {
return nil, fmt.Errorf("expected 2 words in line, but got %d: %s", len(parts), l)
}

from := parts[0]
to := parts[1]
g.edges = append(g.edges, edge{from: from, to: to})

for _, node := range []string{from, to} {
if _, ok := seen[node]; ok {
// Skip over nodes we've already seen.
continue
}
seen[node] = true

var m, v string
if i := strings.IndexByte(node, '@'); i >= 0 {
m, v = node[:i], node[i+1:]
} else {
// Root node doesn't have a version.
g.root = node
continue
}

if maxV, ok := mvsPicked[m]; ok {
if semver.Compare(maxV, v) < 0 {
// This version is higher - replace it and consign the old
// max to the unpicked list.
g.mvsUnpicked = append(g.mvsUnpicked, m+"@"+maxV)
mvsPicked[m] = v
} else {
// Other version is higher - stick this version in the
// unpicked list.
g.mvsUnpicked = append(g.mvsUnpicked, node)
}
} else {
mvsPicked[m] = v
}
}
}
if err := scanner.Err(); err != nil {
return nil, err
}

for m, v := range mvsPicked {
g.mvsPicked = append(g.mvsPicked, m+"@"+v)
}

// Make this function deterministic.
sort.Strings(g.mvsPicked)

return &g, nil
}
Binary file added graph.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions graph_test.go
@@ -0,0 +1,86 @@
package modgv

import (
"bytes"
"fmt"
"reflect"
"testing"
)

func TestMVSPicking(t *testing.T) {
for _, tc := range []struct {
name string
in []string
wantPicked []string
wantUnpicked []string
}{
{
name: "single node",
in: []string{"foo@v0.0.1"},
wantPicked: []string{"foo@v0.0.1"},
wantUnpicked: nil,
},
{
name: "duplicate same node",
in: []string{"foo@v0.0.1", "foo@v0.0.1"},
wantPicked: []string{"foo@v0.0.1"},
wantUnpicked: nil,
},
{
name: "multiple semver - same major",
in: []string{"foo@v1.0.0", "foo@v1.3.7", "foo@v1.2.0", "foo@v1.0.1"},
wantPicked: []string{"foo@v1.3.7"},
wantUnpicked: []string{"foo@v1.0.0", "foo@v1.2.0", "foo@v1.0.1"},
},
{
name: "multiple semver - multiple major",
in: []string{"foo@v1.0.0", "foo@v1.3.7", "foo/v2@v2.2.0", "foo/v2@v2.0.1", "foo@v1.1.1"},
wantPicked: []string{"foo/v2@v2.2.0", "foo@v1.3.7"},
wantUnpicked: []string{"foo@v1.0.0", "foo/v2@v2.0.1", "foo@v1.1.1"},
},
{
name: "semver and pseudo version",
in: []string{"foo@v1.0.0", "foo@v1.3.7", "foo/v2@v2.2.0", "foo/v2@v2.0.1", "foo@v1.1.1", "foo@v0.0.0-20190311183353-d8887717615a"},
wantPicked: []string{"foo/v2@v2.2.0", "foo@v1.3.7"},
wantUnpicked: []string{"foo@v1.0.0", "foo/v2@v2.0.1", "foo@v1.1.1", "foo@v0.0.0-20190311183353-d8887717615a"},
},
{
name: "multiple pseudo version",
in: []string{
"foo@v0.0.0-20190311183353-d8887717615a",
"foo@v0.0.0-20190227222117-0694c2d4d067",
"foo@v0.0.0-20190312151545-0bb0c0a6e846",
},
wantPicked: []string{"foo@v0.0.0-20190312151545-0bb0c0a6e846"},
wantUnpicked: []string{
"foo@v0.0.0-20190227222117-0694c2d4d067",
"foo@v0.0.0-20190311183353-d8887717615a",
},
},
{
name: "semver and suffix",
in: []string{"foo@v1.0.0", "foo@v1.3.8-rc1", "foo@v1.3.7"},
wantPicked: []string{"foo@v1.3.8-rc1"},
wantUnpicked: []string{"foo@v1.0.0", "foo@v1.3.7"},
},
} {
t.Run(tc.name, func(t *testing.T) {
buf := bytes.Buffer{}
for _, node := range tc.in {
fmt.Fprintf(&buf, "A %s\n", node)
}

g, err := convert(&buf)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(g.mvsPicked, tc.wantPicked) {
t.Fatalf("picked: got %v, want %v", g.mvsPicked, tc.wantPicked)
}
if !reflect.DeepEqual(g.mvsUnpicked, tc.wantUnpicked) {
t.Fatalf("unpicked: got %v, want %v", g.mvsUnpicked, tc.wantUnpicked)
}
})
}
}
7 changes: 7 additions & 0 deletions modgv/go.mod
@@ -0,0 +1,7 @@
module github.com/lucasepe/modgv/modgv

go 1.14

require github.com/lucasepe/modgv v0.1.0

replace github.com/lucasepe/modgv => ../
13 changes: 13 additions & 0 deletions modgv/go.sum
@@ -0,0 +1,13 @@
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit c47a7f3

Please sign in to comment.