From 7c9474c9f82ecc18337e3620d3337d450c799700 Mon Sep 17 00:00:00 2001 From: Selyss <99344963+Selyss@users.noreply.github.com> Date: Tue, 21 Nov 2023 21:33:15 -0500 Subject: [PATCH] refactor pretty printing --- cmd/main.go | 15 +++++++++++-- pkg/assembuddy/requestTable.go | 33 +++++++++++++++++++---------- pkg/assembuddy/requestTable_test.go | 6 +++++- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index bd8c72a..76d4baa 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -51,9 +51,20 @@ func listArch() { } func syscallData(opts *assembuddy.CLIOptions) { - table, err := assembuddy.GetSyscallData(opts) + url, err := assembuddy.GetSyscallData(opts) if err != nil { log.Fatalf("Error: %v", err) } - assembuddy.RenderTable(opts, table) + + if opts.PrettyPrint { + assembuddy.PrettyPrint(url) + } else { + + table, err := assembuddy.FetchData(url) + if err != nil { + log.Fatalf("Error: %v", err) + } + + assembuddy.RenderTable(opts, table) + } } diff --git a/pkg/assembuddy/requestTable.go b/pkg/assembuddy/requestTable.go index 5c28a97..ba2487b 100644 --- a/pkg/assembuddy/requestTable.go +++ b/pkg/assembuddy/requestTable.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "net/http" - "os" ) type Syscall struct { @@ -34,7 +33,7 @@ const ( conventionEndpoint = "https://api.syscall.sh/v1/conventions" ) -func fetchData(endpointURL string, prettyPrint bool) ([]Syscall, error) { +func FetchData(endpointURL string) ([]Syscall, error) { response, err := http.Get(endpointURL) if err != nil { return nil, fmt.Errorf("failed to fetch data: %w", err) @@ -45,11 +44,6 @@ func fetchData(endpointURL string, prettyPrint bool) ([]Syscall, error) { if err != nil { return nil, fmt.Errorf("failed to read response body: %w", err) } - if prettyPrint { - - fmt.Println(string(body)) - os.Exit(0) - } var systemCalls []Syscall err = json.Unmarshal(body, &systemCalls) @@ -60,7 +54,24 @@ func fetchData(endpointURL string, prettyPrint bool) ([]Syscall, error) { return systemCalls, nil } -func GetSyscallData(opts *CLIOptions) ([]Syscall, error) { +func PrettyPrint(endpointURL string) error { + response, err := http.Get(endpointURL) + if err != nil { + return fmt.Errorf("failed to fetch data: %w", err) + } + defer response.Body.Close() + + body, err := io.ReadAll(response.Body) + if err != nil { + return fmt.Errorf("failed to read response body: %w", err) + } + + fmt.Println(string(body)) + + return nil +} + +func GetSyscallData(opts *CLIOptions) (string, error) { arch := opts.Arch url := syscallEndpoint // if arch is x64, x86, arm, or arm64, concat to endpointURL @@ -68,14 +79,14 @@ func GetSyscallData(opts *CLIOptions) ([]Syscall, error) { url += "/" + arch // if arch is not empty, return error } else if arch != "" { - return nil, errors.New("invalid architecture") + return "", errors.New("invalid architecture") } if opts.Syscall != "" { url += "/" + opts.Syscall } - return fetchData(url, opts.PrettyPrint) + return url, nil } func ArchInfo() ([]Syscall, error) { - return fetchData(conventionEndpoint, true) + return FetchData(conventionEndpoint) } diff --git a/pkg/assembuddy/requestTable_test.go b/pkg/assembuddy/requestTable_test.go index 5e42c48..4c33581 100644 --- a/pkg/assembuddy/requestTable_test.go +++ b/pkg/assembuddy/requestTable_test.go @@ -29,7 +29,11 @@ func TestGetSyscallData(t *testing.T) { want = append(want, syscall) - got, err := GetSyscallData(&opts) + url, err := GetSyscallData(&opts) + if err != nil { + t.Errorf("Expected url to return correctly %v", err) + } + got, err := FetchData(url) if !(reflect.DeepEqual(want, got)) || err != nil { t.Errorf("Expected opts to parse correctly %v", err) }