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

Remove more reflect usage. #70

Merged
merged 1 commit into from
Sep 2, 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
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
goflag "flag"
"fmt"
"os"
"reflect"
"sort"

"github.com/GoogleCloudPlatform/container-diff/utils"
Expand Down Expand Up @@ -81,7 +80,7 @@ func outputResults(resultMap map[string]utils.Result) {
}

func cleanupImage(image utils.Image) {
if !reflect.DeepEqual(image, (utils.Image{})) {
if image.FSPath != "" {
glog.Infof("Removing image filesystem directory %s from system", image.FSPath)
errMsg := remove(image.FSPath, true)
if errMsg != "" {
Expand Down
4 changes: 4 additions & 0 deletions differs/apt_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
type AptAnalyzer struct {
}

func (a AptAnalyzer) Name() string {
return "AptAnalyzer"
}

// AptDiff compares the packages installed by apt-get.
func (a AptAnalyzer) Diff(image1, image2 utils.Image) (utils.Result, error) {
diff, err := singleVersionDiff(image1, image2, a)
Expand Down
9 changes: 4 additions & 5 deletions differs/differs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package differs
import (
"errors"
"fmt"
"reflect"

"github.com/GoogleCloudPlatform/container-diff/utils"
"github.com/golang/glog"
Expand All @@ -23,6 +22,7 @@ type SingleRequest struct {
type Analyzer interface {
Diff(image1, image2 utils.Image) (utils.Result, error)
Analyze(image utils.Image) (utils.Result, error)
Name() string
}

var analyzers = map[string]Analyzer{
Expand All @@ -40,11 +40,10 @@ func (req DiffRequest) GetDiff() (map[string]utils.Result, error) {

results := map[string]utils.Result{}
for _, differ := range diffs {
differName := reflect.TypeOf(differ).Name()
if diff, err := differ.Diff(img1, img2); err == nil {
results[differName] = diff
results[differ.Name()] = diff
} else {
glog.Errorf("Error getting diff with %s: %s", differName, err)
glog.Errorf("Error getting diff with %s: %s", differ.Name(), err)
}
}

Expand All @@ -64,7 +63,7 @@ func (req SingleRequest) GetAnalysis() (map[string]utils.Result, error) {

results := map[string]utils.Result{}
for _, analyzer := range analyses {
analyzeName := reflect.TypeOf(analyzer).Name()
analyzeName := analyzer.Name()
if analysis, err := analyzer.Analyze(img); err == nil {
results[analyzeName] = analysis
} else {
Expand Down
4 changes: 4 additions & 0 deletions differs/file_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
type FileAnalyzer struct {
}

func (a FileAnalyzer) Name() string {
return "FileAnalyzer"
}

// FileDiff diffs two packages and compares their contents
func (a FileAnalyzer) Diff(image1, image2 utils.Image) (utils.Result, error) {
diff, err := diffImageFiles(image1, image2)
Expand Down
4 changes: 4 additions & 0 deletions differs/history_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type HistDiff struct {
Dels []string
}

func (a HistoryAnalyzer) Name() string {
return "HistoryAnalyzer"
}

func (a HistoryAnalyzer) Diff(image1, image2 utils.Image) (utils.Result, error) {
diff, err := getHistoryDiff(image1, image2)
return &utils.HistDiffResult{
Expand Down
4 changes: 4 additions & 0 deletions differs/node_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
type NodeAnalyzer struct {
}

func (a NodeAnalyzer) Name() string {
return "NodeAnalyzer"
}

// NodeDiff compares the packages installed by apt-get.
func (a NodeAnalyzer) Diff(image1, image2 utils.Image) (utils.Result, error) {
diff, err := multiVersionDiff(image1, image2, a)
Expand Down
11 changes: 6 additions & 5 deletions differs/package_differs.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package differs

import (
"reflect"
"strings"

"github.com/GoogleCloudPlatform/container-diff/utils"
)

type MultiVersionPackageAnalyzer interface {
getPackages(image utils.Image) (map[string]map[string]utils.PackageInfo, error)
Name() string
}

type SingleVersionPackageAnalyzer interface {
getPackages(image utils.Image) (map[string]utils.PackageInfo, error)
Name() string
}

func multiVersionDiff(image1, image2 utils.Image, differ MultiVersionPackageAnalyzer) (*utils.MultiVersionPackageDiffResult, error) {
Expand All @@ -29,7 +30,7 @@ func multiVersionDiff(image1, image2 utils.Image, differ MultiVersionPackageAnal
return &utils.MultiVersionPackageDiffResult{
Image1: image1.Source,
Image2: image2.Source,
DiffType: strings.TrimSuffix(reflect.TypeOf(differ).Name(), "Analyzer"),
DiffType: strings.TrimSuffix(differ.Name(), "Analyzer"),
Diff: diff,
}, nil
}
Expand All @@ -48,7 +49,7 @@ func singleVersionDiff(image1, image2 utils.Image, differ SingleVersionPackageAn
return &utils.SingleVersionPackageDiffResult{
Image1: image1.Source,
Image2: image2.Source,
DiffType: strings.TrimSuffix(reflect.TypeOf(differ).Name(), "Analyzer"),
DiffType: strings.TrimSuffix(differ.Name(), "Analyzer"),
Diff: diff,
}, nil
}
Expand All @@ -61,7 +62,7 @@ func multiVersionAnalysis(image utils.Image, analyzer MultiVersionPackageAnalyze

analysis := utils.MultiVersionPackageAnalyzeResult{
Image: image.Source,
AnalyzeType: strings.TrimSuffix(reflect.TypeOf(analyzer).Name(), "Analyzer"),
AnalyzeType: strings.TrimSuffix(analyzer.Name(), "Analyzer"),
Analysis: pack,
}
return &analysis, nil
Expand All @@ -75,7 +76,7 @@ func singleVersionAnalysis(image utils.Image, analyzer SingleVersionPackageAnaly

analysis := utils.SingleVersionPackageAnalyzeResult{
Image: image.Source,
AnalyzeType: strings.TrimSuffix(reflect.TypeOf(analyzer).Name(), "Analyzer"),
AnalyzeType: strings.TrimSuffix(analyzer.Name(), "Analyzer"),
Analysis: pack,
}
return &analysis, nil
Expand Down
7 changes: 5 additions & 2 deletions differs/pip_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package differs
import (
"io/ioutil"
"path/filepath"
"reflect"
"regexp"
"strings"

Expand All @@ -14,6 +13,10 @@ import (
type PipAnalyzer struct {
}

func (a PipAnalyzer) Name() string {
return "PipAnalyzer"
}

// PipDiff compares pip-installed Python packages between layers of two different images.
func (a PipAnalyzer) Diff(image1, image2 utils.Image) (utils.Result, error) {
diff, err := multiVersionDiff(image1, image2, a)
Expand All @@ -29,7 +32,7 @@ func (a PipAnalyzer) getPackages(image utils.Image) (map[string]map[string]utils
path := image.FSPath
packages := make(map[string]map[string]utils.PackageInfo)
pythonPaths := []string{}
if !reflect.DeepEqual(utils.ConfigSchema{}, image.Config) {
if image.Config.Config.Env != nil {
paths := getPythonPaths(image.Config.Config.Env)
for _, p := range paths {
pythonPaths = append(pythonPaths, p)
Expand Down