Skip to content

Commit

Permalink
fix: output extra information into stderr (jsdelivr#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
radulucut authored Aug 9, 2024
1 parent f93bbc3 commit cf74203
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 67 deletions.
4 changes: 2 additions & 2 deletions cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (r *Root) pingInfinite(opts *globalping.MeasurementCreate) error {
e, ok := err.(*globalping.MeasurementError)
if ok && e.Code == http.StatusTooManyRequests {
r.Cmd.SilenceErrors = true
r.printer.Printf(r.printer.Color("> "+e.Message, view.FGBrightYellow) + "\n")
r.printer.ErrPrintf(r.printer.Color("> "+e.Message, view.FGBrightYellow) + "\n")
}
}
r.viewer.OutputShare()
Expand Down Expand Up @@ -211,7 +211,7 @@ func (r *Root) createMeasurement(opts *globalping.MeasurementCreate) (*view.Hist
r.ctx.RecordToSession = false
err := saveIdToSession(res.ID)
if err != nil {
r.printer.Printf("Warning: %s\n", err)
r.printer.ErrPrintf("Warning: %s\n", err)
}
}
return hm, nil
Expand Down
6 changes: 4 additions & 2 deletions cmd/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,16 @@ func Test_Execute_Ping_Infinite_Output_TooManyRequests_Error(t *testing.T) {
timeMock.EXPECT().Now().Return(defaultCurrentTime).AnyTimes()

w := new(bytes.Buffer)
printer := view.NewPrinter(nil, w, w)
errW := new(bytes.Buffer)
printer := view.NewPrinter(nil, w, errW)
ctx := createDefaultContext("ping")
root := NewRoot(printer, ctx, viewerMock, timeMock, gbMock, nil)
os.Args = []string{"globalping", "ping", "jsdelivr.com", "from", "Berlin", "--infinite", "--share"}
err := root.Cmd.ExecuteContext(context.TODO())
assert.Equal(t, "too many requests", err.Error())

assert.Equal(t, "> too many requests\n", w.String())
assert.Equal(t, "> too many requests\n", errW.String())
assert.Equal(t, "", w.String())

expectedCtx := createDefaultExpectedContext("ping")
expectedCtx.History.Find(measurementID1).Status = globalping.StatusFinished
Expand Down
20 changes: 15 additions & 5 deletions view/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,27 @@ func (v *viewer) outputDefault(id string, data *globalping.Measurement, m *globa
v.printer.Println()
}

// Output slightly different format if state is available
v.printer.Println(v.getProbeInfo(result))
v.printer.ErrPrintln(v.getProbeInfo(result))

if v.isBodyOnlyHttpGet(m) {
v.printer.Println(strings.TrimSpace(result.Result.RawBody))
if v.ctx.Cmd == "http" {
if v.ctx.Full {
firstLineEnd := strings.Index(result.Result.RawOutput, "\n")
if firstLineEnd > 0 {
v.printer.ErrPrintln(result.Result.RawOutput[:firstLineEnd])
}
v.printer.ErrPrintln(result.Result.RawHeaders)
v.printer.Println(strings.TrimSpace(result.Result.RawBody))
} else if m.Options.Request.Method == "GET" {
v.printer.Println(strings.TrimSpace(result.Result.RawBody))
} else {
v.printer.Println(strings.TrimSpace(result.Result.RawOutput))
}
} else {
v.printer.Println(strings.TrimSpace(result.Result.RawOutput))
}
}

if v.ctx.Share {
v.printer.Println(v.getShareMessage(id))
v.printer.ErrPrintln(v.getShareMessage(id))
}
}
43 changes: 27 additions & 16 deletions view/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ func Test_Output_Default_HTTP_Get_Share(t *testing.T) {
},
}
w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
errW := new(bytes.Buffer)
printer := NewPrinter(nil, w, errW)
printer.DisableStyling()
viewer := NewViewer(&Context{
Cmd: "http",
Expand All @@ -140,12 +141,14 @@ func Test_Output_Default_HTTP_Get_Share(t *testing.T) {
viewer.Output(measurementID1, m)

assert.Equal(t, fmt.Sprintf(`> Berlin, DE, EU, Network 1 (AS123)
Body 1
> New York (NY), US, NA, Network 2 (AS567)
Body 2
> View the results online: https://www.jsdelivr.com/globalping?measurement=%s
`, measurementID1), w.String())
`, measurementID1), errW.String())

assert.Equal(t, `Body 1
Body 2
`, w.String())
}

func Test_Output_Default_HTTP_Get_Full(t *testing.T) {
Expand All @@ -163,7 +166,7 @@ func Test_Output_Default_HTTP_Get_Full(t *testing.T) {
Network: "Network 1",
},
Result: globalping.ProbeResult{
RawOutput: "Headers 1\nBody 1",
RawOutput: "HTTP/1.1 301\nHeaders 1\nBody 1",
RawHeaders: "Headers 1",
RawBody: "Body 1",
},
Expand All @@ -178,7 +181,7 @@ func Test_Output_Default_HTTP_Get_Full(t *testing.T) {
Network: "Network 2",
},
Result: globalping.ProbeResult{
RawOutput: "Headers 2\nBody 2",
RawOutput: "HTTP/1.1 301\nHeaders 2\nBody 2",
RawHeaders: "Headers 2",
RawBody: "Body 2",
},
Expand All @@ -197,7 +200,8 @@ func Test_Output_Default_HTTP_Get_Full(t *testing.T) {
},
}
w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
errW := new(bytes.Buffer)
printer := NewPrinter(nil, w, errW)
printer.DisableStyling()
viewer := NewViewer(&Context{
Cmd: "http",
Expand All @@ -208,11 +212,14 @@ func Test_Output_Default_HTTP_Get_Full(t *testing.T) {
viewer.Output(measurementID1, m)

assert.Equal(t, `> Berlin, DE, EU, Network 1 (AS123)
HTTP/1.1 301
Headers 1
Body 1
> New York (NY), US, NA, Network 2 (AS567)
HTTP/1.1 301
Headers 2
`, errW.String())
assert.Equal(t, `Body 1
Body 2
`, w.String())
}
Expand Down Expand Up @@ -265,7 +272,8 @@ func Test_Output_Default_HTTP_Head(t *testing.T) {
},
}
w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
errW := new(bytes.Buffer)
printer := NewPrinter(nil, w, errW)
printer.DisableStyling()
viewer := NewViewer(&Context{
Cmd: "http",
Expand All @@ -275,9 +283,10 @@ func Test_Output_Default_HTTP_Head(t *testing.T) {
viewer.Output(measurementID1, m)

assert.Equal(t, `> Berlin, DE, EU, Network 1 (AS123)
Headers 1
> New York (NY), US, NA, Network 2 (AS567)
`, errW.String())
assert.Equal(t, `Headers 1
Headers 2
`, w.String())
}
Expand Down Expand Up @@ -322,7 +331,8 @@ func Test_Output_Default_Ping(t *testing.T) {

m := &globalping.MeasurementCreate{}
w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
errW := new(bytes.Buffer)
printer := NewPrinter(nil, w, errW)
printer.DisableStyling()
viewer := NewViewer(&Context{
Cmd: "ping",
Expand All @@ -332,9 +342,10 @@ func Test_Output_Default_Ping(t *testing.T) {
viewer.Output(measurementID1, m)

assert.Equal(t, `> Berlin, DE, EU, Network 1 (AS123)
Ping Results 1
> New York (NY), US, NA, Network 2 (AS567)
`, errW.String())
assert.Equal(t, `Ping Results 1
Ping Results 2
`, w.String())
}
6 changes: 3 additions & 3 deletions view/infinite.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (v *viewer) OutputInfinite(m *globalping.Measurement) error {
func (v *viewer) outputStreamingPackets(m *globalping.Measurement) error {
if len(v.ctx.AggregatedStats) == 0 {
v.ctx.AggregatedStats = []*MeasurementStats{NewMeasurementStats()}
v.printer.Print(v.getAPICreditInfo())
v.printer.ErrPrint(v.getAPICreditInfo())
}
probeMeasurement := &m.Results[0]
hm := v.ctx.History.Find(m.ID)
Expand All @@ -60,7 +60,7 @@ func (v *viewer) outputStreamingPackets(m *globalping.Measurement) error {
hm.Stats[0] = parsedOutput.Stats
if !v.ctx.IsHeaderPrinted {
v.ctx.Hostname = parsedOutput.Hostname
v.printer.Println(v.getProbeInfo(probeMeasurement))
v.printer.ErrPrintln(v.getProbeInfo(probeMeasurement))
v.printer.Printf("PING %s (%s) %s bytes of data.\n",
parsedOutput.Hostname,
parsedOutput.Address,
Expand Down Expand Up @@ -101,7 +101,7 @@ func (v *viewer) outputTableView(m *globalping.Measurement) error {

func (v *viewer) outputFailSummary(m *globalping.Measurement) error {
for i := range m.Results {
v.printer.Println(v.getProbeInfo(&m.Results[i]))
v.printer.ErrPrintln(v.getProbeInfo(&m.Results[i]))
v.printer.Println(m.Results[i].Result.RawOutput)
}
return errors.New("all probes failed")
Expand Down
37 changes: 17 additions & 20 deletions view/infinite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func Test_OutputInfinite_SingleProbe_InProgress(t *testing.T) {
ctx := createDefaultContext("ping")
hm := ctx.History.Find(measurementID1)
w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
errW := new(bytes.Buffer)
printer := NewPrinter(nil, w, errW)
printer.DisableStyling()
viewer := NewViewer(ctx, printer, timeMock, nil)

Expand All @@ -34,10 +35,9 @@ func Test_OutputInfinite_SingleProbe_InProgress(t *testing.T) {
err := viewer.OutputInfinite(measurement)
assert.NoError(t, err)

assert.Equal(t,
apiCreditInfo+
`> Berlin, DE, EU, Deutsche Telekom AG (AS3320)
PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
assert.Equal(t, apiCreditInfo+
"> Berlin, DE, EU, Deutsche Telekom AG (AS3320)\n", errW.String())
assert.Equal(t, `PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
`,
w.String(),
)
Expand All @@ -48,10 +48,9 @@ PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
err = viewer.OutputInfinite(measurement)
assert.NoError(t, err)

assert.Equal(t,
apiCreditInfo+
`> Berlin, DE, EU, Deutsche Telekom AG (AS3320)
PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
assert.Equal(t, apiCreditInfo+
"> Berlin, DE, EU, Deutsche Telekom AG (AS3320)\n", errW.String())
assert.Equal(t, `PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
64 bytes from 151.101.1.229 (151.101.1.229): icmp_seq=1 ttl=56 time=12.9 ms
`,
w.String(),
Expand All @@ -64,10 +63,9 @@ PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
err = viewer.OutputInfinite(measurement)
assert.NoError(t, err)

assert.Equal(t,
apiCreditInfo+
`> Berlin, DE, EU, Deutsche Telekom AG (AS3320)
PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
assert.Equal(t, apiCreditInfo+
"> Berlin, DE, EU, Deutsche Telekom AG (AS3320)\n", errW.String())
assert.Equal(t, `PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
64 bytes from 151.101.1.229 (151.101.1.229): icmp_seq=1 ttl=56 time=12.9 ms
64 bytes from 151.101.1.229 (151.101.1.229): icmp_seq=2 ttl=56 time=12.7 ms
`,
Expand All @@ -92,10 +90,9 @@ rtt min/avg/max/mdev = 12.711/12.854/12.952/0.103 ms`
err = viewer.OutputInfinite(measurement)
assert.NoError(t, err)

assert.Equal(t,
apiCreditInfo+
`> Berlin, DE, EU, Deutsche Telekom AG (AS3320)
PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
assert.Equal(t, apiCreditInfo+
"> Berlin, DE, EU, Deutsche Telekom AG (AS3320)\n", errW.String())
assert.Equal(t, `PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
64 bytes from 151.101.1.229 (151.101.1.229): icmp_seq=1 ttl=56 time=12.9 ms
64 bytes from 151.101.1.229 (151.101.1.229): icmp_seq=2 ttl=56 time=12.7 ms
64 bytes from 151.101.1.229 (151.101.1.229): icmp_seq=3 ttl=56 time=13.0 ms
Expand All @@ -116,10 +113,10 @@ PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
err = viewer.OutputInfinite(measurement)
assert.NoError(t, err)

assert.Equal(t, apiCreditInfo+
"> Berlin, DE, EU, Deutsche Telekom AG (AS3320)\n", errW.String())
assert.Equal(t,
apiCreditInfo+
`> Berlin, DE, EU, Deutsche Telekom AG (AS3320)
PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
`PING jsdelivr.map.fastly.net (151.101.1.229) 56(84) bytes of data.
64 bytes from 151.101.1.229 (151.101.1.229): icmp_seq=1 ttl=56 time=12.9 ms
64 bytes from 151.101.1.229 (151.101.1.229): icmp_seq=2 ttl=56 time=12.7 ms
64 bytes from 151.101.1.229 (151.101.1.229): icmp_seq=3 ttl=56 time=13.0 ms
Expand Down
2 changes: 1 addition & 1 deletion view/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func (v *viewer) OutputJson(id string) error {
v.printer.Println(string(output))

if v.ctx.Share {
v.printer.Println(v.getShareMessage(id))
v.printer.ErrPrintln(v.getShareMessage(id))
}
v.printer.Println()

Expand Down
4 changes: 2 additions & 2 deletions view/latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (v *viewer) OutputLatency(id string, data *globalping.Measurement) error {
v.printer.Println()
}

v.printer.Println(v.getProbeInfo(&result))
v.printer.ErrPrintln(v.getProbeInfo(&result))

switch v.ctx.Cmd {
case "ping":
Expand Down Expand Up @@ -50,7 +50,7 @@ func (v *viewer) OutputLatency(id string, data *globalping.Measurement) error {
}

if v.ctx.Share {
v.printer.Println(v.getShareMessage(id))
v.printer.ErrPrintln(v.getShareMessage(id))
}
v.printer.Println()

Expand Down
13 changes: 8 additions & 5 deletions view/latency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ func Test_Output_Latency_Ping(t *testing.T) {
gbMock.EXPECT().GetMeasurement(measurementID1).Times(1).Return(measurement, nil)

w := new(bytes.Buffer)
errW := new(bytes.Buffer)
viewer := NewViewer(
&Context{
Cmd: "ping",
ToLatency: true,
},
NewPrinter(nil, w, w),
NewPrinter(nil, w, errW),
nil,
gbMock,
)
Expand All @@ -66,10 +67,10 @@ func Test_Output_Latency_Ping(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, "\033[1;38;5;43m> City (State), Country, Continent, Network (AS12345) (tag-1)\033[0m\n"+
"\033[1mMin: \033[0m8.00 ms\n"+
"\033[1;38;5;43m> City B (State B), Country B, Continent B, Network B (AS12349)\033[0m\n", errW.String())
assert.Equal(t, "\033[1mMin: \033[0m8.00 ms\n"+
"\033[1mMax: \033[0m20.00 ms\n"+
"\033[1mAvg: \033[0m12.00 ms\n\n"+
"\033[1;38;5;43m> City B (State B), Country B, Continent B, Network B (AS12349)\033[0m\n"+
"\033[1mMin: \033[0m9.00 ms\n"+
"\033[1mMax: \033[0m22.00 ms\n"+
"\033[1mAvg: \033[0m15.00 ms\n\n", w.String())
Expand Down Expand Up @@ -293,7 +294,8 @@ func Test_Output_Latency_Http_StylingDisabled(t *testing.T) {
gbMock.EXPECT().GetMeasurement(measurementID1).Times(1).Return(measurement, nil)

w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
errW := new(bytes.Buffer)
printer := NewPrinter(nil, w, errW)
printer.DisableStyling()
viewer := NewViewer(
&Context{
Expand All @@ -309,7 +311,8 @@ func Test_Output_Latency_Http_StylingDisabled(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, `> City (State), Country, Continent, Network (AS12345)
Total: 44 ms
`, errW.String())
assert.Equal(t, `Total: 44 ms
Download: 11 ms
First byte: 20 ms
DNS: 5 ms
Expand Down
12 changes: 12 additions & 0 deletions view/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ func (p *Printer) Printf(format string, a ...any) {
fmt.Fprintf(p.OutWriter, format, a...)
}

func (p *Printer) ErrPrint(a ...any) {
fmt.Fprint(p.ErrWriter, a...)
}

func (p *Printer) ErrPrintln(a ...any) {
fmt.Fprintln(p.ErrWriter, a...)
}

func (p *Printer) ErrPrintf(format string, a ...any) {
fmt.Fprintf(p.ErrWriter, format, a...)
}

func (p *Printer) FillLeft(s string, w int) string {
if len(s) >= w {
return s
Expand Down
Loading

0 comments on commit cf74203

Please sign in to comment.