Skip to content

Commit

Permalink
refactor pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Selyss committed Nov 22, 2023
1 parent a9f291f commit 7c9474c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
15 changes: 13 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
33 changes: 22 additions & 11 deletions pkg/assembuddy/requestTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"os"
)

type Syscall struct {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -60,22 +54,39 @@ 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
if arch == "x64" || arch == "x86" || arch == "arm" || arch == "arm64" {
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)
}
6 changes: 5 additions & 1 deletion pkg/assembuddy/requestTable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 7c9474c

Please sign in to comment.