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

View file

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