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

Commit 7cfb78d

Browse files
committed
cleanup error handling
1 parent 95c25f9 commit 7cfb78d

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

cmd/analyze.go

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

1919
import (
20-
"errors"
2120
"fmt"
2221
"os"
2322
"strings"
@@ -34,10 +33,10 @@ var analyzeCmd = &cobra.Command{
3433
Long: `Analyzes an image using the specifed analyzers as indicated via flags (see documentation for available ones).`,
3534
Args: func(cmd *cobra.Command, args []string) error {
3635
if err := validateArgs(args, checkAnalyzeArgNum); err != nil {
37-
return errors.New(err.Error())
36+
return err
3837
}
3938
if err := checkIfValidAnalyzer(types); err != nil {
40-
return errors.New(err.Error())
39+
return err
4140
}
4241
return nil
4342
},
@@ -51,21 +50,20 @@ var analyzeCmd = &cobra.Command{
5150

5251
func checkAnalyzeArgNum(args []string) error {
5352
if len(args) != 1 {
54-
return errors.New("'analyze' requires one image as an argument: container analyze [image]")
53+
return fmt.Errorf("'analyze' requires one image as an argument: container-diff analyze [image]")
5554
}
5655
return nil
5756
}
5857

5958
func analyzeImage(imageArg string, analyzerArgs []string) error {
6059
analyzeTypes, err := differs.GetAnalyzers(analyzerArgs)
6160
if err != nil {
62-
glog.Error(err.Error())
63-
return errors.New("Could not perform image analysis")
61+
return err
6462
}
6563

6664
cli, err := NewClient()
6765
if err != nil {
68-
return fmt.Errorf("Error getting docker client for differ: %s", err)
66+
return fmt.Errorf("Error getting docker client: %s", err)
6967
}
7068
defer cli.Close()
7169
ip := pkgutil.ImagePrepper{
@@ -78,15 +76,13 @@ func analyzeImage(imageArg string, analyzerArgs []string) error {
7876
defer pkgutil.CleanupImage(image)
7977
}
8078
if err != nil {
81-
glog.Error(err.Error())
82-
return errors.New("Could not perform image analysis")
79+
return fmt.Errorf("Error processing image: %s", err)
8380
}
8481

8582
req := differs.SingleRequest{image, analyzeTypes}
8683
analyses, err := req.GetAnalysis()
8784
if err != nil {
88-
glog.Error(err.Error())
89-
return errors.New("Could not perform image analysis")
85+
return fmt.Errorf("Error performing image analysis: %s", err)
9086
}
9187

9288
glog.Info("Retrieving analyses")

cmd/diff.go

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

1919
import (
20-
"errors"
2120
"fmt"
2221
"os"
2322
"strings"
@@ -35,10 +34,10 @@ var diffCmd = &cobra.Command{
3534
Long: `Compares two images using the specifed analyzers as indicated via flags (see documentation for available ones).`,
3635
Args: func(cmd *cobra.Command, args []string) error {
3736
if err := validateArgs(args, checkDiffArgNum); err != nil {
38-
return errors.New(err.Error())
37+
return err
3938
}
4039
if err := checkIfValidAnalyzer(types); err != nil {
41-
return errors.New(err.Error())
40+
return err
4241
}
4342
return nil
4443
},
@@ -52,21 +51,20 @@ var diffCmd = &cobra.Command{
5251

5352
func checkDiffArgNum(args []string) error {
5453
if len(args) != 2 {
55-
return errors.New("'diff' requires two images as arguments: container diff [image1] [image2]")
54+
return fmt.Errorf("'diff' requires two images as arguments: container-diff diff [image1] [image2]")
5655
}
5756
return nil
5857
}
5958

6059
func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
6160
diffTypes, err := differs.GetAnalyzers(diffArgs)
6261
if err != nil {
63-
glog.Error(err.Error())
64-
return errors.New("Could not perform image diff")
62+
return err
6563
}
6664

6765
cli, err := NewClient()
6866
if err != nil {
69-
return fmt.Errorf("Error getting docker client for differ: %s", err)
67+
return err
7068
}
7169
defer cli.Close()
7270
var wg sync.WaitGroup
@@ -102,8 +100,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
102100
req := differs.DiffRequest{*imageMap[image1Arg], *imageMap[image2Arg], diffTypes}
103101
diffs, err := req.GetDiff()
104102
if err != nil {
105-
glog.Error(err.Error())
106-
return errors.New("Could not perform image diff")
103+
return fmt.Errorf("Could not retrieve diff: %s", err)
107104
}
108105
glog.Info("Retrieving diffs")
109106
outputResults(diffs)

cmd/root.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/GoogleCloudPlatform/container-diff/util"
2828
"github.com/docker/docker/client"
2929
"github.com/golang/glog"
30-
"github.com/pkg/errors"
3130
"github.com/spf13/cobra"
3231
"github.com/spf13/pflag"
3332
)
@@ -98,9 +97,7 @@ func checkIfValidAnalyzer(flagtypes string) error {
9897
analyzers := strings.Split(flagtypes, ",")
9998
for _, name := range analyzers {
10099
if _, exists := differs.Analyzers[name]; !exists {
101-
errMessage := fmt.Sprintf("Argument %s is not a valid analyzer\n", name)
102-
glog.Errorf(errMessage)
103-
return errors.New(errMessage)
100+
return fmt.Errorf("Argument %s is not a valid analyzer", name)
104101
}
105102
}
106103
return nil

differs/differs.go

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

1919
import (
20-
"errors"
2120
"fmt"
2221

2322
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
@@ -98,16 +97,17 @@ func (req SingleRequest) GetAnalysis() (map[string]util.Result, error) {
9897
return results, err
9998
}
10099

101-
func GetAnalyzers(analyzeNames []string) (analyzeFuncs []Analyzer, err error) {
100+
func GetAnalyzers(analyzeNames []string) ([]Analyzer, error) {
101+
var analyzeFuncs []Analyzer
102102
for _, name := range analyzeNames {
103103
if a, exists := Analyzers[name]; exists {
104104
analyzeFuncs = append(analyzeFuncs, a)
105105
} else {
106-
glog.Errorf("Unknown analyzer/differ specified", name)
106+
return nil, fmt.Errorf("Unknown analyzer/differ specified: %s", name)
107107
}
108108
}
109109
if len(analyzeFuncs) == 0 {
110-
err = errors.New("No known analyzers/differs specified")
110+
return nil, fmt.Errorf("No known analyzers/differs specified")
111111
}
112-
return
112+
return analyzeFuncs, nil
113113
}

0 commit comments

Comments
 (0)