-
Notifications
You must be signed in to change notification settings - Fork 3
/
output.go
149 lines (134 loc) · 3.53 KB
/
output.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package libyear
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"strconv"
"time"
"github.com/nieomylnieja/go-libyear/internal"
)
type Summary struct {
Modules []*internal.Module
Main *internal.Module
releases bool
versions bool
}
type Output interface {
Send(summary Summary) error
}
type TableOutput struct{}
func (p TableOutput) Send(summary Summary) error {
data := convertSummaryToTable(summary)
columnWidths := make([]int, len(data[0]))
for _, row := range data {
for i, cell := range row {
if len(cell) > columnWidths[i] {
columnWidths[i] = len(cell)
}
}
}
for _, row := range data {
for i, cell := range row {
if i == len(row)-1 {
fmt.Print(cell)
break
}
fmt.Printf("%-*s ", columnWidths[i], cell)
}
fmt.Println()
}
return nil
}
type CSVOutput struct{}
func (p CSVOutput) Send(summary Summary) error {
w := csv.NewWriter(os.Stdout)
return w.WriteAll(convertSummaryToTable(summary))
}
const timeFmt = time.DateOnly
func convertSummaryToTable(summary Summary) [][]string {
t := [][]string{
{"package", "version", "date", "latest", "latest_date", "libyear"},
}
if summary.releases {
t[0] = append(t[0], "releases")
}
if summary.versions {
t[0] = append(t[0], "versions")
}
addRow := func(m *internal.Module) {
row := []string{
m.Path, // 0
"", // 1
m.Time.Format(timeFmt), // 2
"", // 3
"", // 4
strconv.FormatFloat(m.Libyear, 'f', 2, 64), // 5
}
if m.Version != nil {
row[1] = m.Version.String()
}
if m.Latest != nil {
row[3] = m.Latest.Version.String()
row[4] = m.Latest.Time.Format(timeFmt)
}
if summary.releases {
row = append(row, strconv.Itoa(m.ReleasesDiff))
}
if summary.versions {
row = append(row, m.VersionsDiff.String())
}
t = append(t, row)
}
addRow(summary.Main)
for _, module := range summary.Modules {
addRow(module)
}
return t
}
type JSONOutput struct{}
type jsonSummaryModel struct {
Module string `json:"module"`
Date string `json:"date"`
Libyear float64 `json:"libyear"`
Packages []jsonPackageModel `json:"packages"`
}
type jsonPackageModel struct {
Package string `json:"package"`
Version string `json:"version"`
Date string `json:"date"`
LatestVersion string `json:"latest_version"`
LatestDate string `json:"latest_date"`
Libyear float64 `json:"libyear"`
Releases *int `json:"releases,omitempty"`
Versions *internal.VersionsDiff `json:"versions,omitempty"`
}
func (j JSONOutput) Send(summary Summary) error {
model := jsonSummaryModel{
Module: summary.Main.Path,
Date: summary.Main.Time.Format(timeFmt),
Libyear: summary.Main.Libyear,
Packages: make([]jsonPackageModel, 0, len(summary.Modules)),
}
for _, module := range summary.Modules {
m := jsonPackageModel{
Package: module.Path,
Version: module.Version.String(),
Date: module.Time.Format(timeFmt),
LatestVersion: module.Latest.Version.String(),
LatestDate: module.Latest.Time.Format(timeFmt),
Libyear: module.Libyear,
}
if summary.releases {
m.Releases = ptr(module.ReleasesDiff)
}
if summary.versions {
m.Versions = &module.VersionsDiff
}
model.Packages = append(model.Packages, m)
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(model)
}
func ptr[T any](v T) *T { return &v }