mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 13:09:49 -07:00
fix: module parameters are now sorted by name when using the 'help module' command
This commit is contained in:
parent
7a08366516
commit
5bf7814cba
1 changed files with 98 additions and 80 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -15,6 +16,101 @@ import (
|
||||||
"github.com/bettercap/readline"
|
"github.com/bettercap/readline"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (s *Session) generalHelp() {
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
maxLen := 0
|
||||||
|
for _, h := range s.CoreHandlers {
|
||||||
|
len := len(h.Name)
|
||||||
|
if len > maxLen {
|
||||||
|
maxLen = len
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pad := "%" + strconv.Itoa(maxLen) + "s"
|
||||||
|
|
||||||
|
for _, h := range s.CoreHandlers {
|
||||||
|
fmt.Printf(" "+core.Yellow(pad)+" : %s\n", h.Name, h.Description)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(core.Bold("\nModules\n"))
|
||||||
|
|
||||||
|
maxLen = 0
|
||||||
|
for _, m := range s.Modules {
|
||||||
|
len := len(m.Name())
|
||||||
|
if len > maxLen {
|
||||||
|
maxLen = len
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pad = "%" + strconv.Itoa(maxLen) + "s"
|
||||||
|
|
||||||
|
for _, m := range s.Modules {
|
||||||
|
status := ""
|
||||||
|
if m.Running() {
|
||||||
|
status = core.Green("running")
|
||||||
|
} else {
|
||||||
|
status = core.Red("not running")
|
||||||
|
}
|
||||||
|
fmt.Printf(" "+core.Yellow(pad)+" > %s\n", m.Name(), status)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) moduleHelp(filter string) error {
|
||||||
|
err, m := s.Module(filter)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
status := ""
|
||||||
|
if m.Running() {
|
||||||
|
status = core.Green("running")
|
||||||
|
} else {
|
||||||
|
status = core.Red("not running")
|
||||||
|
}
|
||||||
|
fmt.Printf("%s (%s): %s\n\n", core.Yellow(m.Name()), status, core.Dim(m.Description()))
|
||||||
|
|
||||||
|
maxLen := 0
|
||||||
|
handlers := m.Handlers()
|
||||||
|
for _, h := range handlers {
|
||||||
|
len := len(h.Name)
|
||||||
|
if len > maxLen {
|
||||||
|
maxLen = len
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, h := range handlers {
|
||||||
|
fmt.Print(h.Help(maxLen))
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
params := m.Parameters()
|
||||||
|
if len(params) > 0 {
|
||||||
|
v := make([]*ModuleParam, 0)
|
||||||
|
maxLen := 0
|
||||||
|
for _, h := range params {
|
||||||
|
len := len(h.Name)
|
||||||
|
if len > maxLen {
|
||||||
|
maxLen = len
|
||||||
|
}
|
||||||
|
v = append(v, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(v, func(i, j int) bool {
|
||||||
|
return v[i].Name < v[j].Name
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Print(" Parameters\n\n")
|
||||||
|
for _, p := range v {
|
||||||
|
fmt.Print(p.Help(maxLen))
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Session) helpHandler(args []string, sess *Session) error {
|
func (s *Session) helpHandler(args []string, sess *Session) error {
|
||||||
filter := ""
|
filter := ""
|
||||||
if len(args) == 2 {
|
if len(args) == 2 {
|
||||||
|
@ -22,89 +118,11 @@ func (s *Session) helpHandler(args []string, sess *Session) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if filter == "" {
|
if filter == "" {
|
||||||
fmt.Println()
|
s.generalHelp()
|
||||||
|
|
||||||
maxLen := 0
|
|
||||||
for _, h := range s.CoreHandlers {
|
|
||||||
len := len(h.Name)
|
|
||||||
if len > maxLen {
|
|
||||||
maxLen = len
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pad := "%" + strconv.Itoa(maxLen) + "s"
|
|
||||||
|
|
||||||
for _, h := range s.CoreHandlers {
|
|
||||||
fmt.Printf(" "+core.Yellow(pad)+" : %s\n", h.Name, h.Description)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(core.Bold("\nModules\n"))
|
|
||||||
|
|
||||||
maxLen = 0
|
|
||||||
for _, m := range s.Modules {
|
|
||||||
len := len(m.Name())
|
|
||||||
if len > maxLen {
|
|
||||||
maxLen = len
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pad = "%" + strconv.Itoa(maxLen) + "s"
|
|
||||||
|
|
||||||
for _, m := range s.Modules {
|
|
||||||
status := ""
|
|
||||||
if m.Running() {
|
|
||||||
status = core.Green("running")
|
|
||||||
} else {
|
|
||||||
status = core.Red("not running")
|
|
||||||
}
|
|
||||||
fmt.Printf(" "+core.Yellow(pad)+" > %s\n", m.Name(), status)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println()
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
err, m := s.Module(filter)
|
if err := s.moduleHelp(filter); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println()
|
|
||||||
status := ""
|
|
||||||
if m.Running() {
|
|
||||||
status = core.Green("running")
|
|
||||||
} else {
|
|
||||||
status = core.Red("not running")
|
|
||||||
}
|
|
||||||
fmt.Printf("%s (%s): %s\n\n", core.Yellow(m.Name()), status, core.Dim(m.Description()))
|
|
||||||
|
|
||||||
maxLen := 0
|
|
||||||
handlers := m.Handlers()
|
|
||||||
for _, h := range handlers {
|
|
||||||
len := len(h.Name)
|
|
||||||
if len > maxLen {
|
|
||||||
maxLen = len
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, h := range handlers {
|
|
||||||
fmt.Print(h.Help(maxLen))
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
|
|
||||||
params := m.Parameters()
|
|
||||||
if len(params) > 0 {
|
|
||||||
fmt.Print(" Parameters\n\n")
|
|
||||||
maxLen := 0
|
|
||||||
for _, h := range params {
|
|
||||||
len := len(h.Name)
|
|
||||||
if len > maxLen {
|
|
||||||
maxLen = len
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, p := range params {
|
|
||||||
fmt.Print(p.Help(maxLen))
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue