mirror of
https://github.com/bettercap/bettercap
synced 2025-08-14 10:46:57 -07:00
new: preloading and exporting caplets from api.rest
This commit is contained in:
parent
6b9b27302e
commit
3edf80fc99
3 changed files with 67 additions and 17 deletions
|
@ -9,10 +9,19 @@ import (
|
|||
)
|
||||
|
||||
type Caplet struct {
|
||||
Name string
|
||||
Path string
|
||||
Size int64
|
||||
Code []string
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Size int64 `json:"size"`
|
||||
Code []string `json:"code"`
|
||||
}
|
||||
|
||||
func NewCaplet(name string, path string, size int64) Caplet {
|
||||
return Caplet{
|
||||
Name: name,
|
||||
Path: path,
|
||||
Size: size,
|
||||
Code: make([]string, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (cap *Caplet) Eval(argv []string, lineCb func(line string) error) error {
|
||||
|
|
|
@ -16,22 +16,22 @@ var (
|
|||
cacheLock = sync.Mutex{}
|
||||
)
|
||||
|
||||
func List() []Caplet {
|
||||
caplets := make([]Caplet, 0)
|
||||
func List() []*Caplet {
|
||||
caplets := make([]*Caplet, 0)
|
||||
for _, searchPath := range 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 {
|
||||
if _, 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(),
|
||||
})
|
||||
if err, caplet := Load(base); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "wtf: %v\n", err)
|
||||
} else {
|
||||
caplets = append(caplets, caplet)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -51,6 +51,7 @@ func Load(name string) (error, *Caplet) {
|
|||
return nil, caplet
|
||||
}
|
||||
|
||||
baseName := name
|
||||
names := []string{}
|
||||
if !strings.HasSuffix(name, Suffix) {
|
||||
name += Suffix
|
||||
|
@ -64,16 +65,18 @@ func Load(name string) (error, *Caplet) {
|
|||
names = append(names, name)
|
||||
}
|
||||
|
||||
for _, filename := range names {
|
||||
if fs.Exists(filename) {
|
||||
for _, fileName := range names {
|
||||
if stats, err := os.Stat(fileName); err == nil {
|
||||
cap := &Caplet{
|
||||
Path: filename,
|
||||
Name: baseName,
|
||||
Path: fileName,
|
||||
Code: make([]string, 0),
|
||||
Size: stats.Size(),
|
||||
}
|
||||
cache[name] = cap
|
||||
|
||||
if reader, err := fs.LineReader(filename); err != nil {
|
||||
return fmt.Errorf("error reading caplet %s: %v", filename, err), nil
|
||||
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] == '#' {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue