| 1 | // Copyright (c) 2016, Daniel Martà <[email protected]> |
| 2 | // See LICENSE for licensing information |
| 3 | |
| 4 | // Package fileutil allows inspecting shell files, such as detecting whether a |
| 5 | // file may be shell or extracting its shebang. |
| 6 | package fileutilscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ |
| 7 | |
| 8 | import ( |
| 9 | "io/fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/" |
| 10 | "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/" |
| 11 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | shebangRescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/shebangRe. = regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`^#![ \t]*/(usr/)?bin/(env[ \t]+)?(sh|bash|mksh|bats|zsh)(\s|$)`) |
| 16 | extRescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/extRe. = regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`\.(sh|bash|mksh|bats|zsh)$`) |
| 17 | ) |
| 18 | |
| 19 | // TODO: consider removing HasShebang in favor of Shebang in v4 |
| 20 | |
| 21 | // HasShebang reports whether bs begins with a valid shell shebang. |
| 22 | // It supports variations with /usr and env. |
| 23 | func HasShebangscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/HasShebang().(bs []byte) bool { |
| 24 | return Shebangscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/Shebang().(bs) != "" |
| 25 | } |
| 26 | |
| 27 | // Shebang parses a "#!" sequence from the beginning of the input bytes, |
| 28 | // and returns the shell that it points to. |
| 29 | // |
| 30 | // For instance, it returns "sh" for "#!/bin/sh", |
| 31 | // and "bash" for "#!/usr/bin/env bash". |
| 32 | func Shebangscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/Shebang().(bs []byte) string { |
| 33 | m := shebangRescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/shebangRe..FindSubmatchscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#FindSubmatch().(bs) |
| 34 | if m == nil { |
| 35 | return "" |
| 36 | } |
| 37 | return string(m[3]) |
| 38 | } |
| 39 | |
| 40 | // ScriptConfidence defines how likely a file is to be a shell script, |
| 41 | // from complete certainty that it is not one to complete certainty that |
| 42 | // it is one. |
| 43 | type ScriptConfidencescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ScriptConfidence# int |
| 44 | |
| 45 | const ( |
| 46 | // ConfNotScript describes files which are definitely not shell scripts, |
| 47 | // such as non-regular files or files with a non-shell extension. |
| 48 | ConfNotScriptscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ConfNotScript. ScriptConfidencescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ScriptConfidence# = iota |
| 49 | |
| 50 | // ConfIfShebang describes files which might be shell scripts, depending |
| 51 | // on the shebang line in the file's contents. Since [CouldBeScript] only |
| 52 | // works on [fs.FileInfo], the answer in this case can't be final. |
| 53 | ConfIfShebangscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ConfIfShebang. |
| 54 | |
| 55 | // ConfIsScript describes files which are definitely shell scripts, |
| 56 | // which are regular files with a valid shell extension. |
| 57 | ConfIsScriptscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ConfIsScript. |
| 58 | ) |
| 59 | |
| 60 | // CouldBeScript is a shortcut for CouldBeScript2(fs.FileInfoToDirEntry(info)). |
| 61 | // |
| 62 | // Deprecated: prefer [CouldBeScript2], which usually requires fewer syscalls. |
| 63 | func CouldBeScriptscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/CouldBeScript().(info fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.FileInfoscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#) ScriptConfidencescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ScriptConfidence# { |
| 64 | return CouldBeScript2scip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/CouldBeScript2().(fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.FileInfoToDirEntryscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfoToDirEntry().(info)) |
| 65 | } |
| 66 | |
| 67 | // CouldBeScript2 reports how likely a directory entry is to be a shell script. |
| 68 | // It discards directories and other non-regular files like symbolic links, |
| 69 | // filenames beginning with '.', and files with non-shell extensions. |
| 70 | func CouldBeScript2scip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/CouldBeScript2().(entry fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#) ScriptConfidencescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ScriptConfidence# { |
| 71 | name := entry.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().() |
| 72 | switch { |
| 73 | case name[0] == '.': |
| 74 | return ConfNotScriptscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ConfNotScript. // '.' prefix (hidden file) |
| 75 | case !entry.Typescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Type().().IsRegularscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileMode#IsRegular().(): |
| 76 | return ConfNotScriptscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ConfNotScript. // dir, symlink, named pipes, etc |
| 77 | case extRescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/extRe..MatchStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#MatchString().(name): |
| 78 | return ConfIsScriptscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ConfIsScript. // shell extension |
| 79 | case stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.IndexBytescip-go gomod github.com/golang/go/src go1.25.0 strings/IndexByte().(name, '.') > 0: |
| 80 | return ConfNotScriptscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ConfNotScript. // non-shell extension |
| 81 | default: |
| 82 | return ConfIfShebangscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/fileutil`/ConfIfShebang. // no extension; read and look for a shebang |
| 83 | } |
| 84 | } |