This repository was archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 237
split container-diff into subcommands: analyze and diff #60
Merged
aaron-prindle
merged 1 commit into
GoogleContainerTools:master
from
aaron-prindle:add-subcommands
Aug 30, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/GoogleCloudPlatform/container-diff/differs" | ||
"github.com/GoogleCloudPlatform/container-diff/utils" | ||
"github.com/golang/glog" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var analyzeCmd = &cobra.Command{ | ||
Use: "analyze", | ||
Short: "Analyzes an image: [image]", | ||
Long: `Analyzes an image 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, checkAnalyzeArgNum, checkArgType); !validArgs { | ||
glog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
utils.SetDockerEngine(eng) | ||
|
||
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 := analyzeImage(args[0], analyzeArgs); err != nil { | ||
glog.Error(err) | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
func checkAnalyzeArgNum(args []string) (bool, error) { | ||
var errMessage string | ||
if len(args) != 1 { | ||
errMessage = "'analyze' requires one image as an argument: container analyze [image]" | ||
return false, errors.New(errMessage) | ||
} | ||
return true, nil | ||
} | ||
|
||
func analyzeImage(imageArg string, analyzerArgs []string) error { | ||
image, err := utils.ImagePrepper{imageArg}.GetImage() | ||
if err != nil { | ||
glog.Error(err.Error()) | ||
cleanupImage(image) | ||
return errors.New("Could not perform image analysis") | ||
} | ||
analyzeTypes, err := differs.GetAnalyzers(analyzerArgs) | ||
if err != nil { | ||
glog.Error(err.Error()) | ||
cleanupImage(image) | ||
return errors.New("Could not perform image analysis") | ||
} | ||
|
||
req := differs.SingleRequest{image, analyzeTypes} | ||
if analyses, err := req.GetAnalysis(); err == nil { | ||
glog.Info("Retrieving analyses") | ||
outputResults(analyses) | ||
if !save { | ||
cleanupImage(image) | ||
} else { | ||
dir, _ := os.Getwd() | ||
glog.Infof("Image was saved at %s as %s", dir, image.FSPath) | ||
} | ||
} else { | ||
glog.Error(err.Error()) | ||
cleanupImage(image) | ||
return errors.New("Could not perform image analysis") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(analyzeCmd) | ||
addSharedFlags(analyzeCmd) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var analyzeArgNumTests = []testpair{ | ||
{[]string{}, false}, | ||
{[]string{"one"}, true}, | ||
{[]string{"one", "two"}, false}, | ||
} | ||
|
||
func TestAnalyzeArgNum(t *testing.T) { | ||
for _, test := range analyzeArgNumTests { | ||
valid, err := checkAnalyzeArgNum(test.input) | ||
if valid != test.expected_output { | ||
if test.expected_output { | ||
t.Errorf("Got unexpected error: %s", err) | ||
} else { | ||
t.Errorf("Expected error but got none") | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"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) | ||
} | ||
|
||
utils.SetDockerEngine(eng) | ||
|
||
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 { | ||
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 | ||
var err error | ||
go func() { | ||
defer wg.Done() | ||
image1, err = utils.ImagePrepper{image1Arg}.GetImage() | ||
if err != nil { | ||
glog.Error(err.Error()) | ||
} | ||
}() | ||
|
||
go func() { | ||
defer wg.Done() | ||
image2, err = utils.ImagePrepper{image2Arg}.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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var diffArgNumTests = []testpair{ | ||
{[]string{}, false}, | ||
{[]string{"one"}, false}, | ||
{[]string{"one", "two"}, true}, | ||
{[]string{"one", "two", "three"}, false}, | ||
} | ||
|
||
func TestDiffArgNum(t *testing.T) { | ||
for _, test := range diffArgNumTests { | ||
valid, err := checkDiffArgNum(test.input) | ||
if valid != test.expected_output { | ||
if test.expected_output { | ||
t.Errorf("Got unexpected error: %s", err) | ||
} else { | ||
t.Errorf("Expected error but got none") | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming this is just a copy-paste from the analyze and diff functions? So no need to do a full review of the code?