From c49589611e624fa98641bc91a3e53efc63e4543e Mon Sep 17 00:00:00 2001 From: Jonas Plum Date: Wed, 12 Jan 2022 22:29:43 +0100 Subject: [PATCH] Add ignore flag --- check.go | 35 +++++++++++++++++++++++++++++++++-- go.cap | 2 +- main.go | 7 ++++--- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/check.go b/check.go index 885fcac..3f7d4ec 100644 --- a/check.go +++ b/check.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "regexp" "sort" ) @@ -22,7 +23,6 @@ func (e *PackageNotProvided) Error() string { return fmt.Sprintf("package '%s' not listed in go.cap file, please add to go.cap file", e.Package) } - type UnnecessaryCapability struct { Capability string } @@ -39,7 +39,7 @@ func (e *CapabilityNotProvided) Error() string { return fmt.Sprintf("capability '%s' not provided by go.cap file, add to go.cap file if you want to grant the capability", e.Capability) } -func checkCmd(path string) { +func checkCmd(path string, ignore string) { file, err := parseGoCap(path) if err != nil { fmt.Println(err) @@ -52,6 +52,11 @@ func checkCmd(path string) { os.Exit(1) } + if ignore != "" { + filterTree(tree, ignore) + file.External = filterFile(file, ignore) + } + packageErrors := check(tree, file) if len(packageErrors) > 0 { @@ -74,6 +79,32 @@ func checkCmd(path string) { } } +func filterTree(tree *Tree, ignore string) { + for name := range tree.nodes { + match, err := regexp.MatchString(ignore, name) + if err != nil { + continue + } + if match { + delete(tree.nodes, name) + } + } +} + +func filterFile(file *File, ignore string) []Line { + var external []Line + for _, line := range file.External { + match, err := regexp.MatchString(ignore, line.Package) + if err != nil { + continue + } + if !match { + external = append(external, line) + } + } + return external +} + func sortedPackageErrorKeys(m map[string][]error) (keys []string) { for key := range m { keys = append(keys, key) diff --git a/go.cap b/go.cap index c8a9923..67882b3 100644 --- a/go.cap +++ b/go.cap @@ -1,4 +1,4 @@ github.com/cugu/gocap (execute, file) -github.com/alecthomas/kong (file, syscall, reflect, unsafe) +github.com/alecthomas/kong (file, reflect, unsafe, syscall) github.com/alecthomas/participle/v2 (reflect) diff --git a/main.go b/main.go index 9115a7e..6a1c3f9 100644 --- a/main.go +++ b/main.go @@ -8,10 +8,11 @@ import ( var CLI struct { Generate struct { - Path string `arg:"" name:"path" help:"..."` + Path string `arg:"" name:"path" help:"Path to the package to generate the go.cap file for"` } `cmd:"" help:"Generate go.cap"` Check struct { - Path string `arg:"" name:"path" help:"..."` + Path string `arg:"" name:"path" help:"Path to the package to check"` + Ignore string `help:"Ignore the package itself."` } `cmd:"" help:"Check go.cap"` } @@ -23,7 +24,7 @@ func main() { case "generate ": generate(CLI.Generate.Path) case "check ": - checkCmd(CLI.Check.Path) + checkCmd(CLI.Check.Path, CLI.Check.Ignore) default: panic(ctx.Command()) }