-
Notifications
You must be signed in to change notification settings - Fork 3
/
shebang.go
49 lines (37 loc) · 838 Bytes
/
shebang.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package eventline
import (
"fmt"
"regexp"
"strings"
)
var (
shebangRE = regexp.MustCompile(`^#!\s*(\S+)\s*(.+)?`)
shebangSpaceRE = regexp.MustCompile(`\s`)
)
type Shebang struct {
Interpreter string
Argument string
}
func (s *Shebang) Parse(data string) error {
parts := strings.SplitN(data, "\n", 2)
line := strings.TrimRight(parts[0], " \n\r\t")
matches := shebangRE.FindAllStringSubmatch(line, -1)
if len(matches) < 1 {
return fmt.Errorf("invalid format")
}
groups := matches[0][1:]
s.Interpreter = groups[0]
if len(groups) >= 2 {
arg := groups[1]
if shebangSpaceRE.MatchString(arg) {
return fmt.Errorf("invalid space character in argument")
}
s.Argument = arg
} else {
s.Argument = ""
}
return nil
}
func StartsWithShebang(data string) bool {
return shebangRE.MatchString(data)
}