From 44cf524cac7a2821b210845ce51fd43d1bb95167 Mon Sep 17 00:00:00 2001 From: Selyss <99344963+Selyss@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:12:58 -0500 Subject: [PATCH] more refactoring --- cmd/main.go | 26 ++++++--------- pkg/assembuddy/asm.go | 29 ++++++++-------- pkg/assembuddy/spinner.go | 69 --------------------------------------- 3 files changed, 23 insertions(+), 101 deletions(-) delete mode 100644 pkg/assembuddy/spinner.go diff --git a/cmd/main.go b/cmd/main.go index 2ac49fa..dcdf7a9 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -4,19 +4,13 @@ import ( "fmt" "os" + "github.com/Selyss/AssemBuddy/pkg/assembuddy" "github.com/akamensky/argparse" // tea "github.com/charmbracelet/bubbletea" ) -type CLIOptions struct { - Query string - Architecture string - ListQueryMatches bool - ListArchQueries bool -} - -func parseArgs() *CLIOptions { - options := &CLIOptions{} +func parseArgs() *assembuddy.CLIOptions { + opts := &assembuddy.CLIOptions{} parser := argparse.NewParser("AssemBuddy", "Tool for querying assembly keywords") query := parser.String("q", "query", &argparse.Options{Help: "Search query"}) @@ -30,15 +24,15 @@ func parseArgs() *CLIOptions { fmt.Print(parser.Usage(err)) } - options.Query = *query - options.Architecture = *arch - options.ListQueryMatches = *listQueryMatches - options.ListArchQueries = *listArchQueries + opts.Syscall = *query + opts.Arch = *arch + opts.ListQueryMatches = *listQueryMatches + opts.ListArchQueries = *listArchQueries - return options + return opts } func main() { - options := parseArgs() - fmt.Println(options) + opts := parseArgs() + assembuddy.QueryASM(opts) } diff --git a/pkg/assembuddy/asm.go b/pkg/assembuddy/asm.go index fea1719..4ef4c99 100644 --- a/pkg/assembuddy/asm.go +++ b/pkg/assembuddy/asm.go @@ -6,7 +6,6 @@ import ( "io" "log" "net/http" - "os" ) type Syscall struct { @@ -23,27 +22,25 @@ type Syscall struct { Arg5 string `json:"arg5"` } -func QueryASM() { - archs := []string{"x64", "x86", "arm", "arm64"} +type CLIOptions struct { + Syscall string + Arch string + ListQueryMatches bool + ListArchQueries bool +} - arch, err := SelectFromList(archs) - if err != nil { - log.Fatalf("Error selecting a language: %s", err) - } +func QueryASM(opts *CLIOptions) { + arch := opts.Arch + syscall := opts.Syscall - syscalls, err := getSyscalls(arch) - if err != nil { - log.Fatalf("Error fetching syscalls for %s: %s", arch, err) - } + var selectedName string - selectedName, err := SelectFromList(syscalls) - if err != nil { - log.Fatalf("Error selecting a syscall: %s", err) + if syscall != "" { + selectedName = syscall } if selectedName == "" { - fmt.Println("No syscall selected.") - os.Exit(0) + log.Fatalf("No syscall selected.") } selectedSyscall, err := getSyscallDetails(arch, selectedName) diff --git a/pkg/assembuddy/spinner.go b/pkg/assembuddy/spinner.go deleted file mode 100644 index 0e211c6..0000000 --- a/pkg/assembuddy/spinner.go +++ /dev/null @@ -1,69 +0,0 @@ -package assembuddy - -import ( - "fmt" - "os" - - "github.com/charmbracelet/bubbles/spinner" - tea "github.com/charmbracelet/bubbletea" - "github.com/charmbracelet/lipgloss" -) - -type errMsg error - -type model struct { - spinner spinner.Model - quitting bool - err error -} - -func initialModel() model { - s := spinner.New() - s.Spinner = spinner.Dot - s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("99")) - return model{spinner: s} -} - -func (m model) Init() tea.Cmd { - return m.spinner.Tick -} - -func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - switch msg := msg.(type) { - case tea.KeyMsg: - switch msg.String() { - case "q", "esc", "ctrl+c": - m.quitting = true - return m, tea.Quit - default: - return m, nil - } - - case errMsg: - m.err = msg - return m, nil - - default: - var cmd tea.Cmd - m.spinner, cmd = m.spinner.Update(msg) - return m, cmd - } -} - -func (m model) View() string { - if m.err != nil { - return m.err.Error() - } - str := fmt.Sprintf("\n %s Loading information...press q to quit\n", m.spinner.View()) - if m.quitting { - return "" - } - return str -} - -func DisplayLoadingSpinner(p *tea.Program) { - if _, err := p.Run(); err != nil { - fmt.Println(err) - os.Exit(1) - } -}