Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit252f430

Browse files
feat(cli): add exp task send (#19922)
Closescoder/internal#895---------Co-authored-by: Mathias Fredriksson <mafredri@gmail.com>
1 parent0a79817 commit252f430

File tree

3 files changed

+252
-0
lines changed

3 files changed

+252
-0
lines changed

‎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
returncmd

‎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+
varstdinbool
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+
iferr!=nil {
31+
returnerr
32+
}
33+
34+
var (
35+
ctx=inv.Context()
36+
exp=codersdk.NewExperimentalClient(client)
37+
task=inv.Args[0]
38+
39+
taskInputstring
40+
taskID uuid.UUID
41+
)
42+
43+
ifstdin {
44+
bytes,err:=io.ReadAll(inv.Stdin)
45+
iferr!=nil {
46+
returnxerrors.Errorf("reading stdio: %w",err)
47+
}
48+
49+
taskInput=string(bytes)
50+
}else {
51+
iflen(inv.Args)!=2 {
52+
returnxerrors.Errorf("expected an input for the task")
53+
}
54+
55+
taskInput=inv.Args[1]
56+
}
57+
58+
ifid,err:=uuid.Parse(task);err==nil {
59+
taskID=id
60+
}else {
61+
ws,err:=namedWorkspace(ctx,client,task)
62+
iferr!=nil {
63+
returnxerrors.Errorf("resolve task: %w",err)
64+
}
65+
66+
taskID=ws.ID
67+
}
68+
69+
iferr=exp.TaskSend(ctx,codersdk.Me,taskID, codersdk.TaskSendRequest{Input:taskInput});err!=nil {
70+
returnxerrors.Errorf("send input to task: %w",err)
71+
}
72+
73+
returnnil
74+
},
75+
}
76+
77+
returncmd
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+
funcTest_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+
stdinstring
31+
expectErrorstring
32+
handlerfunc(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+
returnfunc(w http.ResponseWriter,r*http.Request) {
38+
switchr.URL.Path {
39+
casefmt.Sprintf("/api/v2/users/me/workspace/%s",taskName):
40+
httpapi.Write(ctx,w,http.StatusOK, codersdk.Workspace{
41+
ID:taskID,
42+
})
43+
casefmt.Sprintf("/api/experimental/tasks/me/%s/send",taskID.String()):
44+
varreq 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+
returnfunc(w http.ResponseWriter,r*http.Request) {
62+
switchr.URL.Path {
63+
casefmt.Sprintf("/api/experimental/tasks/me/%s/send",taskID.String()):
64+
varreq 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+
returnfunc(w http.ResponseWriter,r*http.Request) {
83+
switchr.URL.Path {
84+
casefmt.Sprintf("/api/v2/users/me/workspace/%s",taskName):
85+
httpapi.Write(ctx,w,http.StatusOK, codersdk.Workspace{
86+
ID:taskID,
87+
})
88+
casefmt.Sprintf("/api/experimental/tasks/me/%s/send",taskID.String()):
89+
varreq 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+
returnfunc(w http.ResponseWriter,r*http.Request) {
108+
switchr.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+
returnfunc(w http.ResponseWriter,r*http.Request) {
122+
switchr.URL.Path {
123+
casefmt.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+
returnfunc(w http.ResponseWriter,r*http.Request) {
136+
switchr.URL.Path {
137+
casefmt.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:=rangetests {
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+
errerror
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+
iftt.expectError=="" {
167+
assert.NoError(t,err)
168+
}else {
169+
assert.ErrorContains(t,err,tt.expectError)
170+
}
171+
})
172+
}
173+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp