This commit is contained in:
b1ek 2024-06-02 20:19:20 +10:00
parent c8975a2395
commit 69cc904662
Signed by: blek
GPG Key ID: 14546221E3595D0C
4 changed files with 71 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
```
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

View File

@ -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
}

5
test.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/bash
#bshchk:add-cmd curl
#bshchk:ignore-cmd jdfg
jdfg

31
test.sho Executable file
View File

@ -0,0 +1,31 @@
#!/bin/bash
# This is the runtime dependency checker
# Please do not remove the following lines.
deps=('curl')
non_ok=()
for d in $deps
do
if ! command -v $d > /dev/null 2>&1; then
non_ok+=$d
fi
done
if (( ${#non_ok[@]} != 0 )); then
>&2 echo "RDC Failed!"
>&2 echo " This program requires these commands:"
>&2 echo " > $deps"
>&2 echo " --- "
>&2 echo " From which, these are missing:"
>&2 echo " > $non_ok"
>&2 echo "Make sure that those are installed and are present in \$PATH."
fi
unset non_ok
unset deps
# Dependencies are OK at this point
#bshchk:add-cmd curl
#bshchk:ignore-cmd jdfg
jdfg