refact: updated to islazy 1.8.0

This commit is contained in:
evilsocket 2018-10-15 15:01:57 -05:00
commit 90bb05cd5b
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
6 changed files with 96 additions and 54 deletions

View file

@ -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
})
}