Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit d7ebf6d

Browse files
committed
Fixed file diff/analysis output
1 parent 1f73b93 commit d7ebf6d

File tree

7 files changed

+127
-32
lines changed

7 files changed

+127
-32
lines changed

differs/aptDiff.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"os"
66
"path/filepath"
7+
"strconv"
78
"strings"
89

910
"github.com/GoogleCloudPlatform/container-diff/utils"
@@ -81,7 +82,15 @@ func parseLine(text string, currPackage string, packages map[string]utils.Packag
8182
if !ok {
8283
currPackageInfo = utils.PackageInfo{}
8384
}
84-
currPackageInfo.Size = value
85+
var size int64
86+
var err error
87+
size, err = strconv.ParseInt(value, 10, 64)
88+
if err != nil {
89+
glog.Errorf("Could not get size for %s: %s", currPackage, err)
90+
size = -1
91+
}
92+
// Installed-Size is in KB, so we convert it to bytes to keep consistent with the tool's size units
93+
currPackageInfo.Size = size * 1024
8594
packages[currPackage] = currPackageInfo
8695
return currPackage
8796
default:

differs/nodeDiff.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"io/ioutil"
66
"os"
77
"path/filepath"
8-
"strconv"
98
"strings"
109

1110
"github.com/GoogleCloudPlatform/container-diff/utils"
@@ -55,8 +54,7 @@ func (a NodeAnalyzer) getPackages(image utils.Image) (map[string]map[string]util
5554
var currInfo utils.PackageInfo
5655
currInfo.Version = packageJSON.Version
5756
packagePath := strings.TrimSuffix(currPackage, "package.json")
58-
size := utils.GetSize(packagePath)
59-
currInfo.Size = strconv.FormatInt(size, 10)
57+
currInfo.Size = utils.GetSize(packagePath)
6058
mapPath := strings.Replace(packagePath, path, "", 1)
6159
// Check if other package version already recorded
6260
if _, ok := packages[packageJSON.Name]; !ok {

differs/pipDiff.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"path/filepath"
66
"reflect"
77
"regexp"
8-
"strconv"
98
"strings"
109

1110
"github.com/GoogleCloudPlatform/container-diff/utils"
@@ -66,13 +65,12 @@ func (a PipAnalyzer) getPackages(image utils.Image) (map[string]map[string]utils
6665

6766
// Retrieves size for actual package/script corresponding to each dist-info metadata directory
6867
// by taking the file entry alphabetically before it (for a package) or after it (for a script)
69-
var size string
68+
var size int64
7069
if i-1 >= 0 && contents[i-1].Name() == packageName {
7170
packagePath := filepath.Join(pythonPath, packageName)
72-
intSize := utils.GetSize(packagePath)
73-
size = strconv.FormatInt(intSize, 10)
71+
size = utils.GetSize(packagePath)
7472
} else if i+1 < len(contents) && contents[i+1].Name() == packageName+".py" {
75-
size = strconv.FormatInt(contents[i+1].Size(), 10)
73+
size = contents[i+1].Size()
7674

7775
} else {
7876
glog.Errorf("Could not find Python package %s for corresponding metadata info", packageName)

utils/analyze_output_utils.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package utils
22

3+
import (
4+
"code.cloudfoundry.org/bytefmt"
5+
)
6+
37
type AnalyzeResult interface {
48
GetStruct() AnalyzeResult
59
OutputText(analyzeType string) error
@@ -16,7 +20,7 @@ func (r ListAnalyzeResult) GetStruct() AnalyzeResult {
1620
}
1721

1822
func (r ListAnalyzeResult) OutputText(analyzeType string) error {
19-
return TemplateOutput(r)
23+
return TemplateOutput(r, "ListAnalyze")
2024
}
2125

2226
type MultiVersionPackageAnalyzeResult struct {
@@ -30,7 +34,7 @@ func (r MultiVersionPackageAnalyzeResult) GetStruct() AnalyzeResult {
3034
}
3135

3236
func (r MultiVersionPackageAnalyzeResult) OutputText(analyzeType string) error {
33-
return TemplateOutput(r)
37+
return TemplateOutput(r, "MultiVersionPackageAnalyze")
3438
}
3539

3640
type SingleVersionPackageAnalyzeResult struct {
@@ -44,7 +48,7 @@ func (r SingleVersionPackageAnalyzeResult) GetStruct() AnalyzeResult {
4448
}
4549

4650
func (r SingleVersionPackageAnalyzeResult) OutputText(diffType string) error {
47-
return TemplateOutput(r)
51+
return TemplateOutput(r, "SingleVersionPackageAnalyze")
4852
}
4953

5054
type FileAnalyzeResult struct {
@@ -58,5 +62,36 @@ func (r FileAnalyzeResult) GetStruct() AnalyzeResult {
5862
}
5963

6064
func (r FileAnalyzeResult) OutputText(analyzeType string) error {
61-
return TemplateOutput(r)
65+
analysis := r.Analysis
66+
strAnalysis := stringifyDirectoryEntries(analysis)
67+
68+
strResult := struct {
69+
Image string
70+
AnalyzeType string
71+
Analysis []StrDirectoryEntry
72+
}{
73+
Image: r.Image,
74+
AnalyzeType: r.AnalyzeType,
75+
Analysis: strAnalysis,
76+
}
77+
return TemplateOutput(strResult, "FileAnalyze")
78+
}
79+
80+
type StrDirectoryEntry struct {
81+
Name string
82+
Size string
83+
}
84+
85+
func stringifyDirectoryEntries(entries []DirectoryEntry) (strEntries []StrDirectoryEntry) {
86+
for _, entry := range entries {
87+
size := entry.Size
88+
strSize := "unknown"
89+
if size != -1 {
90+
strSize = bytefmt.ByteSize(uint64(size))
91+
}
92+
93+
strEntry := StrDirectoryEntry{Name: entry.Name, Size: strSize}
94+
strEntries = append(strEntries, strEntry)
95+
}
96+
return
6297
}

utils/diff_output_utils.go

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package utils
22

3+
import (
4+
"code.cloudfoundry.org/bytefmt"
5+
)
6+
37
type DiffResult interface {
48
GetStruct() DiffResult
59
OutputText(diffType string) error
@@ -17,7 +21,7 @@ func (r MultiVersionPackageDiffResult) GetStruct() DiffResult {
1721
}
1822

1923
func (r MultiVersionPackageDiffResult) OutputText(diffType string) error {
20-
return TemplateOutput(r)
24+
return TemplateOutput(r, "MultiVersionPackageDiff")
2125
}
2226

2327
type SingleVersionPackageDiffResult struct {
@@ -32,7 +36,7 @@ func (r SingleVersionPackageDiffResult) GetStruct() DiffResult {
3236
}
3337

3438
func (r SingleVersionPackageDiffResult) OutputText(diffType string) error {
35-
return TemplateOutput(r)
39+
return TemplateOutput(r, "SingleVersionPackageDiff")
3640
}
3741

3842
type HistDiffResult struct {
@@ -47,7 +51,7 @@ func (r HistDiffResult) GetStruct() DiffResult {
4751
}
4852

4953
func (r HistDiffResult) OutputText(diffType string) error {
50-
return TemplateOutput(r)
54+
return TemplateOutput(r, "HistDiff")
5155
}
5256

5357
type DirDiffResult struct {
@@ -62,5 +66,58 @@ func (r DirDiffResult) GetStruct() DiffResult {
6266
}
6367

6468
func (r DirDiffResult) OutputText(diffType string) error {
65-
return TemplateOutput(r)
69+
diff := r.Diff
70+
71+
strAdds := stringifyDirectoryEntries(diff.Adds)
72+
strDels := stringifyDirectoryEntries(diff.Dels)
73+
strMods := stringifyEntryDiffs(diff.Mods)
74+
75+
type StrDiff struct {
76+
Adds []StrDirectoryEntry
77+
Dels []StrDirectoryEntry
78+
Mods []StrEntryDiff
79+
}
80+
81+
strResult := struct {
82+
Image1 string
83+
Image2 string
84+
DiffType string
85+
Diff StrDiff
86+
}{
87+
Image1: r.Image1,
88+
Image2: r.Image2,
89+
DiffType: r.DiffType,
90+
Diff: StrDiff{
91+
Adds: strAdds,
92+
Dels: strDels,
93+
Mods: strMods,
94+
},
95+
}
96+
return TemplateOutput(strResult, "DirDiff")
97+
}
98+
99+
type StrEntryDiff struct {
100+
Name string
101+
Size1 string
102+
Size2 string
103+
}
104+
105+
func stringifyEntryDiffs(entries []EntryDiff) (strEntries []StrEntryDiff) {
106+
for _, entry := range entries {
107+
size1 := entry.Size1
108+
strSize1 := "unknown"
109+
if size1 != -1 {
110+
strSize1 = bytefmt.ByteSize(uint64(size1))
111+
}
112+
113+
size2 := entry.Size2
114+
strSize2 := "unknown"
115+
if size2 != -1 {
116+
strSize2 = bytefmt.ByteSize(uint64(size2))
117+
}
118+
119+
strEntry := StrEntryDiff{Name: entry.Name, Size1: strSize1, Size2: strSize2}
120+
strEntries = append(strEntries, strEntry)
121+
}
122+
return
66123
}

utils/format_utils.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@ import (
66
"errors"
77
"html/template"
88
"os"
9-
"reflect"
109
"strings"
1110
"text/tabwriter"
1211

1312
"github.com/golang/glog"
1413
)
1514

1615
var templates = map[string]string{
17-
"utils.SingleVersionPackageDiffResult": SingleVersionDiffOutput,
18-
"utils.MultiVersionPackageDiffResult": MultiVersionDiffOutput,
19-
"utils.HistDiffResult": HistoryDiffOutput,
20-
"utils.DirDiffResult": FSDiffOutput,
21-
"utils.ListAnalyzeResult": ListAnalysisOutput,
22-
"utils.FileAnalyzeResult": FileAnalysisOutput,
23-
"utils.MultiVersionPackageAnalyzeResult": MultiVersionPackageOutput,
24-
"utils.SingleVersionPackageAnalyzeResult": SingleVersionPackageOutput,
16+
"SingleVersionPackageDiff": SingleVersionDiffOutput,
17+
"MultiVersionPackageDiff": MultiVersionDiffOutput,
18+
"HistDiff": HistoryDiffOutput,
19+
"DirDiff": FSDiffOutput,
20+
"ListAnalyze": ListAnalysisOutput,
21+
"FileAnalyze": FileAnalysisOutput,
22+
"MultiVersionPackageAnalyze": MultiVersionPackageOutput,
23+
"SingleVersionPackageAnalyze": SingleVersionPackageOutput,
2524
}
2625

2726
func JSONify(diff interface{}) error {
@@ -35,16 +34,15 @@ func JSONify(diff interface{}) error {
3534
return nil
3635
}
3736

38-
func getTemplate(diff interface{}) (string, error) {
39-
diffType := reflect.TypeOf(diff).String()
40-
if template, ok := templates[diffType]; ok {
37+
func getTemplate(templateType string) (string, error) {
38+
if template, ok := templates[templateType]; ok {
4139
return template, nil
4240
}
4341
return "", errors.New("No available template")
4442
}
4543

46-
func TemplateOutput(diff interface{}) error {
47-
outputTmpl, err := getTemplate(diff)
44+
func TemplateOutput(diff interface{}, templateType string) error {
45+
outputTmpl, err := getTemplate(templateType)
4846
if err != nil {
4947
glog.Error(err)
5048

utils/package_diff_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type Info struct {
4040
// PackageInfo stores the specific metadata about a package.
4141
type PackageInfo struct {
4242
Version string
43-
Size string
43+
Size int64
4444
}
4545

4646
func multiVersionDiff(infoDiff []MultiVersionInfo, packageName string, map1, map2 map[string]PackageInfo) []MultiVersionInfo {

0 commit comments

Comments
 (0)