refact: minor refactoring on the caplets paths parsing and autocomplete logic

This commit is contained in:
evilsocket 2018-08-02 16:42:34 +02:00
commit fa403ad0ec
No known key found for this signature in database
GPG key ID: 1564D7F30393A456

View file

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