mirror of
https://github.com/rclone/rclone.git
synced 2025-02-02 03:29:51 +01:00
core/command: Allow rc to execute rclone terminal commands.
Allow command parameter to be skipped.
This commit is contained in:
parent
3d5a63607e
commit
5549fd25fc
@ -5,6 +5,7 @@ package rc
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -335,3 +336,110 @@ func rcSetBlockProfileRate(ctx context.Context, in Params) (out Params, err erro
|
|||||||
runtime.SetBlockProfileRate(int(rate))
|
runtime.SetBlockProfileRate(int(rate))
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Add(Call{
|
||||||
|
Path: "core/command",
|
||||||
|
AuthRequired: true,
|
||||||
|
Fn: rcRunCommand,
|
||||||
|
Title: "Run a rclone terminal command over rc.",
|
||||||
|
Help: `This takes the following parameters
|
||||||
|
|
||||||
|
- command - a string with the command name
|
||||||
|
- arg - a list of arguments for the backend command
|
||||||
|
- opt - a map of string to string of options
|
||||||
|
|
||||||
|
Returns
|
||||||
|
|
||||||
|
- result - result from the backend command
|
||||||
|
- error - set if rclone exits with an error code
|
||||||
|
|
||||||
|
For example
|
||||||
|
|
||||||
|
rclone rc core/command command=ls -a mydrive:/ -o max-depth=1
|
||||||
|
rclone rc core/command -a ls -a mydrive:/ -o max-depth=1
|
||||||
|
|
||||||
|
Returns
|
||||||
|
|
||||||
|
` + "```" + `
|
||||||
|
{
|
||||||
|
"error": false,
|
||||||
|
"result": "<Raw command line output>"
|
||||||
|
}
|
||||||
|
|
||||||
|
OR
|
||||||
|
{
|
||||||
|
"error": true,
|
||||||
|
"result": "<Raw command line output>"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// rcRunCommand runs an rclone command with the given args and flags
|
||||||
|
func rcRunCommand(ctx context.Context, in Params) (out Params, err error) {
|
||||||
|
|
||||||
|
command, err := in.GetString("command")
|
||||||
|
if err != nil {
|
||||||
|
command = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var opt = map[string]string{}
|
||||||
|
err = in.GetStructMissingOK("opt", &opt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var arg = []string{}
|
||||||
|
err = in.GetStructMissingOK("arg", &arg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var allArgs = []string{}
|
||||||
|
if command != "" {
|
||||||
|
// Add the command eg: ls to the args
|
||||||
|
allArgs = append(allArgs, command)
|
||||||
|
}
|
||||||
|
// Add all from arg
|
||||||
|
for _, cur := range arg {
|
||||||
|
allArgs = append(allArgs, cur)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add flags to args for eg --max-depth 1 comes in as { max-depth 1 }.
|
||||||
|
// Convert it to [ max-depth, 1 ] and append to args list
|
||||||
|
for key, value := range opt {
|
||||||
|
if len(key) == 1 {
|
||||||
|
allArgs = append(allArgs, "-"+key)
|
||||||
|
} else {
|
||||||
|
allArgs = append(allArgs, "--"+key)
|
||||||
|
}
|
||||||
|
allArgs = append(allArgs, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the path for the current executable which was used to run rclone.
|
||||||
|
ex, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.CommandContext(ctx, ex, allArgs...)
|
||||||
|
|
||||||
|
// Run the command and get the output for error and stdout combined.
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fs.Errorf(nil, "Command error %v", err)
|
||||||
|
return Params{
|
||||||
|
"result": string(output),
|
||||||
|
"error": true,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return Params{
|
||||||
|
"result": string(output),
|
||||||
|
"error": false,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
@ -5,6 +5,8 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/rclone/rclone/fstest"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
@ -119,3 +121,27 @@ func TestCoreQuit(t *testing.T) {
|
|||||||
_, err := call.Fn(context.Background(), in)
|
_, err := call.Fn(context.Background(), in)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// core/command: Runs a raw rclone command
|
||||||
|
func TestCoreCommand(t *testing.T) {
|
||||||
|
call := Calls.Get("core/command")
|
||||||
|
r := fstest.NewRun(t)
|
||||||
|
defer r.Finalise()
|
||||||
|
|
||||||
|
in := Params{
|
||||||
|
"command": "ls",
|
||||||
|
"opt": map[string]string{
|
||||||
|
"max-depth": "1",
|
||||||
|
},
|
||||||
|
"arg": []string{
|
||||||
|
r.FremoteName,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
got, err := call.Fn(context.Background(), in)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
errorBool, err := got.GetBool("error")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Equal(t, errorBool, false)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user