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

Export container-diff tar/fs util methods for vendoring #84

Merged
merged 3 commits into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"strings"

"github.com/GoogleCloudPlatform/container-diff/differs"
"github.com/GoogleCloudPlatform/container-diff/utils"
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -62,14 +62,14 @@ func analyzeImage(imageArg string, analyzerArgs []string) error {
return fmt.Errorf("Error getting docker client for differ: %s", err)
}
defer cli.Close()
ip := utils.ImagePrepper{
ip := pkgutil.ImagePrepper{
Source: imageArg,
Client: cli,
}
image, err := ip.GetImage()

if !save {
defer cleanupImage(image)
defer pkgutil.CleanupImage(image)
}
if err != nil {
glog.Error(err.Error())
Expand Down
14 changes: 7 additions & 7 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"sync"

"github.com/GoogleCloudPlatform/container-diff/differs"
"github.com/GoogleCloudPlatform/container-diff/utils"
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -68,29 +68,29 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {

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

imageMap := map[string]*utils.Image{
imageMap := map[string]*pkgutil.Image{
image1Arg: {},
image2Arg: {},
}
for imageArg := range imageMap {
go func(imageName string, imageMap map[string]*utils.Image) {
go func(imageName string, imageMap map[string]*pkgutil.Image) {
defer wg.Done()
ip := utils.ImagePrepper{
ip := pkgutil.ImagePrepper{
Source: imageName,
Client: cli,
}
image, err := ip.GetImage()
imageMap[imageName] = &image
if err != nil {
glog.Error(err.Error())
glog.Errorf("Diff may be inaccurate: %s", err.Error())
}
}(imageArg, imageMap)
}
wg.Wait()

if !save {
defer cleanupImage(*imageMap[image1Arg])
defer cleanupImage(*imageMap[image2Arg])
defer pkgutil.CleanupImage(*imageMap[image1Arg])
defer pkgutil.CleanupImage(*imageMap[image2Arg])
}

diffTypes, err := differs.GetAnalyzers(diffArgs)
Expand Down
40 changes: 6 additions & 34 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"context"
goflag "flag"
"fmt"
"os"
"sort"
"strings"

"github.com/GoogleCloudPlatform/container-diff/differs"
"github.com/GoogleCloudPlatform/container-diff/utils"
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
"github.com/GoogleCloudPlatform/container-diff/util"
"github.com/docker/docker/client"
"github.com/golang/glog"
"github.com/pkg/errors"
Expand Down Expand Up @@ -55,7 +55,7 @@ func NewClient() (*client.Client, error) {
return cli, nil
}

func outputResults(resultMap map[string]utils.Result) {
func outputResults(resultMap map[string]util.Result) {
// Outputs diff/analysis results in alphabetical order by analyzer name
sortedTypes := []string{}
for analyzerType := range resultMap {
Expand All @@ -76,23 +76,13 @@ func outputResults(resultMap map[string]utils.Result) {
}
}
if json {
err := utils.JSONify(results)
err := util.JSONify(results)
if err != nil {
glog.Error(err)
}
}
}

func cleanupImage(image utils.Image) {
if image.FSPath != "" {
glog.Infof("Removing image filesystem directory %s from system", image.FSPath)
errMsg := remove(image.FSPath, true)
if errMsg != "" {
glog.Error(errMsg)
}
}
}

func validateArgs(args []string, validatefxns ...validatefxn) error {
for _, validatefxn := range validatefxns {
if err := validatefxn(args); err != nil {
Expand All @@ -103,7 +93,7 @@ func validateArgs(args []string, validatefxns ...validatefxn) error {
}

func checkImage(arg string) bool {
if !utils.CheckImageID(arg) && !utils.CheckImageURL(arg) && !utils.CheckTar(arg) {
if !pkgutil.CheckImageID(arg) && !pkgutil.CheckImageURL(arg) && !pkgutil.CheckTar(arg) {
return false
}
return true
Expand Down Expand Up @@ -132,24 +122,6 @@ func checkIfValidAnalyzer(flagtypes string) error {
return nil
}

func remove(path string, dir bool) string {
var errStr string
if path == "" {
return ""
}

var err error
if dir {
err = os.RemoveAll(path)
} else {
err = os.Remove(path)
}
if err != nil {
errStr = "\nUnable to remove " + path
}
return errStr
}

func init() {
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
}
Expand All @@ -158,5 +130,5 @@ func addSharedFlags(cmd *cobra.Command) {
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).")
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.")
cmd.Flags().BoolVarP(&save, "save", "s", false, "Set this flag to save rather than remove the final image filesystems on exit.")
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.")
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.")
}
17 changes: 9 additions & 8 deletions differs/apt_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
"strconv"
"strings"

"github.com/GoogleCloudPlatform/container-diff/utils"
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
"github.com/GoogleCloudPlatform/container-diff/util"
"github.com/golang/glog"
)

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

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

func (a AptAnalyzer) Analyze(image utils.Image) (utils.Result, error) {
func (a AptAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) {
analysis, err := singleVersionAnalysis(image, a)
return analysis, err
}

func (a AptAnalyzer) getPackages(image utils.Image) (map[string]utils.PackageInfo, error) {
func (a AptAnalyzer) getPackages(image pkgutil.Image) (map[string]util.PackageInfo, error) {
path := image.FSPath
packages := make(map[string]utils.PackageInfo)
packages := make(map[string]util.PackageInfo)
if _, err := os.Stat(path); err != nil {
// invalid image directory path
return packages, err
Expand All @@ -74,7 +75,7 @@ func (a AptAnalyzer) getPackages(image utils.Image) (map[string]utils.PackageInf
return packages, nil
}

func parseLine(text string, currPackage string, packages map[string]utils.PackageInfo) string {
func parseLine(text string, currPackage string, packages map[string]util.PackageInfo) string {
line := strings.Split(text, ": ")
if len(line) == 2 {
key := line[0]
Expand All @@ -91,7 +92,7 @@ func parseLine(text string, currPackage string, packages map[string]utils.Packag
modifiedValue := strings.Replace(value, "+", " ", 1)
currPackageInfo, ok := packages[currPackage]
if !ok {
currPackageInfo = utils.PackageInfo{}
currPackageInfo = util.PackageInfo{}
}
currPackageInfo.Version = modifiedValue
packages[currPackage] = currPackageInfo
Expand All @@ -100,7 +101,7 @@ func parseLine(text string, currPackage string, packages map[string]utils.Packag
case "Installed-Size":
currPackageInfo, ok := packages[currPackage]
if !ok {
currPackageInfo = utils.PackageInfo{}
currPackageInfo = util.PackageInfo{}
}
var size int64
var err error
Expand Down
41 changes: 21 additions & 20 deletions differs/apt_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,65 @@ import (
"reflect"
"testing"

"github.com/GoogleCloudPlatform/container-diff/utils"
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
"github.com/GoogleCloudPlatform/container-diff/util"
)

func TestParseLine(t *testing.T) {
testCases := []struct {
descrip string
line string
packages map[string]utils.PackageInfo
packages map[string]util.PackageInfo
currPackage string
expPackage string
expected map[string]utils.PackageInfo
expected map[string]util.PackageInfo
}{
{
descrip: "Not applicable line",
line: "Garbage: garbage info",
packages: map[string]utils.PackageInfo{},
packages: map[string]util.PackageInfo{},
expPackage: "",
expected: map[string]utils.PackageInfo{},
expected: map[string]util.PackageInfo{},
},
{
descrip: "Package line",
line: "Package: La-Croix",
currPackage: "Tea",
expPackage: "La-Croix",
packages: map[string]utils.PackageInfo{},
expected: map[string]utils.PackageInfo{},
packages: map[string]util.PackageInfo{},
expected: map[string]util.PackageInfo{},
},
{
descrip: "Version line",
line: "Version: Lime",
packages: map[string]utils.PackageInfo{},
packages: map[string]util.PackageInfo{},
currPackage: "La-Croix",
expPackage: "La-Croix",
expected: map[string]utils.PackageInfo{"La-Croix": {Version: "Lime"}},
expected: map[string]util.PackageInfo{"La-Croix": {Version: "Lime"}},
},
{
descrip: "Version line with deb release info",
line: "Version: Lime+extra_lime",
packages: map[string]utils.PackageInfo{},
packages: map[string]util.PackageInfo{},
currPackage: "La-Croix",
expPackage: "La-Croix",
expected: map[string]utils.PackageInfo{"La-Croix": {Version: "Lime extra_lime"}},
expected: map[string]util.PackageInfo{"La-Croix": {Version: "Lime extra_lime"}},
},
{
descrip: "Size line",
line: "Installed-Size: 12",
packages: map[string]utils.PackageInfo{},
packages: map[string]util.PackageInfo{},
currPackage: "La-Croix",
expPackage: "La-Croix",
expected: map[string]utils.PackageInfo{"La-Croix": {Size: 12288}},
expected: map[string]util.PackageInfo{"La-Croix": {Size: 12288}},
},
{
descrip: "Pre-existing PackageInfo struct",
line: "Installed-Size: 12",
packages: map[string]utils.PackageInfo{"La-Croix": {Version: "Lime"}},
packages: map[string]util.PackageInfo{"La-Croix": {Version: "Lime"}},
currPackage: "La-Croix",
expPackage: "La-Croix",
expected: map[string]utils.PackageInfo{"La-Croix": {Version: "Lime", Size: 12288}},
expected: map[string]util.PackageInfo{"La-Croix": {Version: "Lime", Size: 12288}},
},
}

Expand All @@ -96,32 +97,32 @@ func TestGetAptPackages(t *testing.T) {
testCases := []struct {
descrip string
path string
expected map[string]utils.PackageInfo
expected map[string]util.PackageInfo
err bool
}{
{
descrip: "no directory",
path: "testDirs/notThere",
expected: map[string]utils.PackageInfo{},
expected: map[string]util.PackageInfo{},
err: true,
},
{
descrip: "no packages",
path: "testDirs/noPackages",
expected: map[string]utils.PackageInfo{},
expected: map[string]util.PackageInfo{},
},
{
descrip: "packages in expected location",
path: "testDirs/packageOne",
expected: map[string]utils.PackageInfo{
expected: map[string]util.PackageInfo{
"pac1": {Version: "1.0"},
"pac2": {Version: "2.0"},
"pac3": {Version: "3.0"}},
},
}
for _, test := range testCases {
d := AptAnalyzer{}
image := utils.Image{FSPath: test.path}
image := pkgutil.Image{FSPath: test.path}
packages, err := d.getPackages(image)
if err != nil && !test.err {
t.Errorf("Got unexpected error: %s", err)
Expand Down
Loading