| 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 fileutil |
| 7 |
|
| 8 |
import ( |
| 9 |
"io/fs" |
| 10 |
"regexp" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
var ( |
| 15 |
shebangRe = regexp.MustCompile(`^#![ \t]*/(usr/)?bin/(env[ \t]+)?(sh|dash|bash|mksh|bats|zsh)(\s|$)`) |
| 16 |
extRe = 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 HasShebang(bs []byte) bool { |
| 24 |
return 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 Shebang(bs []byte) string { |
| 33 |
m := shebangRe.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 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 |
ConfNotScript 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 |
ConfIfShebang |
| 54 |
|
| 55 |
// ConfIsScript describes files which are definitely shell scripts, |
| 56 |
// which are regular files with a valid shell extension. |
| 57 |
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 CouldBeScript(info fs.FileInfo) ScriptConfidence { |
| 64 |
return CouldBeScript2(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 CouldBeScript2(entry fs.DirEntry) ScriptConfidence { |
| 71 |
name := entry.Name() |
| 72 |
switch { |
| 73 |
case name[0] == '.': |
| 74 |
return ConfNotScript // '.' prefix (hidden file) |
| 75 |
case !entry.Type().IsRegular(): |
| 76 |
return ConfNotScript // dir, symlink, named pipes, etc |
| 77 |
case extRe.MatchString(name): |
| 78 |
return ConfIsScript // shell extension |
| 79 |
case strings.IndexByte(name, '.') > 0: |
| 80 |
return ConfNotScript // non-shell extension |
| 81 |
default: |
| 82 |
return ConfIfShebang // no extension; read and look for a shebang |
| 83 |
} |
| 84 |
} |