From e6f2770869a8ae596cd77fcf3e5cda70bc015d9f Mon Sep 17 00:00:00 2001 From: b1ek Date: Sun, 2 Jun 2024 21:56:48 +1000 Subject: [PATCH] add tags --- README.md | 13 +++++++++++++ finder.go | 25 ++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 875730c..bba959f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,19 @@ bshchk '' with_bshchk.sh # will read from stdin bshchk # will read from stdin, and write to stdin ``` +tags +```sh +# To explicitly add curl: +#bshchk:add-cmd curl + +# To disable checking for one curl: +#bshchk:ignore-cmd curl + +# You can add/ignore multiple commands at once: +#bshchk:add-cmd curl wget +``` + + ## license ``` bshchk - a bash runtime dependency checker diff --git a/finder.go b/finder.go index 80fe2e5..fd85517 100644 --- a/finder.go +++ b/finder.go @@ -6,6 +6,26 @@ import ( "mvdan.cc/sh/v3/syntax" ) +func get_ignored_and_deps(code string) ([]string, []string) { + var ignored []string = []string{"alias", "bind", "builtin", "caller", "command", "declare", "echo", "enable", "let", "local", "logout", "mapfile", "printf", "read", "readarray", "source", "type", "typeset", "ulimit", "unalias"} + var deps []string + + for _, line := range strings.Split(code, "\n") { + splitted := strings.Split(line, " ") + if len(splitted) < 2 { + continue + } + if splitted[0] == "#bshchk:ignore-cmd" { + ignored = append(ignored, splitted[1:]...) + } + if splitted[0] == "#bshchk:add-cmd" { + deps = append(deps, splitted[1:]...) + } + } + + return ignored, deps +} + func find(code string) ([]string, error) { r := strings.NewReader(code) f, err := syntax.NewParser().Parse(r, "") @@ -13,8 +33,7 @@ func find(code string) ([]string, error) { return make([]string, 0), err } - var builtins = [...]string{"alias", "bind", "builtin", "caller", "command", "declare", "echo", "enable", "help", "let", "local", "logout", "mapfile", "printf", "read", "readarray", "source", "type", "typeset", "ulimit", "unalias"} - var deps []string + ignored, deps := get_ignored_and_deps(code) syntax.Walk(f, func(node syntax.Node) bool { switch x := node.(type) { case *syntax.CallExpr: @@ -36,7 +55,7 @@ func find(code string) ([]string, error) { for _, dep := range deps { is_builtin := false - for _, builtin := range builtins { + for _, builtin := range ignored { if dep == builtin { is_builtin = true }