mirror of
https://github.com/bettercap/bettercap
synced 2025-08-21 14:03:17 -07:00
refact: updated to islazy 1.8.0
This commit is contained in:
parent
7f808ac059
commit
90bb05cd5b
6 changed files with 96 additions and 54 deletions
|
@ -2,9 +2,10 @@ package caplets
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/evilsocket/islazy/fs"
|
||||
)
|
||||
|
||||
type Caplet struct {
|
||||
|
@ -15,39 +16,23 @@ type Caplet struct {
|
|||
}
|
||||
|
||||
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{}
|
||||
}
|
||||
// the caplet might include other files (include directive, proxy modules, etc),
|
||||
// temporarily change the working directory
|
||||
return fs.Chdir(filepath.Dir(cap.Path), func() error {
|
||||
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)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
if err = lineCb(line); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package caplets
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -10,7 +9,6 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/evilsocket/islazy/fs"
|
||||
"github.com/evilsocket/islazy/str"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -68,24 +66,19 @@ func Load(name string) (error, *Caplet) {
|
|||
Path: filename,
|
||||
Code: make([]string, 0),
|
||||
}
|
||||
|
||||
input, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading caplet %s: %v", filename, err), nil
|
||||
}
|
||||
defer input.Close()
|
||||
|
||||
scanner := bufio.NewScanner(input)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
for scanner.Scan() {
|
||||
line := str.Trim(scanner.Text())
|
||||
if line == "" || line[0] == '#' {
|
||||
continue
|
||||
}
|
||||
cap.Code = append(cap.Code, line)
|
||||
}
|
||||
|
||||
cache[name] = cap
|
||||
|
||||
if reader, err := fs.LineReader(filename); err != nil {
|
||||
return fmt.Errorf("error reading caplet %s: %v", filename, err), nil
|
||||
} else {
|
||||
for line := range reader {
|
||||
if line == "" || line[0] == '#' {
|
||||
continue
|
||||
}
|
||||
cap.Code = append(cap.Code, line)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, cap
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue