more functionality

This commit is contained in:
2021-04-10 22:14:45 -04:00
parent ece8baeaac
commit 91ff1a10d4
7 changed files with 200 additions and 15 deletions

View File

@@ -7,7 +7,27 @@ import (
"golang.org/x/mod/modfile"
)
func LoadAndComputePairs(filename string) ([]libyear.Pair, error) {
type logger interface {
Logf(f string, s ...interface{})
Debugf(f string, s ...interface{})
}
type GoMod struct {
IncludeIndirect bool
ProxyLoader Queryer
Logger logger
}
func isReplaced(module string, replaces []*modfile.Replace) bool {
for i := range replaces {
if module == replaces[i].Old.Path {
return true
}
}
return false
}
func (g *GoMod) LoadAndComputePairs(filename string) ([]libyear.Pair, error) {
b, err := os.ReadFile(filename)
if err != nil {
return nil, err
@@ -16,14 +36,20 @@ func LoadAndComputePairs(filename string) ([]libyear.Pair, error) {
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)
if isReplaced(f.Require[v].Mod.Path, f.Replace) {
g.Logger.Logf("%s is replaced, skipping...\n", f.Require[v].Mod.Path)
continue
}
if !g.IncludeIndirect && f.Require[v].Indirect {
g.Logger.Logf("%s is indirect, skipping...\n", f.Require[v].Mod.Path)
continue
}
latest := g.ProxyLoader.GetLatestVersion(f.Require[v].Mod.Path)
current := g.ProxyLoader.GetVersion(f.Require[v].Mod.Path, f.Require[v].Mod.Version)
pairs = append(pairs, libyear.Pair{
Name: f.Require[v].Mod.Path,
Current: current,

View File

@@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
@@ -18,6 +17,7 @@ type Queryer struct {
Root string
Client http.Client
Cache cache.Cache
Logger logger
}
type majorVersion struct {
@@ -59,17 +59,17 @@ func (q *Queryer) findLatestMajorVersion(module string) string {
}
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/"
}
q.Logger.Debugf("makeProxyRequest for %s/%s\n", q.Root, mod)
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)
q.Logger.Debugf("cache hit for https://proxy.golang.org/%s\n", mod)
return i
}
req, err := http.NewRequest("GET", u.String(), nil)

View File

@@ -1,6 +1,9 @@
package libyear
import "time"
import (
"fmt"
"time"
)
type Info struct {
Version string // version string
@@ -13,6 +16,8 @@ type Pair struct {
Current Info
}
// TODO: sum can only represent ~290 years before overflowing, but we don't actually need nanosecond precision!
// Probably worth switching to hour-based summing here.
func Calc(p ...Pair) time.Duration {
sum := time.Duration(0)
for i := range p {
@@ -20,3 +25,7 @@ func Calc(p ...Pair) time.Duration {
}
return sum
}
func DecimalYear(d time.Duration) string {
return fmt.Sprintf("%.2f", float64(d.Truncate(time.Hour)/time.Hour)/float64(8760))
}

View File

@@ -0,0 +1,23 @@
package libyear
import (
"testing"
"time"
)
func TestDecimalYear(t *testing.T) {
values := map[time.Duration]string{
time.Hour: "0.00",
time.Hour * 8760: "1.00",
time.Hour * 4380: "0.50",
time.Hour * 12264: "1.40",
time.Hour * 27520: "3.14",
time.Hour * 1000000: "114.16",
}
for d, s := range values {
if r := DecimalYear(d); r != s {
t.Logf("Expected %s, got %s for %d", s, r, d)
t.Fail()
}
}
}

61
pkg/logger/logger.go Normal file
View File

@@ -0,0 +1,61 @@
package logger
import (
"fmt"
"io"
"os"
)
type Log struct {
quiet bool
debug bool
errOut io.Writer
stdOut io.Writer
}
func NewLogger(quiet, debug bool, stdOut, errOut io.Writer) *Log {
if stdOut == nil {
stdOut = os.Stdout
}
if errOut == nil {
errOut = os.Stderr
}
return &Log{
quiet: quiet,
debug: debug,
stdOut: stdOut,
errOut: errOut,
}
}
func (l *Log) Error(s ...interface{}) {
fmt.Fprint(l.errOut, s...)
}
func (l *Log) Errorf(f string, s ...interface{}) {
fmt.Fprint(l.errOut, s...)
}
func (l *Log) Log(s ...interface{}) {
if !l.quiet {
fmt.Fprint(l.stdOut, s...)
}
}
func (l *Log) Logf(f string, s ...interface{}) {
if !l.quiet {
fmt.Fprintf(l.stdOut, f, s...)
}
}
func (l *Log) Debug(s ...interface{}) {
if l.debug {
fmt.Fprint(l.errOut, s...)
}
}
func (l *Log) Debugf(f string, s ...interface{}) {
if l.debug {
fmt.Fprintf(l.errOut, f, s...)
}
}