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

Commit

Permalink
Proof of concept!
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioCarrion committed Feb 5, 2020
1 parent 9f53540 commit 95055f0
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/MarioCarrion/versions

go 1.13

require golang.org/x/mod v0.2.0
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/mod v0.2.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 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
164 changes: 164 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"sort"

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

type (
Table struct {
Modules []string
Versions []string
Packages map[string][]Module // key is "Package Name"
}

Module struct {
// Path string
Version string
IsIndirect bool
ReplacedPath string
ReplacedVersion string
}
)

func main() {
parse := func(file string) *modfile.File {
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalf("ioutil %s", err)
}

f, err := modfile.Parse(file, data, nil)
if err != nil {
log.Fatalf("log %s", err)
}
return f
}

files := []string{
"repo1/go.mod",
"repo2/go.mod",
"repo2/go.mod",
}

sort.Strings(files)

parsed := make([]*modfile.File, len(files))
for i, f := range files {
parsed[i] = parse(f)
}

table := Table{}
table.Modules = make([]string, len(files))
table.Versions = make([]string, len(files))
table.Packages = make(map[string][]Module)

//-

for i, file := range parsed {
table.Modules[i] = file.Module.Mod.Path
table.Versions[i] = file.Go.Version

for _, req := range file.Require {
pkg := req.Mod.Path

var modules []Module
if modules = table.Packages[pkg]; modules == nil {
modules = make([]Module, len(parsed))
}

modules[i].Version = req.Mod.Version
modules[i].IsIndirect = req.Indirect

table.Packages[pkg] = modules
}

for _, rep := range file.Replace {
old, ok := table.Packages[rep.Old.Path]
if !ok {
continue
}

old[i].ReplacedPath = rep.New.Path
old[i].ReplacedVersion = rep.New.Version
table.Packages[rep.Old.Path] = old
}
}

//-

line0 := "|"
line1 := "|---"
line2 := "| Go version |"

for i, name := range table.Modules {
line0 = fmt.Sprintf("%s | %s ", line0, string(name))
line1 = fmt.Sprintf("%s | :---: ", line1)
line2 = fmt.Sprintf("%s | %s ", line2, table.Versions[i])
}

fmt.Printf("%s |\n", line0)
fmt.Printf("%s |\n", line1)
fmt.Printf("%s |\n", line2)

sortedpkgs := make([]string, len(table.Packages))

var index int
for k, _ := range table.Packages {
sortedpkgs[index] = k
index++
}

sort.Strings(sortedpkgs)

//-

for _, pkg := range sortedpkgs {
v := table.Packages[pkg]

var line, lastversion string

same := true

for _, p := range v {
var version string
if p.ReplacedVersion != "" {
version = p.ReplacedVersion
} else {
version = p.Version
}

if lastversion == "" {
lastversion = version
}

if same && lastversion != version {
same = false
}

line = fmt.Sprintf("%s %s ", line, version)
if p.IsIndirect {
line = fmt.Sprintf("%s :question: ", line)
}
if p.ReplacedVersion != "" {
line = fmt.Sprintf("%s :exclamation: ", line)
}

line = fmt.Sprintf("%s | ", line)
}

var prefix string
if same {
prefix = fmt.Sprintf(":white_check_mark: %s", pkg)
} else {
prefix = pkg
}

line = fmt.Sprintf("| %s | %s |\n", prefix, line)
fmt.Printf(line)
}
}

0 comments on commit 95055f0

Please # to comment.