misc: refactored caplets code in a dedicated package

This commit is contained in:
evilsocket 2018-09-21 15:15:27 +02:00
commit 9721c1d6e0
7 changed files with 209 additions and 157 deletions

93
caplets/manager.go Normal file
View file

@ -0,0 +1,93 @@
package caplets
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"github.com/bettercap/bettercap/core"
)
var (
cache = make(map[string]*Caplet)
cacheLock = sync.Mutex{}
)
func List() []Caplet {
caplets := make([]Caplet, 0)
cwd, _ := filepath.Abs(".")
for _, searchPath := range append([]string{cwd}, LoadPaths...) {
files, _ := filepath.Glob(searchPath + "/*" + Suffix)
files2, _ := filepath.Glob(searchPath + "/*/*" + Suffix)
for _, fileName := range append(files, files2...) {
if stats, err := os.Stat(fileName); err == nil {
base := strings.Replace(fileName, searchPath+"/", "", -1)
base = strings.Replace(base, Suffix, "", -1)
caplets = append(caplets, Caplet{
Name: base,
Path: fileName,
Size: stats.Size(),
})
}
}
}
return caplets
}
func Load(name string) (error, *Caplet) {
cacheLock.Lock()
defer cacheLock.Unlock()
if caplet, found := cache[name]; found {
return nil, caplet
}
names := []string{name}
if !strings.HasSuffix(name, Suffix) {
names = append(names, name+Suffix)
}
for _, path := range LoadPaths {
if !strings.HasSuffix(name, Suffix) {
name += Suffix
}
names = append(names, filepath.Join(path, name))
}
for _, filename := range names {
if core.Exists(filename) {
cap := &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 := core.Trim(scanner.Text())
if line == "" || line[0] == '#' {
continue
}
cap.Code = append(cap.Code, line)
}
cache[name] = cap
return nil, cap
}
}
return fmt.Errorf("caplet %s not found", name), nil
}