From 69cc90466294b2bf79b8c08ac8e42f2b4fc31183 Mon Sep 17 00:00:00 2001 From: b1ek Date: Sun, 2 Jun 2024 20:19:20 +1000 Subject: [PATCH] add tags --- README.md | 13 +++++++++++++ finder.go | 25 ++++++++++++++++++++++--- test.sh | 5 +++++ test.sho | 31 +++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 test.sh create mode 100755 test.sho 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 } diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..8ea0b98 --- /dev/null +++ b/test.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +#bshchk:add-cmd curl +#bshchk:ignore-cmd jdfg +jdfg diff --git a/test.sho b/test.sho new file mode 100755 index 0000000..c8595b5 --- /dev/null +++ b/test.sho @@ -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