-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
140 lines (123 loc) · 2.65 KB
/
main.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
package main
import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"sort"
"strings"
)
func getStemcellInfo(thing string) (map[string]string, error) {
parts := strings.Split(thing, "-")
if len(parts) != 2 {
return nil, errors.New("should be format: xenial-97.18")
}
resp, err := http.Get(fmt.Sprintf("https://s3.amazonaws.com/bosh-aws-light-stemcells/light-bosh-stemcell-%s-aws-xen-hvm-ubuntu-%s-go_agent.tgz", parts[1], parts[0]))
if err != nil {
return nil, err
}
defer resp.Body.Close()
gz, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
defer gz.Close()
tr := tar.NewReader(gz)
if err != nil {
return nil, err
}
for {
header, err := tr.Next()
if err != nil {
return nil, err
}
if header.Name != "packages.txt" {
continue
}
if header.Size > (10 * 1024 * 1024) {
return nil, errors.New("malformed header - packages file reported way too big")
}
packages := make([]byte, header.Size)
_, err = io.ReadFull(tr, packages)
if err != nil {
return nil, err
}
rv := make(map[string]string)
for _, line := range strings.Split(string(packages), "\n") {
if !strings.HasPrefix(line, "ii") {
continue
}
cols := strings.Fields(line)
if len(cols) < 3 {
return nil, errors.New("unrecognized row")
}
rv[cols[1]] = cols[2]
}
return rv, nil
}
}
type tableWriter struct {
rows [][]string
}
func (t *tableWriter) Write(cols []string) {
t.rows = append(t.rows, cols)
}
func (t *tableWriter) Print(w io.Writer) {
var colWidths []int
for _, r := range t.rows {
for ci, cv := range r {
for ci >= len(colWidths) {
colWidths = append(colWidths, 0)
}
if colWidths[ci] < len(cv) {
colWidths[ci] = len(cv)
}
}
}
for _, r := range t.rows {
for ci, cv := range r {
fmt.Fprintf(w, "%-*s", colWidths[ci]+2, cv)
}
fmt.Fprint(w, "\n")
}
}
func showMapDiff(nb, na string, b, a map[string]string, w io.Writer) {
keys := make(map[string]interface{})
for k := range a {
keys[k] = nil
}
for k := range b {
keys[k] = nil
}
keyList := make([]string, 0, len(keys))
for k := range keys {
keyList = append(keyList, k)
}
sort.Strings(keyList)
tw := &tableWriter{}
tw.Write([]string{"", nb, na})
for _, k := range keyList {
if a[k] != b[k] {
tw.Write([]string{k, b[k], a[k]})
}
}
tw.Print(w)
}
func main() {
if len(os.Args) != 3 {
log.Fatal("Usage: stemcell-diff xenial-97.18 xenial-97.28")
}
before, err := getStemcellInfo(os.Args[1])
if err != nil {
log.Fatal(err)
}
after, err := getStemcellInfo(os.Args[2])
if err != nil {
log.Fatal(err)
}
showMapDiff(os.Args[1], os.Args[2], before, after, os.Stdout)
}