This commit is contained in:
b1ek 2024-06-02 21:56:48 +10:00
parent c8975a2395
commit e6f2770869
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 35 additions and 3 deletions

View File

@ -8,6 +8,19 @@ bshchk '' with_bshchk.sh # will read from stdin
bshchk # will read from stdin, and write to 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 ## license
``` ```
bshchk - a bash runtime dependency checker bshchk - a bash runtime dependency checker

View File

@ -6,6 +6,26 @@ import (
"mvdan.cc/sh/v3/syntax" "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) { func find(code string) ([]string, error) {
r := strings.NewReader(code) r := strings.NewReader(code)
f, err := syntax.NewParser().Parse(r, "") f, err := syntax.NewParser().Parse(r, "")
@ -13,8 +33,7 @@ func find(code string) ([]string, error) {
return make([]string, 0), err 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"} ignored, deps := get_ignored_and_deps(code)
var deps []string
syntax.Walk(f, func(node syntax.Node) bool { syntax.Walk(f, func(node syntax.Node) bool {
switch x := node.(type) { switch x := node.(type) {
case *syntax.CallExpr: case *syntax.CallExpr:
@ -36,7 +55,7 @@ func find(code string) ([]string, error) {
for _, dep := range deps { for _, dep := range deps {
is_builtin := false is_builtin := false
for _, builtin := range builtins { for _, builtin := range ignored {
if dep == builtin { if dep == builtin {
is_builtin = true is_builtin = true
} }