This commit is contained in:
2021-04-09 22:01:41 -04:00
commit ece8baeaac
8 changed files with 228 additions and 0 deletions

23
pkg/cache/cache.go vendored Normal file
View File

@@ -0,0 +1,23 @@
package cache
import "git.yetaga.in/deltamualpha/libyear/pkg/libyear"
// Cache isn't concurrency-safe, but we don't care about that right now
type Cache struct {
items map[string]libyear.Info
}
func (c *Cache) Get(k string) libyear.Info {
i, ok := c.items[k]
if ok {
return i
}
return libyear.Info{}
}
func (c *Cache) Set(k string, v libyear.Info) {
if c.items == nil {
c.items = make(map[string]libyear.Info)
}
c.items[k] = v
}

35
pkg/gomod/gomod.go Normal file
View File

@@ -0,0 +1,35 @@
package gomod
import (
"os"
"git.yetaga.in/deltamualpha/libyear/pkg/libyear"
"golang.org/x/mod/modfile"
)
func LoadAndComputePairs(filename string) ([]libyear.Pair, error) {
b, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
f, err := modfile.Parse(filename, b, nil)
if err != nil {
return nil, err
}
q := Queryer{}
pairs := []libyear.Pair{}
for v := range f.Require {
if f.Require[v].Mod.Path != "" && f.Require[v].Mod.Version != "" {
latest := q.GetLatestVersion(f.Require[v].Mod.Path)
current := q.GetVersion(f.Require[v].Mod.Path, f.Require[v].Mod.Version)
pairs = append(pairs, libyear.Pair{
Name: f.Require[v].Mod.Path,
Current: current,
Latest: latest,
})
}
}
return pairs, nil
}

105
pkg/gomod/modproxy.go Normal file
View File

@@ -0,0 +1,105 @@
package gomod
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"git.yetaga.in/deltamualpha/libyear/pkg/cache"
"git.yetaga.in/deltamualpha/libyear/pkg/libyear"
)
type Queryer struct {
Root string
Client http.Client
Cache cache.Cache
}
type majorVersion struct {
raw string
version int
}
func (q *Queryer) GetLatestVersion(module string) libyear.Info {
latestMod := q.findLatestMajorVersion(module)
return q.makeProxyRequest(latestMod + "/@latest")
}
func (q *Queryer) GetVersion(module string, version string) libyear.Info {
if !strings.HasPrefix(version, "v") {
return libyear.Info{
Version: version,
}
}
i := q.makeProxyRequest(module + "/@v/" + version + ".info")
if i.Version == "" {
i.Version = version
}
return i
}
// given a module, return the "latest" major version of that module, fully defined
// i.e. github.com/foo/bar => github.com/foo/bar/v2 in the case of one version bump
// github.com/foo/baz/v2 => github.com/foo/baz/v6 in the case of a large jump
// github.com/foo/quuz/v2 => github.com/foo/quuz/v2 in the case of no jump at all!
func (q *Queryer) findLatestMajorVersion(module string) string {
for {
nextModule := incrementMajorVersion(module)
info := q.makeProxyRequest(nextModule + "/@latest")
if info.Version == "" {
return module
}
module = nextModule
}
}
func (q *Queryer) makeProxyRequest(mod string) libyear.Info {
log.Printf("makeProxyRequest for https://proxy.golang.org/%s", mod)
if q.Root == "" {
q.Root = "https://proxy.golang.org/"
}
u, _ := url.Parse(q.Root)
u.Path = mod
i := q.Cache.Get(u.String())
if i.Version != "" {
log.Printf("cache hit for https://proxy.golang.org/%s", mod)
return i
}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return i
}
req.Header.Add("Disable-Module-Fetch", "true")
resp, err := q.Client.Do(req)
if err != nil || resp.StatusCode != 200 {
return i
}
b, _ := io.ReadAll(resp.Body)
err = json.Unmarshal(b, &i)
q.Cache.Set(u.String(), i)
return i
}
// parses and attempts to add an MVS version to a module
func incrementMajorVersion(mod string) string {
exp := strings.Split(mod, "/")
var module string
if exp[len(exp)-1][0] == 'v' {
n, err := strconv.Atoi(exp[len(exp)-1][1:])
if err != nil {
module = mod + "/v2"
} else {
module = strings.Join(exp[:len(exp)-1], "/") + "/v" + fmt.Sprint(n+1)
}
} else {
module = mod + "/v2"
}
return module
}

22
pkg/libyear/libyear.go Normal file
View File

@@ -0,0 +1,22 @@
package libyear
import "time"
type Info struct {
Version string // version string
Time time.Time // commit time
}
type Pair struct {
Name string
Latest Info
Current Info
}
func Calc(p ...Pair) time.Duration {
sum := time.Duration(0)
for i := range p {
sum = sum + p[i].Latest.Time.Sub(p[i].Current.Time)
}
return sum
}