- Notifications
You must be signed in to change notification settings - Fork1k
feat(cli): add exp task send#19922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
feat(cli): add exp task send
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit7d0f9a6f087ab7de7f10a360bf1b9ff428ad357a
There are no files selected for viewing
1 change: 1 addition & 0 deletionscli/exp_task.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletionscli/exp_task_send.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cli | ||
import ( | ||
"io" | ||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/serpent" | ||
) | ||
func (r *RootCmd) taskSend() *serpent.Command { | ||
var stdin bool | ||
cmd := &serpent.Command{ | ||
Use: "send <task> [<message> | --stdin]", | ||
DanielleMaywood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
Short: "Send input to a task", | ||
Middleware: serpent.RequireRangeArgs(1, 2), | ||
Options: serpent.OptionSet{ | ||
{ | ||
Name: "stdin", | ||
Flag: "stdin", | ||
Description: "Reads from stdin for the message.", | ||
DanielleMaywood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
Value: serpent.BoolOf(&stdin), | ||
}, | ||
}, | ||
Handler: func(inv *serpent.Invocation) error { | ||
client, err := r.InitClient(inv) | ||
if err != nil { | ||
return err | ||
} | ||
var ( | ||
ctx = inv.Context() | ||
exp = codersdk.NewExperimentalClient(client) | ||
task = inv.Args[0] | ||
taskInput string | ||
taskID uuid.UUID | ||
) | ||
if stdin { | ||
bytes, err := io.ReadAll(inv.Stdin) | ||
if err != nil { | ||
return xerrors.Errorf("reading stdio: %w", err) | ||
} | ||
taskInput = string(bytes) | ||
} else { | ||
if len(inv.Args) != 2 { | ||
return xerrors.Errorf("expected an input for the task") | ||
} | ||
taskInput = inv.Args[1] | ||
} | ||
if id, err := uuid.Parse(task); err == nil { | ||
taskID = id | ||
} else { | ||
ws, err := namedWorkspace(ctx, client, task) | ||
if err != nil { | ||
return xerrors.Errorf("resolve task: %w", err) | ||
} | ||
taskID = ws.ID | ||
} | ||
if err = exp.TaskSend(ctx, codersdk.Me, taskID, codersdk.TaskSendRequest{Input: taskInput}); err != nil { | ||
return xerrors.Errorf("send input to task: %w", err) | ||
} | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} |
173 changes: 173 additions & 0 deletionscli/exp_task_send_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package cli_test | ||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/coder/coder/v2/cli/clitest" | ||
"github.com/coder/coder/v2/coderd/httpapi" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/testutil" | ||
) | ||
func Test_TaskSend(t *testing.T) { | ||
t.Parallel() | ||
var ( | ||
taskName = "task-workspace" | ||
taskID = uuid.MustParse("11111111-1111-1111-1111-111111111111") | ||
) | ||
tests := []struct { | ||
args []string | ||
stdin string | ||
expectError string | ||
handler func(t *testing.T, ctx context.Context) http.HandlerFunc | ||
}{ | ||
{ | ||
args: []string{taskName, "carry on with the task"}, | ||
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case fmt.Sprintf("/api/v2/users/me/workspace/%s", taskName): | ||
httpapi.Write(ctx, w, http.StatusOK, codersdk.Workspace{ | ||
ID: taskID, | ||
}) | ||
case fmt.Sprintf("/api/experimental/tasks/me/%s/send", taskID.String()): | ||
var req codersdk.TaskSendRequest | ||
if !httpapi.Read(ctx, w, r, &req) { | ||
return | ||
} | ||
assert.Equal(t, "carry on with the task", req.Input) | ||
httpapi.Write(ctx, w, http.StatusNoContent, nil) | ||
default: | ||
t.Errorf("unexpected path: %s", r.URL.Path) | ||
} | ||
} | ||
}, | ||
}, | ||
{ | ||
args: []string{taskID.String(), "carry on with the task"}, | ||
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case fmt.Sprintf("/api/experimental/tasks/me/%s/send", taskID.String()): | ||
var req codersdk.TaskSendRequest | ||
if !httpapi.Read(ctx, w, r, &req) { | ||
return | ||
} | ||
assert.Equal(t, "carry on with the task", req.Input) | ||
httpapi.Write(ctx, w, http.StatusNoContent, nil) | ||
default: | ||
t.Errorf("unexpected path: %s", r.URL.Path) | ||
} | ||
} | ||
}, | ||
}, | ||
{ | ||
args: []string{taskName, "--stdin"}, | ||
stdin: "carry on with the task", | ||
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case fmt.Sprintf("/api/v2/users/me/workspace/%s", taskName): | ||
httpapi.Write(ctx, w, http.StatusOK, codersdk.Workspace{ | ||
ID: taskID, | ||
}) | ||
case fmt.Sprintf("/api/experimental/tasks/me/%s/send", taskID.String()): | ||
var req codersdk.TaskSendRequest | ||
if !httpapi.Read(ctx, w, r, &req) { | ||
return | ||
} | ||
assert.Equal(t, "carry on with the task", req.Input) | ||
httpapi.Write(ctx, w, http.StatusNoContent, nil) | ||
default: | ||
t.Errorf("unexpected path: %s", r.URL.Path) | ||
} | ||
} | ||
}, | ||
}, | ||
{ | ||
args: []string{"doesnotexist", "some task input"}, | ||
expectError: httpapi.ResourceNotFoundResponse.Message, | ||
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case "/api/v2/users/me/workspace/doesnotexist": | ||
httpapi.ResourceNotFound(w) | ||
default: | ||
t.Errorf("unexpected path: %s", r.URL.Path) | ||
} | ||
} | ||
}, | ||
}, | ||
{ | ||
args: []string{uuid.Nil.String(), "some task input"}, | ||
expectError: httpapi.ResourceNotFoundResponse.Message, | ||
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case fmt.Sprintf("/api/experimental/tasks/me/%s/send", uuid.Nil.String()): | ||
httpapi.ResourceNotFound(w) | ||
default: | ||
t.Errorf("unexpected path: %s", r.URL.Path) | ||
} | ||
} | ||
}, | ||
}, | ||
{ | ||
args: []string{uuid.Nil.String(), "some task input"}, | ||
expectError: assert.AnError.Error(), | ||
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case fmt.Sprintf("/api/experimental/tasks/me/%s/send", uuid.Nil.String()): | ||
httpapi.InternalServerError(w, assert.AnError) | ||
default: | ||
t.Errorf("unexpected path: %s", r.URL.Path) | ||
} | ||
} | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(strings.Join(tt.args, ","), func(t *testing.T) { | ||
t.Parallel() | ||
var ( | ||
ctx = testutil.Context(t, testutil.WaitShort) | ||
srv = httptest.NewServer(tt.handler(t, ctx)) | ||
client = codersdk.New(testutil.MustURL(t, srv.URL)) | ||
args = []string{"exp", "task", "send"} | ||
err error | ||
) | ||
t.Cleanup(srv.Close) | ||
inv, root := clitest.New(t, append(args, tt.args...)...) | ||
inv.Stdin = strings.NewReader(tt.stdin) | ||
clitest.SetupConfig(t, client, root) | ||
err = inv.WithContext(ctx).Run() | ||
if tt.expectError == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.ErrorContains(t, err, tt.expectError) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.