mirror of
https://github.com/rclone/rclone.git
synced 2025-12-10 05:13:45 +00:00
core/command: Add streaming output for long running commands.
This commit is contained in:
committed by
Nick Craig-Wood
parent
1cae4152f9
commit
a9713cd0ed
@@ -4,6 +4,7 @@ package rc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -339,10 +340,12 @@ func rcSetBlockProfileRate(ctx context.Context, in Params) (out Params, err erro
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Add(Call{
|
Add(Call{
|
||||||
Path: "core/command",
|
Path: "core/command",
|
||||||
AuthRequired: true,
|
AuthRequired: true,
|
||||||
Fn: rcRunCommand,
|
Fn: rcRunCommand,
|
||||||
Title: "Run a rclone terminal command over rc.",
|
NeedsRequest: true,
|
||||||
|
NeedsResponse: true,
|
||||||
|
Title: "Run a rclone terminal command over rc.",
|
||||||
Help: `This takes the following parameters
|
Help: `This takes the following parameters
|
||||||
|
|
||||||
- command - a string with the command name
|
- command - a string with the command name
|
||||||
@@ -353,6 +356,7 @@ Returns
|
|||||||
|
|
||||||
- result - result from the backend command
|
- result - result from the backend command
|
||||||
- error - set if rclone exits with an error code
|
- error - set if rclone exits with an error code
|
||||||
|
- returnType - one of ("COMBINED_OUTPUT", "STREAM", "STREAM_ONLY_STDOUT". "STREAM_ONLY_STDERR")
|
||||||
|
|
||||||
For example
|
For example
|
||||||
|
|
||||||
@@ -398,6 +402,17 @@ func rcRunCommand(ctx context.Context, in Params) (out Params, err error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
returnType, err := in.GetString("returnType")
|
||||||
|
if err != nil {
|
||||||
|
returnType = "COMBINED_OUTPUT"
|
||||||
|
}
|
||||||
|
|
||||||
|
var httpResponse *http.ResponseWriter
|
||||||
|
httpResponse, err = in.GetHTTPResponseWriter()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Errorf("response object is required\n" + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
var allArgs = []string{}
|
var allArgs = []string{}
|
||||||
if command != "" {
|
if command != "" {
|
||||||
// Add the command eg: ls to the args
|
// Add the command eg: ls to the args
|
||||||
@@ -427,19 +442,30 @@ func rcRunCommand(ctx context.Context, in Params) (out Params, err error) {
|
|||||||
|
|
||||||
cmd := exec.CommandContext(ctx, ex, allArgs...)
|
cmd := exec.CommandContext(ctx, ex, allArgs...)
|
||||||
|
|
||||||
// Run the command and get the output for error and stdout combined.
|
if returnType == "COMBINED_OUTPUT" {
|
||||||
output, err := cmd.CombinedOutput()
|
// Run the command and get the output for error and stdout combined.
|
||||||
|
|
||||||
if err != nil {
|
out, err := cmd.CombinedOutput()
|
||||||
fs.Errorf(nil, "Command error %v", err)
|
|
||||||
|
if err != nil {
|
||||||
|
return Params{
|
||||||
|
"result": string(out),
|
||||||
|
"error": true,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
return Params{
|
return Params{
|
||||||
"result": string(output),
|
"result": string(out),
|
||||||
"error": true,
|
"error": false,
|
||||||
}, nil
|
}, nil
|
||||||
|
} else if returnType == "STREAM_ONLY_STDOUT" {
|
||||||
|
cmd.Stdout = *httpResponse
|
||||||
|
} else if returnType == "STREAM_ONLY_STDERR" {
|
||||||
|
cmd.Stderr = *httpResponse
|
||||||
|
} else if returnType == "STREAM" {
|
||||||
|
cmd.Stdout = *httpResponse
|
||||||
|
cmd.Stderr = *httpResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
return Params{
|
err = cmd.Run()
|
||||||
"result": string(output),
|
return nil, err
|
||||||
"error": false,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package rc
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -135,10 +137,13 @@ func TestCoreQuit(t *testing.T) {
|
|||||||
func TestCoreCommand(t *testing.T) {
|
func TestCoreCommand(t *testing.T) {
|
||||||
call := Calls.Get("core/command")
|
call := Calls.Get("core/command")
|
||||||
|
|
||||||
|
var httpResponse http.ResponseWriter = httptest.NewRecorder()
|
||||||
|
|
||||||
in := Params{
|
in := Params{
|
||||||
"command": "version",
|
"command": "version",
|
||||||
"opt": map[string]string{},
|
"opt": map[string]string{},
|
||||||
"arg": []string{},
|
"arg": []string{},
|
||||||
|
"_response": &httpResponse,
|
||||||
}
|
}
|
||||||
got, err := call.Fn(context.Background(), in)
|
got, err := call.Fn(context.Background(), in)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user