Skip to content
This repository has been archived by the owner on May 21, 2023. It is now read-only.

Commit

Permalink
Add ignore flag
Browse files Browse the repository at this point in the history
  • Loading branch information
cugu committed Jan 12, 2022
1 parent d43922a commit c495896
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
35 changes: 33 additions & 2 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"regexp"
"sort"
)

Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.cap
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -23,7 +24,7 @@ func main() {
case "generate <path>":
generate(CLI.Generate.Path)
case "check <path>":
checkCmd(CLI.Check.Path)
checkCmd(CLI.Check.Path, CLI.Check.Ignore)
default:
panic(ctx.Command())
}
Expand Down

0 comments on commit c495896

Please sign in to comment.