Skip to content

Commit 0800a68

Browse files
committed
fix(mediactl): Print the JSON results to the command's output
1 parent 39e03fd commit 0800a68

2 files changed

Lines changed: 13 additions & 23 deletions

File tree

‎cmd/mediactl/main.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ import (
3131
const env = "MEDIACTL"
3232

3333
// app is one run of mediactl: its settings, from the flags, the environment and the config file in
34-
// this order, its logger, and the resolver of its k8s:// addresses.
34+
// this order, its logger, its output, and the resolver of its k8s:// addresses.
3535
type app struct {
3636
v *viper.Viper
3737
lf logger.LoggerFactory
3838
log logging.LeveledLogger
39+
out io.Writer
3940
k8s k8s.Resolver
4041
}
4142

@@ -82,6 +83,7 @@ func newRootCmd() *cobra.Command {
8283
}
8384
a.log = a.lf.NewLogger("mediactl")
8485
a.k8s.Log = a.lf.NewLogger("k8s")
86+
a.out = cmd.OutOrStdout()
8587
return nil
8688
},
8789
}
@@ -186,7 +188,9 @@ func (a *app) send(ctx context.Context, d mediactl.Dialer, network, address stri
186188
if err != nil {
187189
return err
188190
}
189-
fmt.Println(string(b))
191+
if _, err := fmt.Fprintln(a.out, string(b)); err != nil {
192+
return err
193+
}
190194
}
191195

192196
if err := errors.Join(errs...); err != nil {

‎cmd/mediactl/main_test.go‎

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
6-
"io"
77
"os"
88
"path/filepath"
99
"sync"
@@ -104,15 +104,16 @@ func TestParallelJSON(t *testing.T) {
104104
// the echo may not be up yet: wait for one stream through it first
105105
eventually(t, "udp", "send", "127.0.0.1:27011")
106106

107-
out := captureStdout(t, func() {
108-
require.NoError(t, run(context.Background(), "udp", "send", "127.0.0.1:27011", "-P", "3", "-o", "json",
109-
"-k", "100", "-t", "300ms"))
110-
})
107+
cmd, out := newRootCmd(), &bytes.Buffer{}
108+
cmd.SetOut(out)
109+
cmd.SetArgs([]string{"udp", "send", "127.0.0.1:27011", "-P", "3", "-o", "json", "-k", "100", "-t",
110+
"300ms", "--log", "all:WARN"})
111+
require.NoError(t, cmd.ExecuteContext(context.Background()))
111112
var res struct {
112113
Streams []map[string]any `json:"streams"`
113114
Total map[string]any `json:"total"`
114115
}
115-
require.NoError(t, json.Unmarshal([]byte(out), &res), out)
116+
require.NoError(t, json.Unmarshal(out.Bytes(), &res), out.String())
116117
assert.Len(t, res.Streams, 3)
117118
assert.Positive(t, res.Total["received"])
118119
assert.NotEmpty(t, res.Total["rtt_avg"])
@@ -141,18 +142,3 @@ func TestK8sAddress(t *testing.T) {
141142
err = run(context.Background(), "turn", "send", "turn:127.0.0.1:3478", "k8s://ns/svc", "-t", "100ms")
142143
assert.ErrorContains(t, err, "invalid Service address")
143144
}
144-
145-
// captureStdout returns what f prints on the standard output.
146-
func captureStdout(t *testing.T, f func()) string {
147-
t.Helper()
148-
r, w, err := os.Pipe()
149-
require.NoError(t, err)
150-
stdout := os.Stdout
151-
os.Stdout = w
152-
f()
153-
os.Stdout = stdout
154-
require.NoError(t, w.Close())
155-
b, err := io.ReadAll(r)
156-
require.NoError(t, err)
157-
return string(b)
158-
}

0 commit comments

Comments
 (0)