Skip to content

Commit 6034fa9

Browse files
committed
Change PrintOutput to format arrays #37
1 parent cae5b2b commit 6034fa9

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

cmd/internal/runtest.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"io"
22+
"strings"
2223
"sync"
2324
"time"
2425

@@ -135,6 +136,13 @@ func PrintOutput(f *Formatter, output *perfops.RunOutput) {
135136
o := r.Output
136137
if o == "-2" {
137138
o = "The command timed-out. It either took too long to execute or we could not connect to your target at all."
139+
} else if a, ok := o.([]interface{}); ok {
140+
sb := strings.Builder{}
141+
for _, i := range a {
142+
sb.WriteString(fmt.Sprintf("%s\n", i))
143+
}
144+
s := sb.String()
145+
o = s[:len(s)-1]
138146
}
139147
f.Printf("Node%d, AS%d, %s, %s\n%s\n", n.ID, n.AsNumber, n.City, n.Country.Name, o)
140148
} else if r.Message != "NO DATA" {
@@ -197,14 +205,15 @@ func (f *Formatter) Flush(limit bool) error {
197205
f.mu.Lock()
198206
defer f.mu.Unlock()
199207

200-
if len(f.buf.Bytes()) == 0 {
208+
buf := f.buf.Bytes()
209+
f.buf.Reset()
210+
if len(buf) == 0 {
201211
return nil
202212
}
203-
defer f.buf.Reset()
204213

205214
termStartOfRow(f.w)
206215
if limit {
207-
out := string(f.buf.Bytes())
216+
out := string(buf)
208217
width := 0
209218
lines := 0
210219
for _, r := range out {
@@ -222,7 +231,7 @@ func (f *Formatter) Flush(limit bool) error {
222231
return err
223232
}
224233
}
225-
} else if _, err := f.w.Write(f.buf.Bytes()); err != nil {
234+
} else if _, err := f.w.Write(buf); err != nil {
226235
return err
227236
}
228237
return f.w.Flush()

cmd/internal/runtest_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ func TestPrintOutput(t *testing.T) {
9292
},
9393
"\x1b[200DNode27, AS23456, Hong Kong, Hong Kong\nThe command timed-out. It either took too long to execute or we could not connect to your target at all.\n",
9494
},
95+
"array output": {
96+
func() *perfops.RunOutput {
97+
var o *perfops.RunOutput
98+
json.Unmarshal([]byte(`{"id":"6e0c06f7445bb8c63949f84fcdbdae55","items":[{"id":"2bff5d6b3a4df8a268afca6c60977032","result":{"dnsServer":"","node":{"as_number":197328,"id":103,"latitude":41.030549854339,"longitude":28.987083435058,"country":{"id":93,"name":"Turkey","continent":{"id":2,"name":"Asia","iso":"AS"},"iso":"TR","iso_numeric":"792","is_eu":false},"city":"Istanbul","sub_region":"Western Asia"},"finished":true,"output":["header"," 1 row", " 10 row"],"time":1539517079.937741}}],"requested":"ns2.no-ip.com","finished":"true","elapsedTime":2.21,"creditsWithdrawn":1}`), &o)
99+
return o
100+
},
101+
"\x1b[200DNode103, AS197328, Istanbul, Turkey\nheader\n 1 row\n 10 row\n",
102+
},
95103
}
96104

97105
var b bytes.Buffer

0 commit comments

Comments
 (0)