Skip to content

Commit 90a93ad

Browse files
committed
add a output type flag
1 parent a9699e4 commit 90a93ad

File tree

4 files changed

+68
-29
lines changed

4 files changed

+68
-29
lines changed

cmd/filter.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010
"io"
1111
"os"
12-
"slices"
1312

1413
"github.com/spf13/cobra"
1514

@@ -33,8 +32,7 @@ func setupFilterCommand() *cobraext.Command {
3332
filter.SetFilterFlags(cmd)
3433

3534
// add the output package name and absolute path flags to the command
36-
cmd.Flags().BoolP(cobraext.FilterOutputPackageNameFlagName, "", false, cobraext.FilterOutputPackageNameFlagDescription)
37-
cmd.Flags().BoolP(cobraext.FilterOutputAbsolutePathFlagName, "", false, cobraext.FilterOutputAbsolutePathFlagDescription)
35+
cmd.Flags().StringP(cobraext.FilterOutputFlagName, cobraext.FilterOutputFlagShorthand, cobraext.FilterOutputFlagDefault, cobraext.FilterOutputFlagDescription)
3836

3937
return cobraext.NewCommand(cmd, cobraext.ContextPackage)
4038
}
@@ -45,17 +43,17 @@ func filterCommandAction(cmd *cobra.Command, args []string) error {
4543
return fmt.Errorf("filtering packages failed: %w", err)
4644
}
4745

48-
printPackageName, err := cmd.Flags().GetBool(cobraext.FilterOutputPackageNameFlagName)
46+
outputFormatStr, err := cmd.Flags().GetString(cobraext.FilterOutputFlagName)
4947
if err != nil {
50-
return fmt.Errorf("getting output package name flag failed: %w", err)
48+
return fmt.Errorf("getting output format flag failed: %w", err)
5149
}
5250

53-
outputAbsolutePath, err := cmd.Flags().GetBool("output-absolute-path")
51+
outputFormat, err := filter.NewOutputFormat(outputFormatStr)
5452
if err != nil {
55-
return fmt.Errorf("getting output absolute path flag failed: %w", err)
53+
return fmt.Errorf("invalid output format: %w", err)
5654
}
5755

58-
if err = printPkgList(filtered, printPackageName, outputAbsolutePath, os.Stdout); err != nil {
56+
if err = printPkgList(filtered, outputFormat, os.Stdout); err != nil {
5957
return fmt.Errorf("printing JSON failed: %w", err)
6058
}
6159

@@ -91,28 +89,14 @@ func filterPackage(cmd *cobra.Command) ([]packages.PackageDirNameAndManifest, er
9189
return filtered, nil
9290
}
9391

94-
func printPkgList(pkgs []packages.PackageDirNameAndManifest, printPackageName bool, outputAbsolutePath bool, w io.Writer) error {
92+
func printPkgList(pkgs []packages.PackageDirNameAndManifest, outputFormat filter.OutputFormat, w io.Writer) error {
9593
enc := json.NewEncoder(w)
9694
enc.SetEscapeHTML(false)
97-
if len(pkgs) == 0 {
98-
return nil
99-
}
10095

101-
names := make([]string, 0, len(pkgs))
102-
if printPackageName {
103-
for _, pkg := range pkgs {
104-
names = append(names, pkg.Manifest.Name)
105-
}
106-
} else if outputAbsolutePath {
107-
for _, pkg := range pkgs {
108-
names = append(names, pkg.Path)
109-
}
110-
} else {
111-
for _, pkg := range pkgs {
112-
names = append(names, pkg.DirName)
113-
}
96+
names, err := outputFormat.ApplyTo(pkgs)
97+
if err != nil {
98+
return fmt.Errorf("applying output format failed: %w", err)
11499
}
115100

116-
slices.Sort(names)
117101
return enc.Encode(names)
118102
}

internal/cobraext/flags.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,10 @@ const (
154154
FilterOutputAbsolutePathFlagName = "output-absolute-path"
155155
FilterOutputAbsolutePathFlagDescription = "output the absolute path of the package"
156156

157-
FilterOutputPackageNameFlagName = "output-package-name"
158-
FilterOutputPackageNameFlagDescription = "print the package name instead of the directory name in the output"
157+
FilterOutputFlagName = "output"
158+
FilterOutputFlagDescription = "select the output format (pkgname, dirname, absolute-path)"
159+
FilterOutputFlagDefault = "dirname"
160+
FilterOutputFlagShorthand = "o"
159161

160162
FilterPackageDirNameFlagName = "package-dirs"
161163
FilterPackageDirNameFlagDescription = "package directories to filter by (comma-separated values)"

internal/filter/registry.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ func (r *FilterRegistry) Execute() (filtered []packages.PackageDirNameAndManifes
9393

9494
filtered = pkgs
9595
for _, filter := range r.filters {
96-
logger.Infof("Applying for %d packages", len(filtered))
9796
filtered, err = filter.ApplyTo(filtered)
9897
if err != nil {
9998
errors = append(errors, err)

internal/filter/type.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,66 @@ package filter
66

77
import (
88
"fmt"
9+
"slices"
910

1011
"github.com/spf13/cobra"
1112

1213
"github.com/elastic/elastic-package/internal/packages"
1314
)
1415

16+
type OutputFormat string
17+
18+
const (
19+
OutputFormatPackageName OutputFormat = "pkgname"
20+
OutputFormatDirectoryName OutputFormat = "dirname"
21+
OutputFormatAbsolutePath OutputFormat = "absolute"
22+
)
23+
24+
func OutputFormatsList() []OutputFormat {
25+
return []OutputFormat{OutputFormatPackageName, OutputFormatDirectoryName, OutputFormatAbsolutePath}
26+
}
27+
28+
func (o OutputFormat) String() string {
29+
return string(o)
30+
}
31+
32+
func NewOutputFormat(s string) (OutputFormat, error) {
33+
switch s {
34+
case string(OutputFormatPackageName):
35+
return OutputFormatPackageName, nil
36+
case string(OutputFormatDirectoryName):
37+
return OutputFormatDirectoryName, nil
38+
case string(OutputFormatAbsolutePath):
39+
return OutputFormatAbsolutePath, nil
40+
}
41+
return "", fmt.Errorf("invalid output format: %s", s)
42+
}
43+
44+
func (o OutputFormat) ApplyTo(pkgs []packages.PackageDirNameAndManifest) ([]string, error) {
45+
// if no packages are found, return an empty slice
46+
if len(pkgs) == 0 {
47+
return nil, nil
48+
}
49+
50+
// apply the output format to the packages
51+
output := make([]string, 0, len(pkgs))
52+
for _, pkg := range pkgs {
53+
switch o {
54+
case OutputFormatPackageName:
55+
output = append(output, pkg.Manifest.Name)
56+
case OutputFormatDirectoryName:
57+
output = append(output, pkg.DirName)
58+
case OutputFormatAbsolutePath:
59+
output = append(output, pkg.Path)
60+
}
61+
}
62+
63+
// sort the output
64+
slices.Sort(output)
65+
66+
return output, nil
67+
}
68+
1569
// FilterFlag defines the basic interface for filter flags.
1670
type FilterFlag interface {
1771
String() string

0 commit comments

Comments
 (0)