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

Commit f1ef660

Browse files
authored
Merge pull request #84 from nkubala/repackage
Export container-diff tar/fs util methods for vendoring
2 parents 9826665 + f3d55c1 commit f1ef660

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+588
-561
lines changed

cmd/analyze.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"strings"
2424

2525
"github.com/GoogleCloudPlatform/container-diff/differs"
26-
"github.com/GoogleCloudPlatform/container-diff/utils"
26+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2727
"github.com/golang/glog"
2828
"github.com/spf13/cobra"
2929
)
@@ -62,14 +62,14 @@ func analyzeImage(imageArg string, analyzerArgs []string) error {
6262
return fmt.Errorf("Error getting docker client for differ: %s", err)
6363
}
6464
defer cli.Close()
65-
ip := utils.ImagePrepper{
65+
ip := pkgutil.ImagePrepper{
6666
Source: imageArg,
6767
Client: cli,
6868
}
6969
image, err := ip.GetImage()
7070

7171
if !save {
72-
defer cleanupImage(image)
72+
defer pkgutil.CleanupImage(image)
7373
}
7474
if err != nil {
7575
glog.Error(err.Error())

cmd/diff.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"sync"
2525

2626
"github.com/GoogleCloudPlatform/container-diff/differs"
27-
"github.com/GoogleCloudPlatform/container-diff/utils"
27+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2828
"github.com/golang/glog"
2929
"github.com/spf13/cobra"
3030
)
@@ -68,29 +68,29 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
6868

6969
glog.Infof("Starting diff on images %s and %s, using differs: %s", image1Arg, image2Arg, diffArgs)
7070

71-
imageMap := map[string]*utils.Image{
71+
imageMap := map[string]*pkgutil.Image{
7272
image1Arg: {},
7373
image2Arg: {},
7474
}
7575
for imageArg := range imageMap {
76-
go func(imageName string, imageMap map[string]*utils.Image) {
76+
go func(imageName string, imageMap map[string]*pkgutil.Image) {
7777
defer wg.Done()
78-
ip := utils.ImagePrepper{
78+
ip := pkgutil.ImagePrepper{
7979
Source: imageName,
8080
Client: cli,
8181
}
8282
image, err := ip.GetImage()
8383
imageMap[imageName] = &image
8484
if err != nil {
85-
glog.Error(err.Error())
85+
glog.Errorf("Diff may be inaccurate: %s", err.Error())
8686
}
8787
}(imageArg, imageMap)
8888
}
8989
wg.Wait()
9090

9191
if !save {
92-
defer cleanupImage(*imageMap[image1Arg])
93-
defer cleanupImage(*imageMap[image2Arg])
92+
defer pkgutil.CleanupImage(*imageMap[image1Arg])
93+
defer pkgutil.CleanupImage(*imageMap[image2Arg])
9494
}
9595

9696
diffTypes, err := differs.GetAnalyzers(diffArgs)

cmd/root.go

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import (
2020
"context"
2121
goflag "flag"
2222
"fmt"
23-
"os"
2423
"sort"
2524
"strings"
2625

2726
"github.com/GoogleCloudPlatform/container-diff/differs"
28-
"github.com/GoogleCloudPlatform/container-diff/utils"
27+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
28+
"github.com/GoogleCloudPlatform/container-diff/util"
2929
"github.com/docker/docker/client"
3030
"github.com/golang/glog"
3131
"github.com/pkg/errors"
@@ -55,7 +55,7 @@ func NewClient() (*client.Client, error) {
5555
return cli, nil
5656
}
5757

58-
func outputResults(resultMap map[string]utils.Result) {
58+
func outputResults(resultMap map[string]util.Result) {
5959
// Outputs diff/analysis results in alphabetical order by analyzer name
6060
sortedTypes := []string{}
6161
for analyzerType := range resultMap {
@@ -76,23 +76,13 @@ func outputResults(resultMap map[string]utils.Result) {
7676
}
7777
}
7878
if json {
79-
err := utils.JSONify(results)
79+
err := util.JSONify(results)
8080
if err != nil {
8181
glog.Error(err)
8282
}
8383
}
8484
}
8585

86-
func cleanupImage(image utils.Image) {
87-
if image.FSPath != "" {
88-
glog.Infof("Removing image filesystem directory %s from system", image.FSPath)
89-
errMsg := remove(image.FSPath, true)
90-
if errMsg != "" {
91-
glog.Error(errMsg)
92-
}
93-
}
94-
}
95-
9686
func validateArgs(args []string, validatefxns ...validatefxn) error {
9787
for _, validatefxn := range validatefxns {
9888
if err := validatefxn(args); err != nil {
@@ -103,7 +93,7 @@ func validateArgs(args []string, validatefxns ...validatefxn) error {
10393
}
10494

10595
func checkImage(arg string) bool {
106-
if !utils.CheckImageID(arg) && !utils.CheckImageURL(arg) && !utils.CheckTar(arg) {
96+
if !pkgutil.CheckImageID(arg) && !pkgutil.CheckImageURL(arg) && !pkgutil.CheckTar(arg) {
10797
return false
10898
}
10999
return true
@@ -135,24 +125,6 @@ func checkIfValidAnalyzer(flagtypes string) error {
135125
return nil
136126
}
137127

138-
func remove(path string, dir bool) string {
139-
var errStr string
140-
if path == "" {
141-
return ""
142-
}
143-
144-
var err error
145-
if dir {
146-
err = os.RemoveAll(path)
147-
} else {
148-
err = os.Remove(path)
149-
}
150-
if err != nil {
151-
errStr = "\nUnable to remove " + path
152-
}
153-
return errStr
154-
}
155-
156128
func init() {
157129
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
158130
}
@@ -161,5 +133,5 @@ func addSharedFlags(cmd *cobra.Command) {
161133
cmd.Flags().BoolVarP(&json, "json", "j", false, "JSON Output defines if the diff should be returned in a human readable format (false) or a JSON (true).")
162134
cmd.Flags().StringVarP(&types, "types", "t", "", "This flag sets the list of analyzer types to use. It expects a comma separated list of supported analyzers.")
163135
cmd.Flags().BoolVarP(&save, "save", "s", false, "Set this flag to save rather than remove the final image filesystems on exit.")
164-
cmd.Flags().BoolVarP(&utils.SortSize, "order", "o", false, "Set this flag to sort any file/package results by descending size. Otherwise, they will be sorted by name.")
136+
cmd.Flags().BoolVarP(&util.SortSize, "order", "o", false, "Set this flag to sort any file/package results by descending size. Otherwise, they will be sorted by name.")
165137
}

differs/apt_diff.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
"strconv"
2424
"strings"
2525

26-
"github.com/GoogleCloudPlatform/container-diff/utils"
26+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
27+
"github.com/GoogleCloudPlatform/container-diff/util"
2728
"github.com/golang/glog"
2829
)
2930

@@ -35,19 +36,19 @@ func (a AptAnalyzer) Name() string {
3536
}
3637

3738
// AptDiff compares the packages installed by apt-get.
38-
func (a AptAnalyzer) Diff(image1, image2 utils.Image) (utils.Result, error) {
39+
func (a AptAnalyzer) Diff(image1, image2 pkgutil.Image) (util.Result, error) {
3940
diff, err := singleVersionDiff(image1, image2, a)
4041
return diff, err
4142
}
4243

43-
func (a AptAnalyzer) Analyze(image utils.Image) (utils.Result, error) {
44+
func (a AptAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) {
4445
analysis, err := singleVersionAnalysis(image, a)
4546
return analysis, err
4647
}
4748

48-
func (a AptAnalyzer) getPackages(image utils.Image) (map[string]utils.PackageInfo, error) {
49+
func (a AptAnalyzer) getPackages(image pkgutil.Image) (map[string]util.PackageInfo, error) {
4950
path := image.FSPath
50-
packages := make(map[string]utils.PackageInfo)
51+
packages := make(map[string]util.PackageInfo)
5152
if _, err := os.Stat(path); err != nil {
5253
// invalid image directory path
5354
return packages, err
@@ -74,7 +75,7 @@ func (a AptAnalyzer) getPackages(image utils.Image) (map[string]utils.PackageInf
7475
return packages, nil
7576
}
7677

77-
func parseLine(text string, currPackage string, packages map[string]utils.PackageInfo) string {
78+
func parseLine(text string, currPackage string, packages map[string]util.PackageInfo) string {
7879
line := strings.Split(text, ": ")
7980
if len(line) == 2 {
8081
key := line[0]
@@ -91,7 +92,7 @@ func parseLine(text string, currPackage string, packages map[string]utils.Packag
9192
modifiedValue := strings.Replace(value, "+", " ", 1)
9293
currPackageInfo, ok := packages[currPackage]
9394
if !ok {
94-
currPackageInfo = utils.PackageInfo{}
95+
currPackageInfo = util.PackageInfo{}
9596
}
9697
currPackageInfo.Version = modifiedValue
9798
packages[currPackage] = currPackageInfo
@@ -100,7 +101,7 @@ func parseLine(text string, currPackage string, packages map[string]utils.Packag
100101
case "Installed-Size":
101102
currPackageInfo, ok := packages[currPackage]
102103
if !ok {
103-
currPackageInfo = utils.PackageInfo{}
104+
currPackageInfo = util.PackageInfo{}
104105
}
105106
var size int64
106107
var err error

differs/apt_diff_test.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,65 @@ import (
2020
"reflect"
2121
"testing"
2222

23-
"github.com/GoogleCloudPlatform/container-diff/utils"
23+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
24+
"github.com/GoogleCloudPlatform/container-diff/util"
2425
)
2526

2627
func TestParseLine(t *testing.T) {
2728
testCases := []struct {
2829
descrip string
2930
line string
30-
packages map[string]utils.PackageInfo
31+
packages map[string]util.PackageInfo
3132
currPackage string
3233
expPackage string
33-
expected map[string]utils.PackageInfo
34+
expected map[string]util.PackageInfo
3435
}{
3536
{
3637
descrip: "Not applicable line",
3738
line: "Garbage: garbage info",
38-
packages: map[string]utils.PackageInfo{},
39+
packages: map[string]util.PackageInfo{},
3940
expPackage: "",
40-
expected: map[string]utils.PackageInfo{},
41+
expected: map[string]util.PackageInfo{},
4142
},
4243
{
4344
descrip: "Package line",
4445
line: "Package: La-Croix",
4546
currPackage: "Tea",
4647
expPackage: "La-Croix",
47-
packages: map[string]utils.PackageInfo{},
48-
expected: map[string]utils.PackageInfo{},
48+
packages: map[string]util.PackageInfo{},
49+
expected: map[string]util.PackageInfo{},
4950
},
5051
{
5152
descrip: "Version line",
5253
line: "Version: Lime",
53-
packages: map[string]utils.PackageInfo{},
54+
packages: map[string]util.PackageInfo{},
5455
currPackage: "La-Croix",
5556
expPackage: "La-Croix",
56-
expected: map[string]utils.PackageInfo{"La-Croix": {Version: "Lime"}},
57+
expected: map[string]util.PackageInfo{"La-Croix": {Version: "Lime"}},
5758
},
5859
{
5960
descrip: "Version line with deb release info",
6061
line: "Version: Lime+extra_lime",
61-
packages: map[string]utils.PackageInfo{},
62+
packages: map[string]util.PackageInfo{},
6263
currPackage: "La-Croix",
6364
expPackage: "La-Croix",
64-
expected: map[string]utils.PackageInfo{"La-Croix": {Version: "Lime extra_lime"}},
65+
expected: map[string]util.PackageInfo{"La-Croix": {Version: "Lime extra_lime"}},
6566
},
6667
{
6768
descrip: "Size line",
6869
line: "Installed-Size: 12",
69-
packages: map[string]utils.PackageInfo{},
70+
packages: map[string]util.PackageInfo{},
7071
currPackage: "La-Croix",
7172
expPackage: "La-Croix",
72-
expected: map[string]utils.PackageInfo{"La-Croix": {Size: 12288}},
73+
expected: map[string]util.PackageInfo{"La-Croix": {Size: 12288}},
7374
},
7475
{
7576
descrip: "Pre-existing PackageInfo struct",
7677
line: "Installed-Size: 12",
77-
packages: map[string]utils.PackageInfo{"La-Croix": {Version: "Lime"}},
78+
packages: map[string]util.PackageInfo{"La-Croix": {Version: "Lime"}},
7879
currPackage: "La-Croix",
7980
expPackage: "La-Croix",
80-
expected: map[string]utils.PackageInfo{"La-Croix": {Version: "Lime", Size: 12288}},
81+
expected: map[string]util.PackageInfo{"La-Croix": {Version: "Lime", Size: 12288}},
8182
},
8283
}
8384

@@ -96,32 +97,32 @@ func TestGetAptPackages(t *testing.T) {
9697
testCases := []struct {
9798
descrip string
9899
path string
99-
expected map[string]utils.PackageInfo
100+
expected map[string]util.PackageInfo
100101
err bool
101102
}{
102103
{
103104
descrip: "no directory",
104105
path: "testDirs/notThere",
105-
expected: map[string]utils.PackageInfo{},
106+
expected: map[string]util.PackageInfo{},
106107
err: true,
107108
},
108109
{
109110
descrip: "no packages",
110111
path: "testDirs/noPackages",
111-
expected: map[string]utils.PackageInfo{},
112+
expected: map[string]util.PackageInfo{},
112113
},
113114
{
114115
descrip: "packages in expected location",
115116
path: "testDirs/packageOne",
116-
expected: map[string]utils.PackageInfo{
117+
expected: map[string]util.PackageInfo{
117118
"pac1": {Version: "1.0"},
118119
"pac2": {Version: "2.0"},
119120
"pac3": {Version: "3.0"}},
120121
},
121122
}
122123
for _, test := range testCases {
123124
d := AptAnalyzer{}
124-
image := utils.Image{FSPath: test.path}
125+
image := pkgutil.Image{FSPath: test.path}
125126
packages, err := d.getPackages(image)
126127
if err != nil && !test.err {
127128
t.Errorf("Got unexpected error: %s", err)

0 commit comments

Comments
 (0)