-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
112 lines (92 loc) · 2.37 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"errors"
"fmt"
"sort"
"github.com/Necoro/arch-log/pkg/entries"
"github.com/Necoro/arch-log/pkg/log"
"github.com/Necoro/arch-log/pkg/provider/arch"
"github.com/Necoro/arch-log/pkg/provider/aur"
)
func maxLength(f func(entries.Change) string) func(changes []entries.Change) int {
return func(changes []entries.Change) int {
max := 0
for _, e := range changes {
if max < len(f(e)) {
max = len(f(e))
}
}
return max
}
}
var maxTagLength = maxLength(func(change entries.Change) string {
return change.Tag
})
var maxRepoLength = maxLength(func(change entries.Change) string {
return change.RepoInfo
})
func formatEntryList(changes []entries.Change) {
log.Debugf("Received entries: %+v", changes)
sort.SliceStable(changes, func(i, j int) bool {
return timeLess(changes[i].CommitTime, changes[j].CommitTime)
})
if len(changes) > options.number {
if options.reverse {
changes = changes[:options.number]
} else {
rest := len(changes) - options.number
changes = changes[rest:]
}
}
maxTL := maxTagLength(changes)
maxRL := maxRepoLength(changes)
for _, c := range changes {
if !options.longLog {
fmt.Println(c.ShortFormat(maxTL, maxRL))
} else {
fmt.Println(c.Format())
fmt.Println("--------------")
}
}
}
func handleEntries(what string, pkg string, repo string, f func(string, string) ([]entries.Change, error)) (bool, error) {
log.Debug("Checking ", what)
if changes, err := f(pkg, repo); err == nil {
formatEntryList(changes)
return false, nil
} else if errors.Is(err, entries.ErrNotFound) {
log.Debug("Not found on ", what)
return true, nil
} else {
return false, fmt.Errorf("error fetching from %s: %w", what, err)
}
}
func notFoundError(pkg string) error {
var msg string
switch {
case options.aur:
msg = "could not be found on AUR"
case options.arch:
msg = "could not be found on Arch"
default:
msg = "could neither be found on Arch nor AUR"
}
return fmt.Errorf("package '%s' %s", pkg, msg)
}
func fetchLog(pkg string) (err error) {
var notfound bool
if !options.aur {
if notfound, err = handleEntries("Arch", pkg, options.repo, arch.GetEntries); err != nil {
return
}
}
if options.aur || (notfound && !options.arch) {
if notfound, err = handleEntries("AUR", pkg, options.repo, aur.GetEntries); err != nil {
return
}
}
if notfound {
return notFoundError(pkg)
}
return nil
}