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

Commit be604f0

Browse files
committed
Changed CLI to use --types flag, a comma separated list of desired analyzers
1 parent 8be7997 commit be604f0

File tree

5 files changed

+17
-66
lines changed

5 files changed

+17
-66
lines changed

cmd/analyze.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"strings"
78

89
"github.com/GoogleCloudPlatform/container-diff/differs"
910
"github.com/GoogleCloudPlatform/container-diff/utils"
@@ -20,20 +21,7 @@ var analyzeCmd = &cobra.Command{
2021
glog.Error(err.Error())
2122
os.Exit(1)
2223
}
23-
analyzeArgs := []string{}
24-
allAnalyzers := getAllAnalyzers()
25-
for _, name := range allAnalyzers {
26-
if *analyzeFlagMap[name] == true {
27-
analyzeArgs = append(analyzeArgs, name)
28-
}
29-
}
30-
31-
// If no analyzers are specified, perform them all as the default
32-
if len(analyzeArgs) == 0 {
33-
analyzeArgs = allAnalyzers
34-
}
35-
36-
if err := analyzeImage(args[0], analyzeArgs); err != nil {
24+
if err := analyzeImage(args[0], strings.Split(types, ",")); err != nil {
3725
glog.Error(err)
3826
os.Exit(1)
3927
}

cmd/diff.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"strings"
78
"sync"
89

910
"github.com/GoogleCloudPlatform/container-diff/differs"
@@ -21,20 +22,7 @@ var diffCmd = &cobra.Command{
2122
glog.Error(err.Error())
2223
os.Exit(1)
2324
}
24-
analyzeArgs := []string{}
25-
allAnalyzers := getAllAnalyzers()
26-
for _, name := range allAnalyzers {
27-
if *analyzeFlagMap[name] == true {
28-
analyzeArgs = append(analyzeArgs, name)
29-
}
30-
}
31-
32-
// If no analyzers are specified, perform them all as the default
33-
if len(analyzeArgs) == 0 {
34-
analyzeArgs = allAnalyzers
35-
}
36-
37-
if err := diffImages(args[0], args[1], analyzeArgs); err != nil {
25+
if err := diffImages(args[0], args[1], strings.Split(types, ",")); err != nil {
3826
glog.Error(err)
3927
os.Exit(1)
4028
}

cmd/root.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"bytes"
55
"context"
6-
"errors"
76
goflag "flag"
87
"fmt"
98
"os"
@@ -13,26 +12,14 @@ import (
1312
"github.com/GoogleCloudPlatform/container-diff/utils"
1413
"github.com/docker/docker/client"
1514
"github.com/golang/glog"
15+
"github.com/pkg/errors"
1616
"github.com/spf13/cobra"
1717
"github.com/spf13/pflag"
1818
)
1919

2020
var json bool
21-
var eng bool
2221
var save bool
23-
var apt bool
24-
var node bool
25-
var file bool
26-
var history bool
27-
var pip bool
28-
29-
var analyzeFlagMap = map[string]*bool{
30-
"apt": &apt,
31-
"node": &node,
32-
"file": &file,
33-
"history": &history,
34-
"pip": &pip,
35-
}
22+
var types string
3623

3724
type validatefxn func(args []string) (bool, error)
3825

@@ -90,14 +77,6 @@ func cleanupImage(image utils.Image) {
9077
}
9178
}
9279

93-
func getAllAnalyzers() []string {
94-
allAnalyzers := []string{}
95-
for name := range analyzeFlagMap {
96-
allAnalyzers = append(allAnalyzers, name)
97-
}
98-
return allAnalyzers
99-
}
100-
10180
func validateArgs(args []string, validatefxns ...validatefxn) (bool, error) {
10281
for _, validatefxn := range validatefxns {
10382
valid, err := validatefxn(args)
@@ -157,11 +136,7 @@ func init() {
157136

158137
func addSharedFlags(cmd *cobra.Command) {
159138
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).")
160-
cmd.Flags().BoolVarP(&pip, "pip", "p", false, "Set this flag to use the pip differ.")
161-
cmd.Flags().BoolVarP(&node, "node", "n", false, "Set this flag to use the node differ.")
162-
cmd.Flags().BoolVarP(&apt, "apt", "a", false, "Set this flag to use the apt differ.")
163-
cmd.Flags().BoolVarP(&file, "file", "f", false, "Set this flag to use the file differ.")
164-
cmd.Flags().BoolVarP(&history, "history", "d", false, "Set this flag to use the dockerfile history differ.")
139+
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.")
165140
cmd.Flags().BoolVarP(&save, "save", "s", false, "Set this flag to save rather than remove the final image filesystems on exit.")
166141
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.")
167142
}

main

12.7 MB
Binary file not shown.

tests/integration_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,75 +72,75 @@ func TestDiffAndAnalysis(t *testing.T) {
7272
subcommand: "diff",
7373
imageA: diffBase,
7474
imageB: diffModified,
75-
differFlag: "-f",
75+
differFlag: "--types=file",
7676
expectedFile: "file_diff_expected.json",
7777
},
7878
{
7979
description: "apt differ",
8080
subcommand: "diff",
8181
imageA: aptBase,
8282
imageB: aptModified,
83-
differFlag: "-a",
83+
differFlag: "--types=apt",
8484
expectedFile: "apt_diff_expected.json",
8585
},
8686
{
8787
description: "node differ",
8888
subcommand: "diff",
8989
imageA: nodeBase,
9090
imageB: nodeModified,
91-
differFlag: "-n",
91+
differFlag: "--types=node",
9292
expectedFile: "node_diff_order_expected.json",
9393
},
9494
{
9595
description: "multi differ",
9696
subcommand: "diff",
9797
imageA: multiBase,
9898
imageB: multiModified,
99-
differFlag: "-npa",
99+
differFlag: "--types=node,pip,apt",
100100
expectedFile: "multi_diff_expected.json",
101101
},
102102
{
103103
description: "history differ",
104104
subcommand: "diff",
105105
imageA: diffBase,
106106
imageB: diffModified,
107-
differFlag: "-d",
107+
differFlag: "--types=history",
108108
expectedFile: "hist_diff_expected.json",
109109
},
110110
{
111111
description: "apt sorted differ",
112112
subcommand: "diff",
113113
imageA: aptBase,
114114
imageB: aptModified,
115-
differFlag: "-ao",
115+
differFlag: "--types=apt -o",
116116
expectedFile: "apt_sorted_diff_expected.json",
117117
},
118118
{
119119
description: "apt analysis",
120120
subcommand: "analyze",
121121
imageA: aptModified,
122-
differFlag: "-a",
122+
differFlag: "--types=apt",
123123
expectedFile: "apt_analysis_expected.json",
124124
},
125125
{
126126
description: "file sorted analysis",
127127
subcommand: "analyze",
128128
imageA: diffModified,
129-
differFlag: "-fo",
129+
differFlag: "--types=file -o",
130130
expectedFile: "file_sorted_analysis_expected.json",
131131
},
132132
{
133133
description: "pip analysis",
134134
subcommand: "analyze",
135135
imageA: pipModified,
136-
differFlag: "-p",
136+
differFlag: "--types=pip",
137137
expectedFile: "pip_analysis_expected.json",
138138
},
139139
{
140140
description: "node analysis",
141141
subcommand: "analyze",
142142
imageA: nodeModified,
143-
differFlag: "-n",
143+
differFlag: "--types=node",
144144
expectedFile: "node_analysis_expected.json",
145145
},
146146
}

0 commit comments

Comments
 (0)