mirror of
https://github.com/bettercap/bettercap
synced 2025-08-20 13:33:21 -07:00
misc: refactored caplets code in a dedicated package
This commit is contained in:
parent
d16b0c7cf5
commit
9721c1d6e0
7 changed files with 209 additions and 157 deletions
53
caplets/caplet.go
Normal file
53
caplets/caplet.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package caplets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Caplet struct {
|
||||
Name string
|
||||
Path string
|
||||
Size int64
|
||||
Code []string
|
||||
}
|
||||
|
||||
func (cap *Caplet) Eval(argv []string, lineCb func(line string) error) error {
|
||||
// the caplet might include other files (include directive, proxy modules, etc),
|
||||
// temporarily change the working directory
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error while getting current working directory: %v", err)
|
||||
}
|
||||
|
||||
capPath := filepath.Dir(cap.Path)
|
||||
if err := os.Chdir(capPath); err != nil {
|
||||
return fmt.Errorf("error while changing current working directory: %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := os.Chdir(cwd); err != nil {
|
||||
fmt.Printf("error while restoring working directory: %v\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if argv == nil {
|
||||
argv = []string{}
|
||||
}
|
||||
|
||||
for _, line := range cap.Code {
|
||||
// replace $0 with argv[0], $1 with argv[1] and so on
|
||||
for i, arg := range argv {
|
||||
what := fmt.Sprintf("$%d", i)
|
||||
line = strings.Replace(line, what, arg, -1)
|
||||
}
|
||||
|
||||
if err = lineCb(line); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue