-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented an 'ignore line' content filter
- Loading branch information
Showing
11 changed files
with
245 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/urfave/cli/v2" | ||
"golang.org/x/exp/slices" | ||
|
||
"github.com/crashdump/edmgen/pkg/edm" | ||
"github.com/crashdump/edmgen/pkg/filters/content" | ||
|
@@ -14,8 +16,17 @@ import ( | |
|
||
var logger *logging | ||
|
||
const STDOUT = "stdout" | ||
|
||
func init() { | ||
cli.HelpFlag = &cli.BoolFlag{Name: "help"} | ||
cli.VersionFlag = &cli.BoolFlag{Name: "version", Aliases: []string{"v"}} | ||
} | ||
|
||
func main() { | ||
logger = newLogger() | ||
var flagOutput string | ||
var flagFormat string | ||
|
||
logger.print("┌────────┐") | ||
logger.print("│ EDMGEN │") | ||
|
@@ -24,83 +35,127 @@ func main() { | |
|
||
app := &cli.App{ | ||
Name: "edmgen", | ||
Usage: "Walk files in a directory and extract samples for the purporse of Exact Data Match DLPs", | ||
Usage: "Walk files in a directory and extract (samples) lines", | ||
Compiled: time.Now(), | ||
Authors: []*cli.Author{{ | ||
Name: "Adrien Pujol", | ||
Email: "[email protected]", | ||
}}, | ||
Flags: []cli.Flag{ | ||
&cli.StringSliceFlag{Name: "extensions", Aliases: []string{"E"}}, | ||
&cli.StringFlag{ | ||
Name: "output", | ||
Aliases: []string{"o"}, | ||
Destination: &flagOutput, | ||
Action: func(ctx *cli.Context, v string) error { | ||
if v == "" { | ||
return errors.New("please specify a filename") | ||
} | ||
return nil | ||
}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "format", | ||
Aliases: []string{"f"}, | ||
DefaultText: STDOUT, | ||
Destination: &flagFormat, | ||
Action: func(ctx *cli.Context, v string) error { | ||
formats := []string{STDOUT, "csv", "txt"} | ||
if slices.Contains[[]string](formats, flagFormat) { | ||
return fmt.Errorf("output %s not currently supported", flagFormat) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
Before: func(cCtx *cli.Context) error { | ||
if cCtx.Args().Len() < 1 { | ||
logger.printFatal("You need to specify a path to a folder") | ||
logger.print("You need to specify a path to a folder") | ||
os.Exit(1) | ||
} | ||
return nil | ||
}, | ||
Action: run, | ||
} | ||
Action: func(cCtx *cli.Context) error { | ||
edmc, err := edm.New(edm.Opts{}) | ||
if err != nil { | ||
logger.printFatal(err.Error()) | ||
} | ||
|
||
if err := app.Run(os.Args); err != nil { | ||
logger.printFatal(err.Error()) | ||
} | ||
} | ||
path := cCtx.Args().First() | ||
|
||
func run(cCtx *cli.Context) error { | ||
edmc, err := edm.New(edm.Opts{}) | ||
if err != nil { | ||
logger.printFatal(err.Error()) | ||
} | ||
/* | ||
* Search for all the relevant files | ||
*/ | ||
logger.printHeader("Searching for relevant files...") | ||
err = edmc.SelectFiles(path, | ||
file.IgnoreFilename(ignoreFilenames), | ||
file.IgnoreDirname(ignoreDirnames), | ||
file.RequireExtensions(requireExtentions), | ||
) | ||
if err != nil { | ||
logger.printFatal(err.Error()) | ||
} | ||
logger.printfResult("Found %d files.", len(edmc.Paths)) | ||
logger.print("") | ||
|
||
path := cCtx.Args().First() | ||
|
||
/* | ||
* Search for all the relevant files | ||
*/ | ||
logger.printHeader("Searching for relevant files...") | ||
err = edmc.SelectFiles(path, | ||
file.IgnoreFilename(ignoreFilenames), | ||
file.IgnoreDirname(ignoreDirnames), | ||
file.RequireExtensions(requireExtentions), | ||
) | ||
if err != nil { | ||
logger.printFatal(err.Error()) | ||
/* | ||
* Walk through each of the files and sample lines | ||
*/ | ||
logger.printHeader("Examining files...") | ||
err = edmc.ExamineFiles( | ||
content.IgnoreLine("serialVersionUID"), | ||
content.LineLength(60, 120, true), | ||
content.LongestLine, | ||
) | ||
if err != nil { | ||
logger.printFatal(err.Error()) | ||
} | ||
logger.printfResult("Selected %d lines.", len(edmc.Content)) | ||
logger.print("") | ||
|
||
/* | ||
* Sample the lines previously selected down to the result | ||
*/ | ||
logger.printHeader("Sampling content...") | ||
lines := edmc.SampleContent( | ||
content.Uniq, | ||
) | ||
logger.printfResult("Sampled down to %d lines", len(lines)) | ||
logger.print("") | ||
|
||
/* | ||
* Finally, output the result | ||
*/ | ||
switch flagFormat { | ||
case "txt": | ||
writeFileTxt(flagOutput, lines) | ||
|
||
case "csv": | ||
writeFileCsv(flagOutput, lines) | ||
|
||
default: | ||
writeStdout(lines) | ||
} | ||
|
||
logger.print("Complete!") | ||
return nil | ||
}, | ||
} | ||
logger.printfResult("Found %d files.", len(edmc.Paths)) | ||
logger.print("") | ||
|
||
/* | ||
* Walk through each of the files and sample lines | ||
*/ | ||
logger.printHeader("Examining files...") | ||
err = edmc.ExamineFiles( | ||
content.LineLength(40, 100), | ||
content.LongestLine, | ||
) | ||
if err != nil { | ||
if err := app.Run(os.Args); err != nil { | ||
logger.printFatal(err.Error()) | ||
} | ||
logger.printfResult("Selected %d lines.", len(edmc.Content)) | ||
logger.print("") | ||
|
||
/* | ||
* Sample the lines previously selected down to the result | ||
*/ | ||
logger.printHeader("Sampling content...") | ||
lines := edmc.SampleContent( | ||
content.Uniq, | ||
) | ||
logger.printfResult("Sampled down to %d lines", len(edmc.Content)) | ||
logger.print("") | ||
} | ||
|
||
/* | ||
* Finally, output the result | ||
*/ | ||
func writeStdout(lines []string) { | ||
for _, line := range lines { | ||
fmt.Println(line) | ||
} | ||
} | ||
|
||
func writeFileTxt(filename string, lines []string) { | ||
|
||
} | ||
|
||
func writeFileCsv(filename string, lines []string) { | ||
|
||
logger.print("Complete!") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package content | ||
|
||
import "strings" | ||
|
||
func IgnoreLine(str string) func(content []string) (result []string) { | ||
return func(content []string) (result []string) { | ||
for _, line := range content { | ||
if strings.Contains(line, str) { | ||
continue | ||
} | ||
result = append(result, line) | ||
} | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package content_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/crashdump/edmgen/pkg/filters/content" | ||
) | ||
|
||
func Test_IgnoreLine(t *testing.T) { | ||
type args struct { | ||
string string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
content []string | ||
want []string | ||
}{ | ||
{ | ||
name: "empty-string", | ||
content: []string{ | ||
"Donec a dui et dui fringilla consectetur id nec massa.", | ||
" Nulla aliquet porttitor venenatis.", | ||
"Nam tristique maximus ante hendrerit aliquet.", | ||
"Integer dignissim posuere lobortis.", | ||
" Aenean ultrices erat ut augue ultrices", | ||
"Suspendisse lacinia ante nunc, pulvinar blandit nisl ornare ut.", | ||
}, | ||
args: args{ | ||
string: "", | ||
}, | ||
want: []string(nil), | ||
}, | ||
{ | ||
name: "ignore-maximus", | ||
content: []string{ | ||
"Donec a dui et dui fringilla consectetur id nec massa.", | ||
"Nulla aliquet porttitor venenatis.", | ||
"Nam tristique maximus ante hendrerit aliquet.", | ||
"Integer dignissim posuere lobortis.", | ||
" Aenean ultrices erat ut augue ultrices", | ||
"Suspendisse lacinia ante nunc, pulvinar blandit nisl ornare ut.", | ||
}, | ||
args: args{ | ||
string: "maximus", | ||
}, | ||
want: []string{ | ||
"Donec a dui et dui fringilla consectetur id nec massa.", | ||
"Nulla aliquet porttitor venenatis.", | ||
"Integer dignissim posuere lobortis.", | ||
" Aenean ultrices erat ut augue ultrices", | ||
"Suspendisse lacinia ante nunc, pulvinar blandit nisl ornare ut.", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := content.IgnoreLine(tt.args.string)(tt.content) | ||
assert.EqualValues(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.