Skip to content

Commit 772f940

Browse files
authored
Merge pull request #12 from Bandwidth/recording-pause-resume
feat(recording): add pause and resume subcommands
2 parents 8d7de36 + b8a5b2c commit 772f940

4 files changed

Lines changed: 143 additions & 1 deletion

File tree

cmd/recording/golden_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,49 @@ func TestRecordingListPlainOutput(t *testing.T) {
4444
t.Fatalf("golden mismatch:\n got: %q\nwant: %q", out, want)
4545
}
4646
}
47+
48+
func TestRecordingPauseOutput(t *testing.T) {
49+
// No t.Parallel(): these tests mutate the global cmdutil.VoiceClient.
50+
orig := cmdutil.VoiceClient
51+
t.Cleanup(func() { cmdutil.VoiceClient = orig })
52+
cmdutil.VoiceClient = func(string) (api.Requester, string, error) {
53+
return &testutil.FakeClient{}, "acct-123", nil
54+
}
55+
56+
root := testutil.NewTestRoot(pauseCmd)
57+
root.SetArgs([]string{"pause", "c-abc123"})
58+
59+
out := testutil.CaptureStdout(t, func() {
60+
if err := root.Execute(); err != nil {
61+
t.Fatalf("execute: %v", err)
62+
}
63+
})
64+
65+
want := "Recording paused on call c-abc123.\n"
66+
if out != want {
67+
t.Fatalf("golden mismatch:\n got: %q\nwant: %q", out, want)
68+
}
69+
}
70+
71+
func TestRecordingResumeOutput(t *testing.T) {
72+
// No t.Parallel(): these tests mutate the global cmdutil.VoiceClient.
73+
orig := cmdutil.VoiceClient
74+
t.Cleanup(func() { cmdutil.VoiceClient = orig })
75+
cmdutil.VoiceClient = func(string) (api.Requester, string, error) {
76+
return &testutil.FakeClient{}, "acct-123", nil
77+
}
78+
79+
root := testutil.NewTestRoot(resumeCmd)
80+
root.SetArgs([]string{"resume", "c-abc123"})
81+
82+
out := testutil.CaptureStdout(t, func() {
83+
if err := root.Execute(); err != nil {
84+
t.Fatalf("execute: %v", err)
85+
}
86+
})
87+
88+
want := "Recording resumed on call c-abc123.\n"
89+
if out != want {
90+
t.Fatalf("golden mismatch:\n got: %q\nwant: %q", out, want)
91+
}
92+
}

cmd/recording/pause.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package recording
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/Bandwidth/cli/internal/cmdutil"
10+
)
11+
12+
func init() {
13+
Cmd.AddCommand(pauseCmd)
14+
}
15+
16+
var pauseCmd = &cobra.Command{
17+
Use: "pause <callId>",
18+
Short: "Pause the active recording on a live call",
19+
Args: cobra.ExactArgs(1),
20+
RunE: runPause,
21+
}
22+
23+
func runPause(cmd *cobra.Command, args []string) error {
24+
if err := cmdutil.ValidateID(args[0]); err != nil {
25+
return err
26+
}
27+
client, acctID, err := cmdutil.VoiceClient(cmdutil.AccountIDFlag(cmd))
28+
if err != nil {
29+
return err
30+
}
31+
32+
reqBody := map[string]string{
33+
"state": "paused",
34+
}
35+
36+
if err := client.Put(fmt.Sprintf("/accounts/%s/calls/%s/recording", acctID, url.PathEscape(args[0])), reqBody, nil); err != nil {
37+
return fmt.Errorf("pausing recording: %w", err)
38+
}
39+
40+
fmt.Printf("Recording paused on call %s.\n", args[0])
41+
return nil
42+
}

cmd/recording/recording_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestCmdStructure(t *testing.T) {
1313
for _, c := range Cmd.Commands() {
1414
subs[c.Use] = true
1515
}
16-
for _, name := range []string{"get <callId> <recordingId>", "list <callId>", "delete <callId> <recordingId>", "download <callId> <recordingId>"} {
16+
for _, name := range []string{"get <callId> <recordingId>", "list <callId>", "delete <callId> <recordingId>", "download <callId> <recordingId>", "pause <callId>", "resume <callId>"} {
1717
if !subs[name] {
1818
t.Errorf("missing subcommand %q", name)
1919
}
@@ -38,3 +38,15 @@ func TestDownloadRequiredFlags(t *testing.T) {
3838
t.Error("missing flag \"output\"")
3939
}
4040
}
41+
42+
func TestPauseArgs(t *testing.T) {
43+
if pauseCmd.Args == nil {
44+
t.Fatal("pause command should have arg validation")
45+
}
46+
}
47+
48+
func TestResumeArgs(t *testing.T) {
49+
if resumeCmd.Args == nil {
50+
t.Fatal("resume command should have arg validation")
51+
}
52+
}

cmd/recording/resume.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package recording
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/Bandwidth/cli/internal/cmdutil"
10+
)
11+
12+
func init() {
13+
Cmd.AddCommand(resumeCmd)
14+
}
15+
16+
var resumeCmd = &cobra.Command{
17+
Use: "resume <callId>",
18+
Short: "Resume a paused recording on a live call",
19+
Args: cobra.ExactArgs(1),
20+
RunE: runResume,
21+
}
22+
23+
func runResume(cmd *cobra.Command, args []string) error {
24+
if err := cmdutil.ValidateID(args[0]); err != nil {
25+
return err
26+
}
27+
client, acctID, err := cmdutil.VoiceClient(cmdutil.AccountIDFlag(cmd))
28+
if err != nil {
29+
return err
30+
}
31+
32+
reqBody := map[string]string{
33+
"state": "recording",
34+
}
35+
36+
if err := client.Put(fmt.Sprintf("/accounts/%s/calls/%s/recording", acctID, url.PathEscape(args[0])), reqBody, nil); err != nil {
37+
return fmt.Errorf("resuming recording: %w", err)
38+
}
39+
40+
fmt.Printf("Recording resumed on call %s.\n", args[0])
41+
return nil
42+
}

0 commit comments

Comments
 (0)