mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 04:59:25 -07:00
refact: minor refactoring on the caplets paths parsing and autocomplete logic
This commit is contained in:
parent
db30cfdd6a
commit
fa403ad0ec
1 changed files with 21 additions and 24 deletions
|
@ -27,7 +27,9 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
const HistoryFile = "~/bettercap.history"
|
const (
|
||||||
|
HistoryFile = "~/bettercap.history"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
I = (*Session)(nil)
|
I = (*Session)(nil)
|
||||||
|
@ -38,6 +40,11 @@ var (
|
||||||
|
|
||||||
reCmdSpaceCleaner = regexp.MustCompile(`^([^\s]+)\s+(.+)$`)
|
reCmdSpaceCleaner = regexp.MustCompile(`^([^\s]+)\s+(.+)$`)
|
||||||
reEnvVarCapture = regexp.MustCompile(`{env\.([^}]+)}`)
|
reEnvVarCapture = regexp.MustCompile(`{env\.([^}]+)}`)
|
||||||
|
|
||||||
|
CapPaths = []string{
|
||||||
|
"./caplets/",
|
||||||
|
"/usr/share/bettercap/caplets/",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type UnknownCommandCallback func(cmd string) bool
|
type UnknownCommandCallback func(cmd string) bool
|
||||||
|
@ -213,15 +220,14 @@ func (s *Session) setupReadline() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
searchForCap("./", tree, false)
|
for _, path := range core.SepSplit(core.Trim(os.Getenv("CAPSPATH")), ":") {
|
||||||
searchForCap("./caplets/", tree, true)
|
if path = core.Trim(path); len(path) > 0 {
|
||||||
searchForCap("/usr/share/bettercap/caplets/", tree, true)
|
CapPaths = append(CapPaths, path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
capspath := core.Trim(os.Getenv("CAPSPATH"))
|
for _, path := range CapPaths {
|
||||||
var paths []string
|
buildCapletsTree(path, tree)
|
||||||
paths = append(paths, core.SepSplit(capspath, ":")...)
|
|
||||||
for _, path := range paths {
|
|
||||||
searchForCap(path, tree, false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for root, subElems := range tree {
|
for root, subElems := range tree {
|
||||||
|
@ -251,18 +257,17 @@ func (s *Session) setupReadline() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func searchForCap(path string, tree map[string][]string, recursive bool) {
|
func buildCapletsTree(path string, tree map[string][]string) {
|
||||||
_searchForCap(path, tree, recursive, path)
|
_buildCapletsTree(path, tree, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func _searchForCap(path string, tree map[string][]string, recursive bool, prefix string) {
|
func _buildCapletsTree(path string, tree map[string][]string, prefix string) {
|
||||||
subFiles, _ := ioutil.ReadDir(path)
|
subFiles, _ := ioutil.ReadDir(path)
|
||||||
|
|
||||||
for _, subF := range subFiles {
|
for _, subF := range subFiles {
|
||||||
if strings.HasSuffix(subF.Name(), ".cap") {
|
if strings.HasSuffix(subF.Name(), ".cap") {
|
||||||
tree[strings.TrimPrefix(path, prefix)+strings.Replace(subF.Name(), ".cap", "", -1)] = []string{}
|
tree[strings.TrimPrefix(path, prefix)+strings.Replace(subF.Name(), ".cap", "", -1)] = []string{}
|
||||||
} else if subF.IsDir() && recursive {
|
} else if subF.IsDir() {
|
||||||
_searchForCap(path+subF.Name()+"/", tree, true, prefix)
|
_buildCapletsTree(path+subF.Name()+"/", tree, prefix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -528,14 +533,6 @@ func (s *Session) RunCaplet(filename string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) isCapletCommand(line string) (is bool, filename string, argv []string) {
|
func (s *Session) isCapletCommand(line string) (is bool, filename string, argv []string) {
|
||||||
paths := []string{
|
|
||||||
"./",
|
|
||||||
"./caplets/",
|
|
||||||
"/usr/share/bettercap/caplets/",
|
|
||||||
}
|
|
||||||
|
|
||||||
capspath := core.Trim(os.Getenv("CAPSPATH"))
|
|
||||||
paths = append(paths, core.SepSplit(capspath, ":")...)
|
|
||||||
file := core.Trim(line)
|
file := core.Trim(line)
|
||||||
parts := strings.Split(file, " ")
|
parts := strings.Split(file, " ")
|
||||||
argc := len(parts)
|
argc := len(parts)
|
||||||
|
@ -548,7 +545,7 @@ func (s *Session) isCapletCommand(line string) (is bool, filename string, argv [
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range paths {
|
for _, path := range CapPaths {
|
||||||
filename := filepath.Join(path, file) + ".cap"
|
filename := filepath.Join(path, file) + ".cap"
|
||||||
if core.Exists(filename) {
|
if core.Exists(filename) {
|
||||||
return true, filename, argv
|
return true, filename, argv
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue