forked from GoogleContainerTools/container-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
128 lines (113 loc) · 2.88 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package cmd
import (
"errors"
"fmt"
"os"
"sync"
"github.com/GoogleCloudPlatform/container-diff/differs"
"github.com/GoogleCloudPlatform/container-diff/utils"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
var diffCmd = &cobra.Command{
Use: "diff",
Short: "Compare two images: [image1] [image2]",
Long: `Compares two images using the specifed analyzers as indicated via flags (see documentation for available ones).`,
Run: func(cmd *cobra.Command, args []string) {
if validArgs, err := validateArgs(args, checkDiffArgNum, checkArgType); !validArgs {
glog.Error(err.Error())
os.Exit(1)
}
analyzeArgs := []string{}
allAnalyzers := getAllAnalyzers()
for _, name := range allAnalyzers {
if *analyzeFlagMap[name] == true {
analyzeArgs = append(analyzeArgs, name)
}
}
// If no analyzers are specified, perform them all as the default
if len(analyzeArgs) == 0 {
analyzeArgs = allAnalyzers
}
if err := diffImages(args[0], args[1], analyzeArgs); err != nil {
glog.Error(err)
os.Exit(1)
}
},
}
func checkDiffArgNum(args []string) (bool, error) {
var errMessage string
if len(args) != 2 {
errMessage = "'diff' requires two images as arguments: container diff [image1] [image2]"
return false, errors.New(errMessage)
}
return true, nil
}
func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
cli, err := NewClient()
if err != nil {
return fmt.Errorf("Error getting docker client for differ: %s", err)
}
defer cli.Close()
var wg sync.WaitGroup
wg.Add(2)
glog.Infof("Starting diff on images %s and %s, using differs: %s", image1Arg, image2Arg, diffArgs)
var image1, image2 utils.Image
go func() {
defer wg.Done()
ip := utils.ImagePrepper{
Source: image1Arg,
Client: cli,
}
image1, err = ip.GetImage()
if err != nil {
glog.Error(err.Error())
}
}()
go func() {
defer wg.Done()
ip := utils.ImagePrepper{
Source: image2Arg,
Client: cli,
}
image2, err = ip.GetImage()
if err != nil {
glog.Error(err.Error())
}
}()
wg.Wait()
if err != nil {
cleanupImage(image1)
cleanupImage(image2)
return errors.New("Could not perform image diff")
}
diffTypes, err := differs.GetAnalyzers(diffArgs)
if err != nil {
glog.Error(err.Error())
cleanupImage(image1)
cleanupImage(image2)
return errors.New("Could not perform image diff")
}
req := differs.DiffRequest{image1, image2, diffTypes}
if diffs, err := req.GetDiff(); err == nil {
glog.Info("Retrieving diffs")
outputResults(diffs)
if !save {
cleanupImage(image1)
cleanupImage(image2)
} else {
dir, _ := os.Getwd()
glog.Infof("Images were saved at %s as %s and %s", dir, image1.FSPath, image2.FSPath)
}
} else {
glog.Error(err.Error())
cleanupImage(image1)
cleanupImage(image2)
return errors.New("Could not perform image diff")
}
return nil
}
func init() {
RootCmd.AddCommand(diffCmd)
addSharedFlags(diffCmd)
}