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

Commit e2f42d2

Browse files
committed
Cleanup flag parsing.
This also changes the types flag from a single comma separated list to a repeated flag that fills an array. This changes the name from types to type, which is a minor breaking change. Let me know what you think.
1 parent ba42184 commit e2f42d2

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

cmd/analyze.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"errors"
2121
"fmt"
2222
"os"
23-
"strings"
2423

2524
"github.com/GoogleCloudPlatform/container-diff/differs"
2625
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
@@ -33,16 +32,13 @@ var analyzeCmd = &cobra.Command{
3332
Short: "Analyzes an image: [image]",
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 {
36-
if err := validateArgs(args, checkAnalyzeArgNum); err != nil {
37-
return err
38-
}
39-
if err := checkIfValidAnalyzer(types); err != nil {
35+
if err := validateArgs(args, checkAnalyzeArgNum, checkIfValidAnalyzer); err != nil {
4036
return err
4137
}
4238
return nil
4339
},
4440
Run: func(cmd *cobra.Command, args []string) {
45-
if err := analyzeImage(args[0], strings.Split(types, ",")); err != nil {
41+
if err := analyzeImage(args[0], types); err != nil {
4642
logrus.Error(err)
4743
os.Exit(1)
4844
}

cmd/diff.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ package cmd
1919
import (
2020
"errors"
2121
"fmt"
22+
"os"
23+
"sync"
24+
2225
"github.com/GoogleCloudPlatform/container-diff/differs"
2326
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2427
"github.com/GoogleCloudPlatform/container-diff/util"
2528
"github.com/sirupsen/logrus"
2629
"github.com/spf13/cobra"
27-
"os"
28-
"strings"
29-
"sync"
3030
)
3131

3232
var filename string
@@ -36,19 +36,13 @@ var diffCmd = &cobra.Command{
3636
Short: "Compare two images: [image1] [image2]",
3737
Long: `Compares two images using the specifed analyzers as indicated via flags (see documentation for available ones).`,
3838
Args: func(cmd *cobra.Command, args []string) error {
39-
if err := validateArgs(args, checkDiffArgNum); err != nil {
40-
return err
41-
}
42-
if err := checkIfValidAnalyzer(types); err != nil {
43-
return err
44-
}
45-
if err := checkFilenameFlag(types); err != nil {
39+
if err := validateArgs(args, checkDiffArgNum, checkIfValidAnalyzer, checkFilenameFlag); err != nil {
4640
return err
4741
}
4842
return nil
4943
},
5044
Run: func(cmd *cobra.Command, args []string) {
51-
if err := diffImages(args[0], args[1], strings.Split(types, ",")); err != nil {
45+
if err := diffImages(args[0], args[1], types); err != nil {
5246
logrus.Error(err)
5347
os.Exit(1)
5448
}
@@ -62,13 +56,16 @@ func checkDiffArgNum(args []string) error {
6256
return nil
6357
}
6458

65-
func checkFilenameFlag(types string) error {
66-
if filename != "" {
67-
if !strings.Contains(types, "file") {
68-
return errors.New("Please include --types=file with the --filename flag")
59+
func checkFilenameFlag(_ []string) error {
60+
if filename == "" {
61+
return nil
62+
}
63+
for _, t := range types {
64+
if t == "file" {
65+
return nil
6966
}
7067
}
71-
return nil
68+
return errors.New("Please include --types=file with the --filename flag")
7269
}
7370

7471
func diffImages(image1Arg, image2Arg string, diffArgs []string) error {

cmd/root.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535

3636
var json bool
3737
var save bool
38-
var types string
38+
var types diffTypes = []string{"apt"}
3939

4040
var LogLevel string
4141

@@ -104,12 +104,11 @@ func validateArgs(args []string, validatefxns ...validatefxn) error {
104104
return nil
105105
}
106106

107-
func checkIfValidAnalyzer(flagtypes string) error {
108-
if flagtypes == "" {
107+
func checkIfValidAnalyzer(_ []string) error {
108+
if len(types) == 0 {
109109
return errors.New("Please provide at least one analyzer to run")
110110
}
111-
analyzers := strings.Split(flagtypes, ",")
112-
for _, name := range analyzers {
111+
for _, name := range types {
113112
if _, exists := differs.Analyzers[name]; !exists {
114113
return fmt.Errorf("Argument %s is not a valid analyzer", name)
115114
}
@@ -146,9 +145,35 @@ func init() {
146145
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
147146
}
148147

148+
// Define a type named "diffSlice" as a slice of strings
149+
type diffTypes []string
150+
151+
// Now, for our new type, implement the two methods of
152+
// the flag.Value interface...
153+
// The first method is String() string
154+
func (d *diffTypes) String() string {
155+
return strings.Join(*d, ",")
156+
}
157+
158+
// The second method is Set(value string) error
159+
func (d *diffTypes) Set(value string) error {
160+
// Dedupe repeated elements.
161+
for _, t := range *d {
162+
if t == value {
163+
return nil
164+
}
165+
}
166+
*d = append(*d, value)
167+
return nil
168+
}
169+
170+
func (d *diffTypes) Type() string {
171+
return "Diff Types"
172+
}
173+
149174
func addSharedFlags(cmd *cobra.Command) {
150175
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).")
151-
cmd.Flags().StringVarP(&types, "types", "t", "apt", "This flag sets the list of analyzer types to use. It expects a comma separated list of supported analyzers.")
176+
cmd.Flags().VarP(&types, "type", "t", "This flag sets the list of analyzer types to use. Set it repeatedly to use multiple analyzers.")
152177
cmd.Flags().BoolVarP(&save, "save", "s", false, "Set this flag to save rather than remove the final image filesystems on exit.")
153178
cmd.Flags().BoolVarP(&util.SortSize, "order", "o", false, "Set this flag to sort any file/package results by descending size. Otherwise, they will be sorted by name.")
154179
}

0 commit comments

Comments
 (0)