Skip to content

Commit 252f430

Browse files
feat(cli): add exp task send (coder#19922)
Closes coder/internal#895 --------- Co-authored-by: Mathias Fredriksson <[email protected]>
1 parent 0a79817 commit 252f430

3 files changed

Lines changed: 252 additions & 0 deletions

File tree

‎cli/exp_task.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func (r *RootCmd) tasksCommand() *serpent.Command {
1717
r.taskCreate(),
1818
r.taskStatus(),
1919
r.taskDelete(),
20+
r.taskSend(),
2021
},
2122
}
2223
return cmd

‎cli/exp_task_send.go‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package cli
2+
3+
import (
4+
"io"
5+
6+
"github.com/google/uuid"
7+
"golang.org/x/xerrors"
8+
9+
"github.com/coder/coder/v2/codersdk"
10+
"github.com/coder/serpent"
11+
)
12+
13+
func (r *RootCmd) taskSend() *serpent.Command {
14+
var stdin bool
15+
16+
cmd := &serpent.Command{
17+
Use: "send <task> [<input> | --stdin]",
18+
Short: "Send input to a task",
19+
Middleware: serpent.RequireRangeArgs(1, 2),
20+
Options: serpent.OptionSet{
21+
{
22+
Name: "stdin",
23+
Flag: "stdin",
24+
Description: "Reads the input from stdin.",
25+
Value: serpent.BoolOf(&stdin),
26+
},
27+
},
28+
Handler: func(inv *serpent.Invocation) error {
29+
client, err := r.InitClient(inv)
30+
if err != nil {
31+
return err
32+
}
33+
34+
var (
35+
ctx = inv.Context()
36+
exp = codersdk.NewExperimentalClient(client)
37+
task = inv.Args[0]
38+
39+
taskInput string
40+
taskID uuid.UUID
41+
)
42+
43+
if stdin {
44+
bytes, err := io.ReadAll(inv.Stdin)
45+
if err != nil {
46+
return xerrors.Errorf("reading stdio: %w", err)
47+
}
48+
49+
taskInput = string(bytes)
50+
} else {
51+
if len(inv.Args) != 2 {
52+
return xerrors.Errorf("expected an input for the task")
53+
}
54+
55+
taskInput = inv.Args[1]
56+
}
57+
58+
if id, err := uuid.Parse(task); err == nil {
59+
taskID = id
60+
} else {
61+
ws, err := namedWorkspace(ctx, client, task)
62+
if err != nil {
63+
return xerrors.Errorf("resolve task: %w", err)
64+
}
65+
66+
taskID = ws.ID
67+
}
68+
69+
if err = exp.TaskSend(ctx, codersdk.Me, taskID, codersdk.TaskSendRequest{Input: taskInput}); err != nil {
70+
return xerrors.Errorf("send input to task: %w", err)
71+
}
72+
73+
return nil
74+
},
75+
}
76+
77+
return cmd
78+
}

