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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
package shell
import (
"bufio"
"errors"
"io/fs"
"os/exec"
"path/filepath"
"pik/identity"
"pik/indexers/pikdex"
"pik/model"
"pik/runner"
"pik/spool"
"slices"
"strings"
)
//TODO: Clean up shell selection? Maybe default to bash?
var NoContentError = errors.New("not enough content in target")
var NoShellError = errors.New("could not find any shell interpreters")
var ExtShellMap = map[string]string{
".sh": "bash",
".ps1": "powershell",
}
var Shells = []string{"bash", "bash.exe", "zsh", "fish", "powershell", "powershell.exe", "cmd.exe"}
var Runner = &shell{
Locations: map[string]string{},
}
type shell struct {
Locations map[string]string
}
var WrongTargetError = errors.New("wrong target type")
func (s *shell) Hydrate(target model.Target) (model.HydratedTarget, error) {
cast, ok := target.(*ShellTarget)
if !ok {
return nil, WrongTargetError
}
hyd := &HydratedShellTarget{BaseHydration: runner.Hydrated(cast)}
return hyd, nil
}
func (s *shell) Wants(f fs.FS, file string, entry fs.DirEntry) (bool, error) {
if entry != nil && entry.IsDir() {
return false, nil
}
fd, err := f.Open(file)
if err != nil {
return false, err
}
scanner := bufio.NewScanner(fd)
scanner.Split(bufio.ScanRunes)
if !scanner.Scan() {
return false, nil
}
txt := scanner.Text()
if txt == "#" { //
return true, nil
}
for k, _ := range ExtShellMap {
if strings.HasSuffix(file, k) {
return true, nil
}
}
return false, nil
}
func (s *shell) Find(shell string) (string, error) {
if s.Locations[shell] != "" {
return s.Locations[shell], nil
}
if p, err := exec.LookPath(shell); err == nil {
s.Locations[shell] = p
return shell, nil
} else {
return "", err
}
}
func (s *shell) CreateTarget(fs fs.FS, src string, file string, _ fs.DirEntry) (model.Target, error) {
shell, err := s.ShellFor(fs, file)
if err != nil {
return nil, err
}
_, filename := filepath.Split(file)
var sub []string
split := strings.Split(file, "/")
for _, p := range split {
if slices.Contains(pikdex.Roots, p) {
continue
}
if filename == p {
continue
}
sub = append(sub, p)
}
return &ShellTarget{
BaseTarget: runner.BaseTarget{
Identity: identity.New(filename),
MyTags: model.TagsFromFilename(filename),
},
Shell: shell,
Script: file,
SubValue: sub,
}, nil
}
func (s *shell) ShellFor(fs fs.FS, file string) (string, error) {
var shell, shebang string
// low-hanging fruit - indicative filename
if byFile := s.ShellByFilename(file); byFile != "" {
return byFile, nil
}
fd, err := fs.Open(file)
if err != nil {
return "", err
}
scanner := bufio.NewScanner(fd)
scanner.Split(bufio.ScanLines)
if !scanner.Scan() {
return "", NoContentError
}
txt := scanner.Text()
if strings.HasPrefix(txt, "#!") {
// shebang found
for _, potentialShell := range Shells {
if strings.Contains(txt, potentialShell) {
shebang = shell
if loc, err := s.Find(potentialShell); err == nil {
shell = loc
} else {
_, _ = spool.Warn("script has %s but could not find %s (%s)\n", shebang, potentialShell)
}
}
}
}
if shebang == "" {
// if no shebang, just send the first one we find
for _, s := range Shells {
if p, err := exec.LookPath(s); err != nil {
shell = p
}
}
}
if shell == "" {
return "", NoShellError
}
return shell, nil
}
func (s *shell) ShellByFilename(file string) string {
ext := filepath.Ext(file)
if ExtShellMap[ext] != "" {
sh, err := s.Find(ExtShellMap[ext])
if err == nil {
return sh
}
}
return ""
}
|