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

Commit 6525657

Browse files
committed
export various util methods into pkg/util
1 parent 32b0eaa commit 6525657

27 files changed

+392
-364
lines changed

cmd/analyze.go

Lines changed: 2 additions & 2 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,7 +62,7 @@ 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
}

cmd/diff.go

Lines changed: 5 additions & 5 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,21 +68,21 @@ 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
}

cmd/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626

2727
"github.com/GoogleCloudPlatform/container-diff/differs"
28+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2829
"github.com/GoogleCloudPlatform/container-diff/utils"
2930
"github.com/docker/docker/client"
3031
"github.com/golang/glog"
@@ -83,7 +84,7 @@ func outputResults(resultMap map[string]utils.Result) {
8384
}
8485
}
8586

86-
func cleanupImage(image utils.Image) {
87+
func cleanupImage(image pkgutil.Image) {
8788
if image.FSPath != "" {
8889
glog.Infof("Removing image filesystem directory %s from system", image.FSPath)
8990
errMsg := remove(image.FSPath, true)
@@ -103,7 +104,7 @@ func validateArgs(args []string, validatefxns ...validatefxn) error {
103104
}
104105

105106
func checkImage(arg string) bool {
106-
if !utils.CheckImageID(arg) && !utils.CheckImageURL(arg) && !utils.CheckTar(arg) {
107+
if !pkgutil.CheckImageID(arg) && !pkgutil.CheckImageURL(arg) && !pkgutil.CheckTar(arg) {
107108
return false
108109
}
109110
return true

differs/apt_diff.go

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

26+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2627
"github.com/GoogleCloudPlatform/container-diff/utils"
2728
"github.com/golang/glog"
2829
)
@@ -35,17 +36,17 @@ 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) (utils.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) (utils.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]utils.PackageInfo, error) {
4950
path := image.FSPath
5051
packages := make(map[string]utils.PackageInfo)
5152
if _, err := os.Stat(path); err != nil {

differs/apt_diff_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"reflect"
2121
"testing"
2222

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

@@ -121,7 +122,7 @@ func TestGetAptPackages(t *testing.T) {
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)

differs/differs.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,25 @@ import (
2020
"errors"
2121
"fmt"
2222

23+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2324
"github.com/GoogleCloudPlatform/container-diff/utils"
2425
"github.com/golang/glog"
2526
)
2627

2728
type DiffRequest struct {
28-
Image1 utils.Image
29-
Image2 utils.Image
29+
Image1 pkgutil.Image
30+
Image2 pkgutil.Image
3031
DiffTypes []Analyzer
3132
}
3233

3334
type SingleRequest struct {
34-
Image utils.Image
35+
Image pkgutil.Image
3536
AnalyzeTypes []Analyzer
3637
}
3738

3839
type Analyzer interface {
39-
Diff(image1, image2 utils.Image) (utils.Result, error)
40-
Analyze(image utils.Image) (utils.Result, error)
40+
Diff(image1, image2 pkgutil.Image) (utils.Result, error)
41+
Analyze(image pkgutil.Image) (utils.Result, error)
4142
Name() string
4243
}
4344

differs/file_diff.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package differs
1818

1919
import (
20+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2021
"github.com/GoogleCloudPlatform/container-diff/utils"
2122
)
2223

@@ -28,7 +29,7 @@ func (a FileAnalyzer) Name() string {
2829
}
2930

3031
// FileDiff diffs two packages and compares their contents
31-
func (a FileAnalyzer) Diff(image1, image2 utils.Image) (utils.Result, error) {
32+
func (a FileAnalyzer) Diff(image1, image2 pkgutil.Image) (utils.Result, error) {
3233
diff, err := diffImageFiles(image1, image2)
3334
return &utils.DirDiffResult{
3435
Image1: image1.Source,
@@ -38,31 +39,31 @@ func (a FileAnalyzer) Diff(image1, image2 utils.Image) (utils.Result, error) {
3839
}, err
3940
}
4041

41-
func (a FileAnalyzer) Analyze(image utils.Image) (utils.Result, error) {
42+
func (a FileAnalyzer) Analyze(image pkgutil.Image) (utils.Result, error) {
4243
var result utils.FileAnalyzeResult
4344

44-
imgDir, err := utils.GetDirectory(image.FSPath, true)
45+
imgDir, err := pkgutil.GetDirectory(image.FSPath, true)
4546
if err != nil {
4647
return result, err
4748
}
4849

4950
result.Image = image.Source
5051
result.AnalyzeType = "File"
51-
result.Analysis = utils.GetDirectoryEntries(imgDir)
52+
result.Analysis = pkgutil.GetDirectoryEntries(imgDir)
5253
return &result, err
5354
}
5455

55-
func diffImageFiles(image1, image2 utils.Image) (utils.DirDiff, error) {
56+
func diffImageFiles(image1, image2 pkgutil.Image) (utils.DirDiff, error) {
5657
img1 := image1.FSPath
5758
img2 := image2.FSPath
5859

5960
var diff utils.DirDiff
6061

61-
img1Dir, err := utils.GetDirectory(img1, true)
62+
img1Dir, err := pkgutil.GetDirectory(img1, true)
6263
if err != nil {
6364
return diff, err
6465
}
65-
img2Dir, err := utils.GetDirectory(img2, true)
66+
img2Dir, err := pkgutil.GetDirectory(img2, true)
6667
if err != nil {
6768
return diff, err
6869
}

differs/history_diff.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package differs
1919
import (
2020
"strings"
2121

22+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2223
"github.com/GoogleCloudPlatform/container-diff/utils"
2324
)
2425

@@ -34,7 +35,7 @@ func (a HistoryAnalyzer) Name() string {
3435
return "HistoryAnalyzer"
3536
}
3637

37-
func (a HistoryAnalyzer) Diff(image1, image2 utils.Image) (utils.Result, error) {
38+
func (a HistoryAnalyzer) Diff(image1, image2 pkgutil.Image) (utils.Result, error) {
3839
diff, err := getHistoryDiff(image1, image2)
3940
return &utils.HistDiffResult{
4041
Image1: image1.Source,
@@ -44,7 +45,7 @@ func (a HistoryAnalyzer) Diff(image1, image2 utils.Image) (utils.Result, error)
4445
}, err
4546
}
4647

47-
func (a HistoryAnalyzer) Analyze(image utils.Image) (utils.Result, error) {
48+
func (a HistoryAnalyzer) Analyze(image pkgutil.Image) (utils.Result, error) {
4849
history := getHistoryList(image.Config.History)
4950
result := utils.ListAnalyzeResult{
5051
Image: image.Source,
@@ -54,7 +55,7 @@ func (a HistoryAnalyzer) Analyze(image utils.Image) (utils.Result, error) {
5455
return &result, nil
5556
}
5657

57-
func getHistoryDiff(image1, image2 utils.Image) (HistDiff, error) {
58+
func getHistoryDiff(image1, image2 pkgutil.Image) (HistDiff, error) {
5859
history1 := getHistoryList(image1.Config.History)
5960
history2 := getHistoryList(image2.Config.History)
6061

@@ -64,7 +65,7 @@ func getHistoryDiff(image1, image2 utils.Image) (HistDiff, error) {
6465
return diff, nil
6566
}
6667

67-
func getHistoryList(historyItems []utils.ImageHistoryItem) []string {
68+
func getHistoryList(historyItems []pkgutil.ImageHistoryItem) []string {
6869
strhistory := make([]string, len(historyItems))
6970
for i, layer := range historyItems {
7071
strhistory[i] = strings.TrimSpace(layer.CreatedBy)

differs/node_diff.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"path/filepath"
2424
"strings"
2525

26+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2627
"github.com/GoogleCloudPlatform/container-diff/utils"
2728
"github.com/golang/glog"
2829
)
@@ -35,17 +36,17 @@ func (a NodeAnalyzer) Name() string {
3536
}
3637

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

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

48-
func (a NodeAnalyzer) getPackages(image utils.Image) (map[string]map[string]utils.PackageInfo, error) {
49+
func (a NodeAnalyzer) getPackages(image pkgutil.Image) (map[string]map[string]utils.PackageInfo, error) {
4950
path := image.FSPath
5051
packages := make(map[string]map[string]utils.PackageInfo)
5152
if _, err := os.Stat(path); err != nil {
@@ -74,7 +75,7 @@ func (a NodeAnalyzer) getPackages(image utils.Image) (map[string]map[string]util
7475
var currInfo utils.PackageInfo
7576
currInfo.Version = packageJSON.Version
7677
packagePath := strings.TrimSuffix(currPackage, "package.json")
77-
currInfo.Size = utils.GetSize(packagePath)
78+
currInfo.Size = pkgutil.GetSize(packagePath)
7879
mapPath := strings.Replace(packagePath, path, "", 1)
7980
// Check if other package version already recorded
8081
if _, ok := packages[packageJSON.Name]; !ok {

differs/node_diff_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"reflect"
2121
"testing"
2222

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

@@ -60,7 +61,7 @@ func TestGetNodePackages(t *testing.T) {
6061
}
6162

6263
for _, test := range testCases {
63-
image := utils.Image{FSPath: test.path}
64+
image := pkgutil.Image{FSPath: test.path}
6465
d := NodeAnalyzer{}
6566
packages, err := d.getPackages(image)
6667
if err != nil && !test.err {

0 commit comments

Comments
 (0)