‎cli/exp_task_send_test.go‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package cli_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/http/httptest"
8+
"strings"
9+
"testing"
10+
11+
"github.com/google/uuid"
12+
"github.com/stretchr/testify/assert"
13+
14+
"github.com/coder/coder/v2/cli/clitest"
15+
"github.com/coder/coder/v2/coderd/httpapi"
16+
"github.com/coder/coder/v2/codersdk"
17+
"github.com/coder/coder/v2/testutil"
18+
)
19+
20+
func Test_TaskSend(t *testing.T) {
21+
t.Parallel()
22+
23+
var (
24+
taskName = "task-workspace"
25+
taskID = uuid.MustParse("11111111-1111-1111-1111-111111111111")
26+
)
27+
28+
tests := []struct {
29+
args []string
30+
stdin string
31+
expectError string
32+
handler func(t *testing.T, ctx context.Context) http.HandlerFunc
33+
}{
34+
{
35+
args: []string{taskName, "carry on with the task"},
36+
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
37+
return func(w http.ResponseWriter, r *http.Request) {
38+
switch r.URL.Path {
39+
case fmt.Sprintf("/api/v2/users/me/workspace/%s", taskName):
40+
httpapi.Write(ctx, w, http.StatusOK, codersdk.Workspace{
41+
ID: taskID,
42+
})
43+
case fmt.Sprintf("/api/experimental/tasks/me/%s/send", taskID.String()):
44+
var req codersdk.TaskSendRequest
45+
if !httpapi.Read(ctx, w, r, &req) {
46+
return
47+
}
48+
49+
assert.Equal(t, "carry on with the task", req.Input)
50+
51+
httpapi.Write(ctx, w, http.StatusNoContent, nil)
52+
default:
53+
t.Errorf("unexpected path: %s", r.URL.Path)
54+
}
55+
}
56+
},
57+
},
58+
{
59+
args: []string{taskID.String(), "carry on with the task"},
60+
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
61+
return func(w http.ResponseWriter, r *http.Request) {
62+
switch r.URL.Path {
63+
case fmt.Sprintf("/api/experimental/tasks/me/%s/send", taskID.String()):
64+
var req codersdk.TaskSendRequest
65+
if !httpapi.Read(ctx, w, r, &req) {
66+
return
67+
}
68+
69+
assert.Equal(t, "carry on with the task", req.Input)
70+
71+
httpapi.Write(ctx, w, http.StatusNoContent, nil)
72+
default:
73+
t.Errorf("unexpected path: %s", r.URL.Path)
74+
}
75+
}
76+
},
77+
},
78+
{
79+
args: []string{taskName, "--stdin"},
80+
stdin: "carry on with the task",
81+
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
82+
return func(w http.ResponseWriter, r *http.Request) {
83+
switch r.URL.Path {
84+
case fmt.Sprintf("/api/v2/users/me/workspace/%s", taskName):
85+
httpapi.Write(ctx, w, http.StatusOK, codersdk.Workspace{
86+
ID: taskID,
87+
})
88+
case fmt.Sprintf("/api/experimental/tasks/me/%s/send", taskID.String()):
89+
var req codersdk.TaskSendRequest
90+
if !httpapi.Read(ctx, w, r, &req) {
91+
return
92+
}
93+
94+
assert.Equal(t, "carry on with the task", req.Input)
95+
96+
httpapi.Write(ctx, w, http.StatusNoContent, nil)
97+
default:
98+
t.Errorf("unexpected path: %s", r.URL.Path)
99+
}
100+
}
101+
},
102+
},
103+
{
104+
args: []string{"doesnotexist", "some task input"},
105+
expectError: httpapi.ResourceNotFoundResponse.Message,
106+
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
107+
return func(w http.ResponseWriter, r *http.Request) {
108+
switch r.URL.Path {
109+
case "/api/v2/users/me/workspace/doesnotexist":
110+
httpapi.ResourceNotFound(w)
111+
default:
112+
t.Errorf("unexpected path: %s", r.URL.Path)
113+
}
114+
}
115+
},
116+
},
117+
{
118+
args: []string{uuid.Nil.String(), "some task input"},
119+
expectError: httpapi.ResourceNotFoundResponse.Message,
120+
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
121+
return func(w http.ResponseWriter, r *http.Request) {
122+
switch r.URL.Path {
123+
case fmt.Sprintf("/api/experimental/tasks/me/%s/send", uuid.Nil.String()):
124+
httpapi.ResourceNotFound(w)
125+
default:
126+
t.Errorf("unexpected path: %s", r.URL.Path)
127+
}
128+
}
129+
},
130+
},
131+
{
132+
args: []string{uuid.Nil.String(), "some task input"},
133+
expectError: assert.AnError.Error(),
134+
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
135+
return func(w http.ResponseWriter, r *http.Request) {
136+
switch r.URL.Path {
137+
case fmt.Sprintf("/api/experimental/tasks/me/%s/send", uuid.Nil.String()):
138+
httpapi.InternalServerError(w, assert.AnError)
139+
default:
140+
t.Errorf("unexpected path: %s", r.URL.Path)
141+
}
142+
}
143+
},
144+
},
145+
}
146+
147+
for _, tt := range tests {
148+
t.Run(strings.Join(tt.args, ","), func(t *testing.T) {
149+
t.Parallel()
150+
151+
var (
152+
ctx = testutil.Context(t, testutil.WaitShort)
153+
srv = httptest.NewServer(tt.handler(t, ctx))
154+
client = codersdk.New(testutil.MustURL(t, srv.URL))
155+
args = []string{"exp", "task", "send"}
156+
err error
157+
)
158+
159+
t.Cleanup(srv.Close)
160+
161+
inv, root := clitest.New(t, append(args, tt.args...)...)
162+
inv.Stdin = strings.NewReader(tt.stdin)
163+
clitest.SetupConfig(t, client, root)
164+
165+
err = inv.WithContext(ctx).Run()
166+
if tt.expectError == "" {
167+
assert.NoError(t, err)
168+
} else {
169+
assert.ErrorContains(t, err, tt.expectError)
170+
}
171+
})
172+
}
173+
}

0 commit comments

Comments
 (0)