Compare commits
3 Commits
8cd4d4663a
...
96d42c6385
Author | SHA1 | Date |
---|---|---|
b1ek | 96d42c6385 | |
b1ek | 388ab8f020 | |
b1ek | 35ed194361 |
|
@ -18,6 +18,10 @@ tags
|
|||
|
||||
# You can add/ignore multiple commands at once:
|
||||
#bshchk:add-cmd curl wget
|
||||
|
||||
# To ignore next line:
|
||||
#bshchk:ignore-next-line
|
||||
curl # won't be checked
|
||||
```
|
||||
|
||||
|
||||
|
|
23
finder.go
23
finder.go
|
@ -6,13 +6,22 @@ import (
|
|||
"mvdan.cc/sh/v3/syntax"
|
||||
)
|
||||
|
||||
var ignored_lines = []uint{}
|
||||
|
||||
func get_ignored_and_deps(code string) ([]string, []string) {
|
||||
// source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html
|
||||
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") {
|
||||
for i, line := range strings.Split(code, "\n") {
|
||||
splitted := strings.Split(line, " ")
|
||||
|
||||
// comments w/ no arguments
|
||||
if splitted[0] == "#bshchk:ignore-next-line" {
|
||||
ignored_lines = append(ignored_lines, uint(i)+1)
|
||||
}
|
||||
|
||||
// comments with arguments
|
||||
if len(splitted) < 2 {
|
||||
continue
|
||||
}
|
||||
|
@ -27,6 +36,15 @@ func get_ignored_and_deps(code string) ([]string, []string) {
|
|||
return ignored, deps
|
||||
}
|
||||
|
||||
func is_line_ignored(line uint) bool {
|
||||
for _, ignored := range ignored_lines {
|
||||
if line == ignored {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func find(code string) ([]string, error) {
|
||||
r := strings.NewReader(code)
|
||||
f, err := syntax.NewParser().Parse(r, "")
|
||||
|
@ -50,6 +68,9 @@ func find(code string) ([]string, error) {
|
|||
syntax.Walk(f, func(node syntax.Node) bool {
|
||||
switch x := node.(type) {
|
||||
case *syntax.CallExpr:
|
||||
if is_line_ignored(node.Pos().Line() - 1) {
|
||||
return true
|
||||
}
|
||||
for i := range x.Args {
|
||||
for _, part := range x.Args[i].Parts {
|
||||
switch xx := part.(type) {
|
||||
|
|
40
main.go
40
main.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
@ -15,10 +16,18 @@ var args struct {
|
|||
File string `arg:"positional" help:"if not specified, will read from stdin" default:""`
|
||||
Outfile string `arg:"positional" help:"if not specified, will emit to stdout" default:""`
|
||||
Version bool `arg:"-v" help:"print version and exit"`
|
||||
YieldDepsOnly bool `arg:"--yield-deps-only" help:"print dependencies as a JSON array and exit" default:false`
|
||||
ExposeDeps bool `arg:"--expose-deps" help:"expose dependencies to program" default:false`
|
||||
YieldDepsOnly bool `arg:"--yield-deps-only" help:"print dependencies as a JSON array and exit" default:"false"`
|
||||
ExposeDeps bool `arg:"--expose-deps" help:"expose dependencies to program" default:"false"`
|
||||
DepsVarName string `arg:"--deps-var-name" help:"override deps variable name" default:"deps"`
|
||||
IgnoreShebang bool `arg:"--ignore-shebang" help:"ignore shebang requirement" default:false`
|
||||
IgnoreShebang bool `arg:"--ignore-shebang" help:"ignore shebang requirement" default:"false"`
|
||||
}
|
||||
|
||||
func emit(code string) {
|
||||
if args.Outfile == "" {
|
||||
fmt.Printf("%s", code)
|
||||
} else {
|
||||
os.WriteFile(args.Outfile, []byte(code), os.FileMode(0o755))
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -55,6 +64,24 @@ func main() {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
if args.YieldDepsOnly {
|
||||
marshaled, err := json.Marshal(found)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if string(marshaled) == "null" {
|
||||
emit("[]")
|
||||
} else {
|
||||
emit(string(marshaled))
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if len(found) == 0 {
|
||||
emit(code)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
codelines := strings.Split(code, "\n")
|
||||
|
||||
if len(codelines) < 2 {
|
||||
|
@ -70,10 +97,5 @@ func main() {
|
|||
}
|
||||
|
||||
gen := shebang + "\n\n" + gencode(found) + "\n\n" + strings.Join(codelines[1:], "\n")
|
||||
|
||||
if args.Outfile == "" {
|
||||
fmt.Printf("%s", gen)
|
||||
} else {
|
||||
os.WriteFile(args.Outfile, []byte(gen), os.FileMode(0o755))
|
||||
}
|
||||
emit(gen)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue