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

Commite605686

Browse files
committed
apply pr review suggestions
1 parentf5cb3b7 commite605686

File tree

8 files changed

+78
-136
lines changed

8 files changed

+78
-136
lines changed

‎cli/exp_task.go‎

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
package cli
22

33
import (
4-
"context"
5-
"strings"
6-
7-
"github.com/google/uuid"
8-
"golang.org/x/xerrors"
9-
10-
"github.com/coder/coder/v2/codersdk"
114
"github.com/coder/serpent"
125
)
136

@@ -30,71 +23,3 @@ func (r *RootCmd) tasksCommand() *serpent.Command {
3023
}
3124
returncmd
3225
}
33-
34-
funcsplitTaskIdentifier(identifierstring) (ownerstring,taskNamestring,errerror) {
35-
parts:=strings.Split(identifier,"/")
36-
37-
switchlen(parts) {
38-
case1:
39-
owner=codersdk.Me
40-
taskName=parts[0]
41-
case2:
42-
owner=parts[0]
43-
taskName=parts[1]
44-
default:
45-
return"","",xerrors.Errorf("invalid task identifier: %q",identifier)
46-
}
47-
returnowner,taskName,nil
48-
}
49-
50-
// resolveTask fetches and returns a task by an identifier, which may be either
51-
// a UUID, a bare name (for a task owned by the current user), or a "user/task"
52-
// combination, where user is either a username or UUID.
53-
//
54-
// Since there is no TaskByOwnerAndName endpoint yet, this function uses the
55-
// list endpoint with filtering when a name is provided.
56-
funcresolveTask(ctx context.Context,client*codersdk.Client,identifierstring) (codersdk.Task,error) {
57-
exp:=codersdk.NewExperimentalClient(client)
58-
59-
identifier=strings.TrimSpace(identifier)
60-
61-
// Try parsing as UUID first.
62-
iftaskID,err:=uuid.Parse(identifier);err==nil {
63-
returnexp.TaskByID(ctx,taskID)
64-
}
65-
66-
// Not a UUID, treat as identifier.
67-
owner,taskName,err:=splitTaskIdentifier(identifier)
68-
iferr!=nil {
69-
return codersdk.Task{},err
70-
}
71-
72-
tasks,err:=exp.Tasks(ctx,&codersdk.TasksFilter{
73-
Owner:owner,
74-
})
75-
iferr!=nil {
76-
return codersdk.Task{},xerrors.Errorf("list tasks for owner %q: %w",owner,err)
77-
}
78-
79-
iftaskID,err:=uuid.Parse(taskName);err==nil {
80-
// Find task by ID.
81-
for_,task:=rangetasks {
82-
iftask.ID==taskID {
83-
returntask,nil
84-
}
85-
}
86-
}else {
87-
// Find task by name.
88-
for_,task:=rangetasks {
89-
iftask.Name==taskName {
90-
returntask,nil
91-
}
92-
}
93-
}
94-
95-
// Mimic resource not found from API.
96-
varnotFoundErrerror=&codersdk.Error{
97-
Response: codersdk.Response{Message:"Resource not found or you do not have access to this resource"},
98-
}
99-
return codersdk.Task{},xerrors.Errorf("task %q not found for owner %q: %w",taskName,owner,notFoundErr)
100-
}

‎cli/exp_task_delete.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (r *RootCmd) taskDelete() *serpent.Command {
4848

4949
vartasks []codersdk.Task
5050
for_,identifier:=rangeinv.Args {
51-
task,err:=resolveTask(ctx,client,identifier)
51+
task,err:=exp.TaskByIdentifier(ctx,identifier)
5252
iferr!=nil {
5353
returnxerrors.Errorf("resolve task %q: %w",identifier,err)
5454
}

‎cli/exp_task_logs.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (r *RootCmd) taskLogs() *serpent.Command {
4545
identifier=inv.Args[0]
4646
)
4747

48-
task,err:=resolveTask(ctx,client,identifier)
48+
task,err:=exp.TaskByIdentifier(ctx,identifier)
4949
iferr!=nil {
5050
returnxerrors.Errorf("resolve task %q: %w",identifier,err)
5151
}

‎cli/exp_task_send.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (r *RootCmd) taskSend() *serpent.Command {
6060
taskInput=inv.Args[1]
6161
}
6262

63-
task,err:=resolveTask(ctx,client,identifier)
63+
task,err:=exp.TaskByIdentifier(ctx,identifier)
6464
iferr!=nil {
6565
returnxerrors.Errorf("resolve task: %w",err)
6666
}

‎cli/exp_task_status.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ func (r *RootCmd) taskStatus() *serpent.Command {
8383
}
8484

8585
ctx:=i.Context()
86-
ec:=codersdk.NewExperimentalClient(client)
86+
exp:=codersdk.NewExperimentalClient(client)
8787
identifier:=i.Args[0]
8888

89-
task,err:=resolveTask(ctx,client,identifier)
89+
task,err:=exp.TaskByIdentifier(ctx,identifier)
9090
iferr!=nil {
9191
returnerr
9292
}
@@ -107,7 +107,7 @@ func (r *RootCmd) taskStatus() *serpent.Command {
107107
// TODO: implement streaming updates instead of polling
108108
lastStatusRow:=tsr
109109
forranget.C {
110-
task,err:=ec.TaskByID(ctx,task.ID)
110+
task,err:=exp.TaskByID(ctx,task.ID)
111111
iferr!=nil {
112112
returnerr
113113
}

‎codersdk/aitasks.go‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,72 @@ func (c *ExperimentalClient) TaskByID(ctx context.Context, id uuid.UUID) (Task,
255255
returntask,nil
256256
}
257257

258+
funcsplitTaskIdentifier(identifierstring) (ownerstring,taskNamestring,errerror) {
259+
parts:=strings.Split(identifier,"/")
260+
261+
switchlen(parts) {
262+
case1:
263+
owner=Me
264+
taskName=parts[0]
265+
case2:
266+
owner=parts[0]
267+
taskName=parts[1]
268+
default:
269+
return"","",xerrors.Errorf("invalid task identifier: %q",identifier)
270+
}
271+
returnowner,taskName,nil
272+
}
273+
274+
// TaskByIdentifier fetches and returns a task by an identifier, which may be
275+
// either a UUID, a name (for a task owned by the current user), or a
276+
// "user/task" combination, where user is either a username or UUID.
277+
//
278+
// Since there is no TaskByOwnerAndName endpoint yet, this function uses the
279+
// list endpoint with filtering when a name is provided.
280+
func (c*ExperimentalClient)TaskByIdentifier(ctx context.Context,identifierstring) (Task,error) {
281+
identifier=strings.TrimSpace(identifier)
282+
283+
// Try parsing as UUID first.
284+
iftaskID,err:=uuid.Parse(identifier);err==nil {
285+
returnc.TaskByID(ctx,taskID)
286+
}
287+
288+
// Not a UUID, treat as identifier.
289+
owner,taskName,err:=splitTaskIdentifier(identifier)
290+
iferr!=nil {
291+
returnTask{},err
292+
}
293+
294+
tasks,err:=c.Tasks(ctx,&TasksFilter{
295+
Owner:owner,
296+
})
297+
iferr!=nil {
298+
returnTask{},xerrors.Errorf("list tasks for owner %q: %w",owner,err)
299+
}
300+
301+
iftaskID,err:=uuid.Parse(taskName);err==nil {
302+
// Find task by ID.
303+
for_,task:=rangetasks {
304+
iftask.ID==taskID {
305+
returntask,nil
306+
}
307+
}
308+
}else {
309+
// Find task by name.
310+
for_,task:=rangetasks {
311+
iftask.Name==taskName {
312+
returntask,nil
313+
}
314+
}
315+
}
316+
317+
// Mimic resource not found from API.
318+
varnotFoundErrerror=&Error{
319+
Response:Response{Message:"Resource not found or you do not have access to this resource"},
320+
}
321+
returnTask{},xerrors.Errorf("task %q not found for owner %q: %w",taskName,owner,notFoundErr)
322+
}
323+
258324
// DeleteTask deletes a task by its ID.
259325
//
260326
// Experimental: This method is experimental and may change in the future.

‎cli/exp_task_internal_test.go‎renamed to ‎codersdk/aitasks_internal_test.go‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
packagecli
1+
packagecodersdk
22

33
import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
77
"github.com/stretchr/testify/require"
8-
9-
"github.com/coder/coder/v2/codersdk"
108
)
119

1210
funcTest_splitTaskIdentifier(t*testing.T) {
@@ -22,7 +20,7 @@ func Test_splitTaskIdentifier(t *testing.T) {
2220
{
2321
name:"bare task name",
2422
identifier:"mytask",
25-
expectedOwner:codersdk.Me,
23+
expectedOwner:Me,
2624
expectedTask:"mytask",
2725
expectErr:false,
2826
},

‎codersdk/toolsdk/toolsdk.go‎

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,7 @@ var DeleteTask = Tool[DeleteTaskArgs, codersdk.Response]{
19091909

19101910
expClient:=codersdk.NewExperimentalClient(deps.coderClient)
19111911

1912-
task,err:=resolveTask(ctx,expClient,args.TaskID)
1912+
task,err:=expClient.TaskByIdentifier(ctx,args.TaskID)
19131913
iferr!=nil {
19141914
return codersdk.Response{},xerrors.Errorf("resolve task: %w",err)
19151915
}
@@ -2004,7 +2004,7 @@ var GetTaskStatus = Tool[GetTaskStatusArgs, GetTaskStatusResponse]{
20042004

20052005
expClient:=codersdk.NewExperimentalClient(deps.coderClient)
20062006

2007-
task,err:=resolveTask(ctx,expClient,args.TaskID)
2007+
task,err:=expClient.TaskByIdentifier(ctx,args.TaskID)
20082008
iferr!=nil {
20092009
returnGetTaskStatusResponse{},xerrors.Errorf("resolve task %q: %w",args.TaskID,err)
20102010
}
@@ -2051,7 +2051,7 @@ var SendTaskInput = Tool[SendTaskInputArgs, codersdk.Response]{
20512051

20522052
expClient:=codersdk.NewExperimentalClient(deps.coderClient)
20532053

2054-
task,err:=resolveTask(ctx,expClient,args.TaskID)
2054+
task,err:=expClient.TaskByIdentifier(ctx,args.TaskID)
20552055
iferr!=nil {
20562056
return codersdk.Response{},xerrors.Errorf("resolve task %q: %w",args.TaskID,err)
20572057
}
@@ -2095,7 +2095,7 @@ var GetTaskLogs = Tool[GetTaskLogsArgs, codersdk.TaskLogsResponse]{
20952095

20962096
expClient:=codersdk.NewExperimentalClient(deps.coderClient)
20972097

2098-
task,err:=resolveTask(ctx,expClient,args.TaskID)
2098+
task,err:=expClient.TaskByIdentifier(ctx,args.TaskID)
20992099
iferr!=nil {
21002100
return codersdk.TaskLogsResponse{},err
21012101
}
@@ -2179,50 +2179,3 @@ func taskIDDescription(action string) string {
21792179
funcuserDescription(actionstring)string {
21802180
returnfmt.Sprintf("Username or ID of the user for which to %s. Omit or use the `me` keyword to %s for the authenticated user.",action,action)
21812181
}
2182-
2183-
// resolveTask fetches and returns a task by an identifier, which may be either
2184-
// a UUID, a bare name (for a task owned by the current user), or a "user/task"
2185-
// combination, where user is either a username or UUID.
2186-
//
2187-
// Since there is no TaskByOwnerAndName endpoint yet, this function uses the
2188-
// list endpoint with filtering when a name is provided.
2189-
funcresolveTask(ctx context.Context,exp*codersdk.ExperimentalClient,identifierstring) (codersdk.Task,error) {
2190-
identifier=strings.TrimSpace(identifier)
2191-
2192-
// Try parsing as UUID first.
2193-
iftaskID,err:=uuid.Parse(identifier);err==nil {
2194-
returnexp.TaskByID(ctx,taskID)
2195-
}
2196-
2197-
// Not a UUID, treat as identifier.
2198-
taskName,owner:=splitNameAndOwner(identifier)
2199-
2200-
tasks,err:=exp.Tasks(ctx,&codersdk.TasksFilter{
2201-
Owner:owner,
2202-
})
2203-
iferr!=nil {
2204-
return codersdk.Task{},xerrors.Errorf("list tasks for owner %q: %w",owner,err)
2205-
}
2206-
2207-
iftaskID,err:=uuid.Parse(taskName);err==nil {
2208-
// Find task by ID.
2209-
for_,task:=rangetasks {
2210-
iftask.ID==taskID {
2211-
returntask,nil
2212-
}
2213-
}
2214-
}else {
2215-
// Find task by name.
2216-
for_,task:=rangetasks {
2217-
iftask.Name==taskName {
2218-
returntask,nil
2219-
}
2220-
}
2221-
}
2222-
2223-
// Mimic resource not found from API.
2224-
varnotFoundErrerror=&codersdk.Error{
2225-
Response: codersdk.Response{Message:"Resource not found or you do not have access to this resource"},
2226-
}
2227-
return codersdk.Task{},xerrors.Errorf("task %q not found for owner %q: %w",taskName,owner,notFoundErr)
2228-
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp