mirror of
https://github.com/bettercap/bettercap
synced 2025-07-16 10:03:39 -07:00
fix: working directory is temporarily switched when running a caplet in order to properly load/include files relative to the caplet itself
This commit is contained in:
parent
9a3a514f6a
commit
f213d0d0ca
4 changed files with 265 additions and 254 deletions
84
session/session_parse.go
Normal file
84
session/session_parse.go
Normal file
|
@ -0,0 +1,84 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/bettercap/bettercap/core"
|
||||
)
|
||||
|
||||
func ParseCommands(line string) []string {
|
||||
args := []string{}
|
||||
buf := ""
|
||||
|
||||
singleQuoted := false
|
||||
doubleQuoted := false
|
||||
finish := false
|
||||
|
||||
for _, c := range line {
|
||||
switch c {
|
||||
case ';':
|
||||
if !singleQuoted && !doubleQuoted {
|
||||
finish = true
|
||||
} else {
|
||||
buf += string(c)
|
||||
}
|
||||
|
||||
case '"':
|
||||
if doubleQuoted {
|
||||
// finish of quote
|
||||
doubleQuoted = false
|
||||
} else if singleQuoted {
|
||||
// quote initiated with ', so we ignore it
|
||||
buf += string(c)
|
||||
} else {
|
||||
// quote init here
|
||||
doubleQuoted = true
|
||||
}
|
||||
|
||||
case '\'':
|
||||
if singleQuoted {
|
||||
singleQuoted = false
|
||||
} else if doubleQuoted {
|
||||
buf += string(c)
|
||||
} else {
|
||||
singleQuoted = true
|
||||
}
|
||||
|
||||
default:
|
||||
buf += string(c)
|
||||
}
|
||||
|
||||
if finish {
|
||||
args = append(args, buf)
|
||||
finish = false
|
||||
buf = ""
|
||||
}
|
||||
}
|
||||
|
||||
if len(buf) > 0 {
|
||||
args = append(args, buf)
|
||||
}
|
||||
|
||||
cmds := make([]string, 0)
|
||||
for _, cmd := range args {
|
||||
cmd = core.Trim(cmd)
|
||||
if cmd != "" || (len(cmd) > 0 && cmd[0] != '#') {
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
}
|
||||
|
||||
return cmds
|
||||
}
|
||||
|
||||
func (s *Session) parseEnvTokens(str string) (string, error) {
|
||||
for _, m := range reEnvVarCapture.FindAllString(str, -1) {
|
||||
varName := strings.Trim(strings.Replace(m, "env.", "", -1), "{}")
|
||||
if found, value := s.Env.Get(varName); found {
|
||||
str = strings.Replace(str, m, value, -1)
|
||||
} else {
|
||||
return "", fmt.Errorf("variable '%s' is not defined", varName)
|
||||
}
|
||||
}
|
||||
return str, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue