Skip to content

Commit

Permalink
Merge pull request #22 from catatsuy/feature_use_exclude
Browse files Browse the repository at this point in the history
Update CLI filters with excludes option
  • Loading branch information
catatsuy authored Mar 31, 2024
2 parents eb42e57 + 7aa592a commit 8740fca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
20 changes: 10 additions & 10 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type CLI struct {
replaceExpr string
isOverwrite bool
filters rawStrings
notFilters rawStrings
excludes rawStrings
help bool
}

Expand Down Expand Up @@ -106,20 +106,20 @@ func (c *CLI) Run(args []string) int {
}
}

if len(c.filters) > 0 || len(c.notFilters) > 0 {
if len(c.filters) > 0 || len(c.excludes) > 0 {
filters, err := compileRegexps(c.filters)
if err != nil {
fmt.Fprintf(c.errStream, "Failed to compile regex patterns: %s\n", err)
return ExitCodeFail
}

notFilters, err := compileRegexps(c.notFilters)
excludes, err := compileRegexps(c.excludes)
if err != nil {
fmt.Fprintf(c.errStream, "Failed to compile regex patterns: %s\n", err)
return ExitCodeFail
}

err = c.filterProcess(filters, notFilters)
err = c.filterProcess(filters, excludes)
if err != nil {
fmt.Fprintf(c.errStream, "Failed to process files: %s\n", err)
return ExitCodeFail
Expand All @@ -142,8 +142,8 @@ func (c *CLI) parseFlags(args []string) (*flag.FlagSet, error) {

flags.BoolVar(&c.isOverwrite, "overwrite", false, "overwrite the file in place")
flags.StringVar(&c.replaceExpr, "replace", "", `Replacement expression, e.g., "@search@replace@"`)
flags.Var(&c.filters, "filter", `Filter expression`)
flags.Var(&c.notFilters, "not-filter", `Not filter expression`)
flags.Var(&c.filters, "filter", `filter expression`)
flags.Var(&c.excludes, "exclude", `exclude expression`)
flags.BoolVar(&c.help, "help", false, `Show help`)

flags.Usage = func() {
Expand All @@ -168,11 +168,11 @@ func (c *CLI) validateInput(flags *flag.FlagSet) error {
return fmt.Errorf("cannot use -overwrite option with stdin")
}

if len(c.replaceExpr) != 0 && (len(c.filters) != 0 || len(c.notFilters) != 0) {
if len(c.replaceExpr) != 0 && (len(c.filters) != 0 || len(c.excludes) != 0) {
return fmt.Errorf("cannot use -replace and -filter options together")
}

if (len(c.filters) == 0 && len(c.notFilters) == 0) && len(c.replaceExpr) < 3 {
if (len(c.filters) == 0 && len(c.excludes) == 0) && len(c.replaceExpr) < 3 {
return fmt.Errorf("invalid replace expression format. Use \"@search@replace@\"")
}

Expand Down Expand Up @@ -209,12 +209,12 @@ func (c *CLI) replaceProcess(searchPattern, replacement string) error {
return nil
}

func (c *CLI) filterProcess(filters []*regexp.Regexp, notFilters []*regexp.Regexp) error {
func (c *CLI) filterProcess(filters []*regexp.Regexp, excludes []*regexp.Regexp) error {
scanner := bufio.NewScanner(c.inputStream)

for scanner.Scan() {
line := scanner.Text()
if (len(filters) == 0 || matchesFilters(line, filters)) && !matchesFilters(line, notFilters) {
if (len(filters) == 0 || matchesFilters(line, filters)) && !matchesFilters(line, excludes) {
fmt.Fprintln(c.outStream, line)
}
}
Expand Down
36 changes: 18 additions & 18 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ func TestRun_success(t *testing.T) {
expectedCode: 0,
},
{
desc: "-not-filter",
args: []string{"purl", "-not-filter", "not filter"},
desc: "-exclude",
args: []string{"purl", "-exclude", "not filter"},
expectedCode: 0,
},
{
desc: "multiple -not-filter",
args: []string{"purl", "-not-filter", "not filter", "-not-filter", "not filter2"},
desc: "multiple -exclude",
args: []string{"purl", "-exclude", "not filter", "-exclude", "not filter2"},
expectedCode: 0,
},
{
desc: "provide -filter and -not-filter",
args: []string{"purl", "-filter", "filter", "-not-filter", "not filter2"},
desc: "provide -filter and -exclude",
args: []string{"purl", "-filter", "filter", "-exclude", "not filter2"},
expectedCode: 0,
},
}
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestFilterProcess(t *testing.T) {
name string
input string
filters []string
notFilters []string
excludes []string
wantOutput string
}{
{
Expand Down Expand Up @@ -273,34 +273,34 @@ func TestFilterProcess(t *testing.T) {
wantOutput: "",
},
{
name: "-not-filter: SingleMatch",
name: "-exclude: SingleMatch",
input: "apple\nbanana\ncherry\n",
notFilters: []string{"banana"},
excludes: []string{"banana"},
wantOutput: "apple\ncherry\n",
},
{
name: "-not-filter: MultipleMatches",
name: "-exclude: MultipleMatches",
input: "apple\nbanana\ncherry\n",
notFilters: []string{"apple", "cherry"},
excludes: []string{"apple", "cherry"},
wantOutput: "banana\n",
},
{
name: "-not-filter: NoMatch",
name: "-exclude: NoMatch",
input: "apple\nbanana\ncherry\n",
notFilters: []string{"date"},
excludes: []string{"date"},
wantOutput: "apple\nbanana\ncherry\n",
},
{
name: "-not-filter: EmptyInput",
name: "-exclude: EmptyInput",
input: "",
notFilters: []string{"apple"},
excludes: []string{"apple"},
wantOutput: "",
},
{
name: "provide filter and not filter",
input: "apple\nbanana\napple cherry\ncherry\n",
filters: []string{"apple"},
notFilters: []string{"cherry"},
excludes: []string{"cherry"},
wantOutput: "apple\n",
},
}
Expand All @@ -318,13 +318,13 @@ func TestFilterProcess(t *testing.T) {
return
}

notFilters, err := cli.CompileRegexps(tt.notFilters)
excludes, err := cli.CompileRegexps(tt.excludes)
if err != nil {
t.Errorf("CompileRegexps() error = %v", err)
return
}

err = cl.FilterProcess(filters, notFilters)
err = cl.FilterProcess(filters, excludes)
if err != nil {
t.Errorf("filterProcess() error = %v", err)
return
Expand Down

0 comments on commit 8740fca

Please sign in to comment.