2018-03-05 12:44:16 +01:00
|
|
|
// Define the internal rc functions
|
|
|
|
|
|
|
|
package rc
|
|
|
|
|
2018-04-05 16:12:34 +02:00
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2021-11-04 11:12:57 +01:00
|
|
|
"fmt"
|
2020-07-27 20:17:09 +02:00
|
|
|
"net/http"
|
2018-04-05 16:12:34 +02:00
|
|
|
"os"
|
2020-07-14 20:09:59 +02:00
|
|
|
"os/exec"
|
2018-04-23 21:10:28 +02:00
|
|
|
"runtime"
|
2020-08-18 22:21:53 +02:00
|
|
|
"strings"
|
2019-10-17 16:04:22 +02:00
|
|
|
"time"
|
2018-04-05 16:12:34 +02:00
|
|
|
|
2020-08-18 22:21:53 +02:00
|
|
|
"github.com/coreos/go-semver/semver"
|
2019-10-17 16:04:22 +02:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
2019-10-17 16:04:22 +02:00
|
|
|
"github.com/rclone/rclone/lib/atexit"
|
2021-03-21 13:13:09 +01:00
|
|
|
"github.com/rclone/rclone/lib/buildinfo"
|
2022-10-27 13:16:31 +02:00
|
|
|
"github.com/rclone/rclone/lib/debug"
|
2018-04-05 16:12:34 +02:00
|
|
|
)
|
2018-03-05 12:44:16 +01:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
2018-11-04 17:16:43 +01:00
|
|
|
Path: "rc/noopauth",
|
|
|
|
AuthRequired: true,
|
|
|
|
Fn: rcNoop,
|
|
|
|
Title: "Echo the input to the output parameters requiring auth",
|
2018-03-05 12:44:16 +01:00
|
|
|
Help: `
|
|
|
|
This echoes the input parameters to the output parameters for testing
|
2018-11-03 17:37:09 +01:00
|
|
|
purposes. It can be used to check that rclone is still alive and to
|
|
|
|
check that parameter passing is working properly.`,
|
|
|
|
})
|
|
|
|
Add(Call{
|
2018-11-04 17:16:43 +01:00
|
|
|
Path: "rc/noop",
|
|
|
|
Fn: rcNoop,
|
|
|
|
Title: "Echo the input to the output parameters",
|
2018-11-03 17:37:09 +01:00
|
|
|
Help: `
|
|
|
|
This echoes the input parameters to the output parameters for testing
|
2018-03-05 12:44:16 +01:00
|
|
|
purposes. It can be used to check that rclone is still alive and to
|
|
|
|
check that parameter passing is working properly.`,
|
|
|
|
})
|
2018-11-04 17:16:43 +01:00
|
|
|
}
|
|
|
|
|
2019-04-30 14:06:24 +02:00
|
|
|
// Echo the input to the output parameters
|
2019-06-17 10:34:30 +02:00
|
|
|
func rcNoop(ctx context.Context, in Params) (out Params, err error) {
|
2018-11-04 17:16:43 +01:00
|
|
|
return in, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-03-05 12:44:16 +01:00
|
|
|
Add(Call{
|
|
|
|
Path: "rc/error",
|
|
|
|
Fn: rcError,
|
|
|
|
Title: "This returns an error",
|
|
|
|
Help: `
|
|
|
|
This returns an error with the input as part of its error string.
|
|
|
|
Useful for testing error handling.`,
|
|
|
|
})
|
2018-11-04 17:16:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an error regardless
|
2019-06-17 10:34:30 +02:00
|
|
|
func rcError(ctx context.Context, in Params) (out Params, err error) {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("arbitrary error on input %+v", in)
|
2018-11-04 17:16:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-03-05 12:44:16 +01:00
|
|
|
Add(Call{
|
|
|
|
Path: "rc/list",
|
|
|
|
Fn: rcList,
|
|
|
|
Title: "List all the registered remote control commands",
|
|
|
|
Help: `
|
|
|
|
This lists all the registered remote control commands as a JSON map in
|
|
|
|
the commands response.`,
|
|
|
|
})
|
2018-11-04 17:16:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// List the registered commands
|
2019-06-17 10:34:30 +02:00
|
|
|
func rcList(ctx context.Context, in Params) (out Params, err error) {
|
2018-11-04 17:16:43 +01:00
|
|
|
out = make(Params)
|
|
|
|
out["commands"] = Calls.List()
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-04-05 16:12:34 +02:00
|
|
|
Add(Call{
|
2018-04-23 21:44:44 +02:00
|
|
|
Path: "core/pid",
|
2018-04-05 16:12:34 +02:00
|
|
|
Fn: rcPid,
|
|
|
|
Title: "Return PID of current process",
|
|
|
|
Help: `
|
|
|
|
This returns PID of current process.
|
|
|
|
Useful for stopping rclone process.`,
|
|
|
|
})
|
2018-11-04 17:16:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return PID of current process
|
2019-06-17 10:34:30 +02:00
|
|
|
func rcPid(ctx context.Context, in Params) (out Params, err error) {
|
2018-11-04 17:16:43 +01:00
|
|
|
out = make(Params)
|
|
|
|
out["pid"] = os.Getpid()
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-04-23 21:10:28 +02:00
|
|
|
Add(Call{
|
|
|
|
Path: "core/memstats",
|
|
|
|
Fn: rcMemStats,
|
2018-04-23 21:44:44 +02:00
|
|
|
Title: "Returns the memory statistics",
|
2018-04-23 21:10:28 +02:00
|
|
|
Help: `
|
|
|
|
This returns the memory statistics of the running program. What the values mean
|
|
|
|
are explained in the go docs: https://golang.org/pkg/runtime/#MemStats
|
|
|
|
|
|
|
|
The most interesting values for most people are:
|
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
- HeapAlloc - this is the amount of memory rclone is actually using
|
|
|
|
- HeapSys - this is the amount of memory rclone has obtained from the OS
|
|
|
|
- Sys - this is the total amount of memory requested from the OS
|
|
|
|
- It is virtual memory so may include unused memory
|
2018-05-11 14:53:13 +02:00
|
|
|
`,
|
|
|
|
})
|
2018-04-05 16:12:34 +02:00
|
|
|
}
|
2018-04-23 21:10:28 +02:00
|
|
|
|
|
|
|
// Return the memory statistics
|
2019-06-17 10:34:30 +02:00
|
|
|
func rcMemStats(ctx context.Context, in Params) (out Params, err error) {
|
2018-04-23 21:10:28 +02:00
|
|
|
out = make(Params)
|
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
out["Alloc"] = m.Alloc
|
|
|
|
out["TotalAlloc"] = m.TotalAlloc
|
|
|
|
out["Sys"] = m.Sys
|
|
|
|
out["Mallocs"] = m.Mallocs
|
|
|
|
out["Frees"] = m.Frees
|
|
|
|
out["HeapAlloc"] = m.HeapAlloc
|
|
|
|
out["HeapSys"] = m.HeapSys
|
|
|
|
out["HeapIdle"] = m.HeapIdle
|
|
|
|
out["HeapInuse"] = m.HeapInuse
|
|
|
|
out["HeapReleased"] = m.HeapReleased
|
|
|
|
out["HeapObjects"] = m.HeapObjects
|
|
|
|
out["StackInuse"] = m.StackInuse
|
|
|
|
out["StackSys"] = m.StackSys
|
|
|
|
out["MSpanInuse"] = m.MSpanInuse
|
|
|
|
out["MSpanSys"] = m.MSpanSys
|
|
|
|
out["MCacheInuse"] = m.MCacheInuse
|
|
|
|
out["MCacheSys"] = m.MCacheSys
|
|
|
|
out["BuckHashSys"] = m.BuckHashSys
|
|
|
|
out["GCSys"] = m.GCSys
|
|
|
|
out["OtherSys"] = m.OtherSys
|
|
|
|
return out, nil
|
|
|
|
}
|
2018-05-11 14:53:13 +02:00
|
|
|
|
2018-11-04 17:16:43 +01:00
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "core/gc",
|
|
|
|
Fn: rcGc,
|
|
|
|
Title: "Runs a garbage collection.",
|
|
|
|
Help: `
|
|
|
|
This tells the go runtime to do a garbage collection run. It isn't
|
|
|
|
necessary to call this normally, but it can be useful for debugging
|
|
|
|
memory problems.
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:53:13 +02:00
|
|
|
// Do a garbage collection run
|
2019-06-17 10:34:30 +02:00
|
|
|
func rcGc(ctx context.Context, in Params) (out Params, err error) {
|
2018-05-11 14:53:13 +02:00
|
|
|
runtime.GC()
|
2018-10-27 19:29:20 +02:00
|
|
|
return nil, nil
|
2018-05-11 14:53:13 +02:00
|
|
|
}
|
2018-11-04 17:16:43 +01:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "core/version",
|
|
|
|
Fn: rcVersion,
|
|
|
|
Title: "Shows the current version of rclone and the go runtime.",
|
|
|
|
Help: `
|
2021-08-16 11:30:01 +02:00
|
|
|
This shows the current version of go and the go runtime:
|
2019-10-27 11:32:56 +01:00
|
|
|
|
2020-10-13 23:49:58 +02:00
|
|
|
- version - rclone version, e.g. "v1.53.0"
|
2020-08-18 22:21:53 +02:00
|
|
|
- decomposed - version number as [major, minor, patch]
|
2018-11-04 17:16:43 +01:00
|
|
|
- isGit - boolean - true if this was compiled from the git version
|
2020-08-18 22:21:53 +02:00
|
|
|
- isBeta - boolean - true if this is a beta version
|
2018-11-04 17:16:43 +01:00
|
|
|
- os - OS in use as according to Go
|
|
|
|
- arch - cpu architecture in use according to Go
|
|
|
|
- goVersion - version of Go runtime in use
|
2021-03-21 13:13:09 +01:00
|
|
|
- linking - type of rclone executable (static or dynamic)
|
|
|
|
- goTags - space separated build tags or "none"
|
2018-11-04 17:16:43 +01:00
|
|
|
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return version info
|
2019-06-17 10:34:30 +02:00
|
|
|
func rcVersion(ctx context.Context, in Params) (out Params, err error) {
|
2020-08-18 22:21:53 +02:00
|
|
|
version, err := semver.NewVersion(fs.Version[1:])
|
2018-11-04 17:16:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-21 13:13:09 +01:00
|
|
|
linking, tagString := buildinfo.GetLinkingAndTags()
|
2018-11-04 17:16:43 +01:00
|
|
|
out = Params{
|
|
|
|
"version": fs.Version,
|
2020-08-18 22:21:53 +02:00
|
|
|
"decomposed": version.Slice(),
|
|
|
|
"isGit": strings.HasSuffix(fs.Version, "-DEV"),
|
|
|
|
"isBeta": version.PreRelease != "",
|
2018-11-04 17:16:43 +01:00
|
|
|
"os": runtime.GOOS,
|
|
|
|
"arch": runtime.GOARCH,
|
|
|
|
"goVersion": runtime.Version(),
|
2021-03-21 13:13:09 +01:00
|
|
|
"linking": linking,
|
|
|
|
"goTags": tagString,
|
2018-11-04 17:16:43 +01:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2018-11-04 17:30:56 +01:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "core/obscure",
|
|
|
|
Fn: rcObscure,
|
|
|
|
Title: "Obscures a string passed in.",
|
|
|
|
Help: `
|
|
|
|
Pass a clear string and rclone will obscure it for the config file:
|
|
|
|
- clear - string
|
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
Returns:
|
2018-11-04 17:30:56 +01:00
|
|
|
- obscured - string
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return obscured string
|
2019-06-17 10:34:30 +02:00
|
|
|
func rcObscure(ctx context.Context, in Params) (out Params, err error) {
|
2018-11-04 17:30:56 +01:00
|
|
|
clear, err := in.GetString("clear")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
obscured, err := obscure.Obscure(clear)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = Params{
|
|
|
|
"obscured": obscured,
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2019-10-17 16:04:22 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "core/quit",
|
|
|
|
Fn: rcQuit,
|
|
|
|
Title: "Terminates the app.",
|
|
|
|
Help: `
|
2021-08-16 11:30:01 +02:00
|
|
|
(Optional) Pass an exit code to be used for terminating the app:
|
2019-10-17 16:04:22 +02:00
|
|
|
- exitCode - int
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminates app
|
|
|
|
func rcQuit(ctx context.Context, in Params) (out Params, err error) {
|
|
|
|
code, err := in.GetInt64("exitCode")
|
|
|
|
|
|
|
|
if IsErrParamInvalid(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if IsErrParamNotFound(err) {
|
|
|
|
code = 0
|
|
|
|
}
|
|
|
|
exitCode := int(code)
|
|
|
|
|
|
|
|
go func(exitCode int) {
|
|
|
|
time.Sleep(time.Millisecond * 1500)
|
|
|
|
atexit.Run()
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}(exitCode)
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-10-26 17:27:20 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "debug/set-mutex-profile-fraction",
|
|
|
|
Fn: rcSetMutexProfileFraction,
|
|
|
|
Title: "Set runtime.SetMutexProfileFraction for mutex profiling.",
|
|
|
|
Help: `
|
|
|
|
SetMutexProfileFraction controls the fraction of mutex contention
|
|
|
|
events that are reported in the mutex profile. On average 1/rate
|
|
|
|
events are reported. The previous rate is returned.
|
|
|
|
|
|
|
|
To turn off profiling entirely, pass rate 0. To just read the current
|
|
|
|
rate, pass rate < 0. (For n>1 the details of sampling may change.)
|
|
|
|
|
|
|
|
Once this is set you can look use this to profile the mutex contention:
|
|
|
|
|
|
|
|
go tool pprof http://localhost:5572/debug/pprof/mutex
|
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
Parameters:
|
2019-10-26 17:27:20 +02:00
|
|
|
|
|
|
|
- rate - int
|
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
Results:
|
2019-10-26 17:27:20 +02:00
|
|
|
|
|
|
|
- previousRate - int
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func rcSetMutexProfileFraction(ctx context.Context, in Params) (out Params, err error) {
|
|
|
|
rate, err := in.GetInt64("rate")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
previousRate := runtime.SetMutexProfileFraction(int(rate))
|
|
|
|
out = make(Params)
|
|
|
|
out["previousRate"] = previousRate
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "debug/set-block-profile-rate",
|
|
|
|
Fn: rcSetBlockProfileRate,
|
|
|
|
Title: "Set runtime.SetBlockProfileRate for blocking profiling.",
|
|
|
|
Help: `
|
|
|
|
SetBlockProfileRate controls the fraction of goroutine blocking events
|
|
|
|
that are reported in the blocking profile. The profiler aims to sample
|
|
|
|
an average of one blocking event per rate nanoseconds spent blocked.
|
|
|
|
|
|
|
|
To include every blocking event in the profile, pass rate = 1. To turn
|
|
|
|
off profiling entirely, pass rate <= 0.
|
|
|
|
|
|
|
|
After calling this you can use this to see the blocking profile:
|
|
|
|
|
|
|
|
go tool pprof http://localhost:5572/debug/pprof/block
|
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
Parameters:
|
2019-10-26 17:27:20 +02:00
|
|
|
|
|
|
|
- rate - int
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func rcSetBlockProfileRate(ctx context.Context, in Params) (out Params, err error) {
|
|
|
|
rate, err := in.GetInt64("rate")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
runtime.SetBlockProfileRate(int(rate))
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-07-14 20:09:59 +02:00
|
|
|
|
2022-10-27 13:16:31 +02:00
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "debug/set-soft-memory-limit",
|
|
|
|
Fn: rcSetSoftMemoryLimit,
|
|
|
|
Title: "Call runtime/debug.SetMemoryLimit for setting a soft memory limit for the runtime.",
|
|
|
|
Help: `
|
|
|
|
SetMemoryLimit provides the runtime with a soft memory limit.
|
|
|
|
|
|
|
|
The runtime undertakes several processes to try to respect this memory limit, including
|
|
|
|
adjustments to the frequency of garbage collections and returning memory to the underlying
|
|
|
|
system more aggressively. This limit will be respected even if GOGC=off (or, if SetGCPercent(-1) is executed).
|
|
|
|
|
|
|
|
The input limit is provided as bytes, and includes all memory mapped, managed, and not
|
|
|
|
released by the Go runtime. Notably, it does not account for space used by the Go binary
|
|
|
|
and memory external to Go, such as memory managed by the underlying system on behalf of
|
|
|
|
the process, or memory managed by non-Go code inside the same process.
|
|
|
|
Examples of excluded memory sources include: OS kernel memory held on behalf of the process,
|
|
|
|
memory allocated by C code, and memory mapped by syscall.Mmap (because it is not managed by the Go runtime).
|
|
|
|
|
|
|
|
A zero limit or a limit that's lower than the amount of memory used by the Go runtime may cause
|
|
|
|
the garbage collector to run nearly continuously. However, the application may still make progress.
|
|
|
|
|
|
|
|
The memory limit is always respected by the Go runtime, so to effectively disable this behavior,
|
|
|
|
set the limit very high. math.MaxInt64 is the canonical value for disabling the limit, but values
|
|
|
|
much greater than the available memory on the underlying system work just as well.
|
|
|
|
|
|
|
|
See https://go.dev/doc/gc-guide for a detailed guide explaining the soft memory limit in more detail,
|
|
|
|
as well as a variety of common use-cases and scenarios.
|
|
|
|
|
|
|
|
SetMemoryLimit returns the previously set memory limit. A negative input does not adjust the limit,
|
|
|
|
and allows for retrieval of the currently set memory limit.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
- mem-limit - int
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func rcSetSoftMemoryLimit(ctx context.Context, in Params) (out Params, err error) {
|
|
|
|
memLimit, err := in.GetInt64("mem-limit")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
oldMemLimit, err := debug.SetMemoryLimit(memLimit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = Params{
|
|
|
|
"existing-mem-limit": oldMemLimit,
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "debug/set-gc-percent",
|
|
|
|
Fn: rcSetGCPercent,
|
|
|
|
Title: "Call runtime/debug.SetGCPercent for setting the garbage collection target percentage.",
|
|
|
|
Help: `
|
|
|
|
SetGCPercent sets the garbage collection target percentage: a collection is triggered
|
|
|
|
when the ratio of freshly allocated data to live data remaining after the previous collection
|
|
|
|
reaches this percentage. SetGCPercent returns the previous setting. The initial setting is the
|
|
|
|
value of the GOGC environment variable at startup, or 100 if the variable is not set.
|
|
|
|
|
|
|
|
This setting may be effectively reduced in order to maintain a memory limit.
|
|
|
|
A negative percentage effectively disables garbage collection, unless the memory limit is reached.
|
|
|
|
|
|
|
|
See https://pkg.go.dev/runtime/debug#SetMemoryLimit for more details.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
- gc-percent - int
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func rcSetGCPercent(ctx context.Context, in Params) (out Params, err error) {
|
|
|
|
gcPercent, err := in.GetInt64("gc-percent")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
oldGCPercent := debug.SetGCPercent(int(gcPercent))
|
|
|
|
out = Params{
|
|
|
|
"existing-gc-percent": oldGCPercent,
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:09:59 +02:00
|
|
|
func init() {
|
|
|
|
Add(Call{
|
2020-07-27 20:17:09 +02:00
|
|
|
Path: "core/command",
|
|
|
|
AuthRequired: true,
|
|
|
|
Fn: rcRunCommand,
|
|
|
|
NeedsRequest: true,
|
|
|
|
NeedsResponse: true,
|
|
|
|
Title: "Run a rclone terminal command over rc.",
|
2021-08-16 11:30:01 +02:00
|
|
|
Help: `This takes the following parameters:
|
2020-07-14 20:09:59 +02:00
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
- 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.
|
|
|
|
- returnType - one of ("COMBINED_OUTPUT", "STREAM", "STREAM_ONLY_STDOUT", "STREAM_ONLY_STDERR").
|
|
|
|
- Defaults to "COMBINED_OUTPUT" if not set.
|
|
|
|
- The STREAM returnTypes will write the output to the body of the HTTP message.
|
|
|
|
- The COMBINED_OUTPUT will write the output to the "result" parameter.
|
2020-07-14 20:09:59 +02:00
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
Returns:
|
2020-07-14 20:09:59 +02:00
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
- result - result from the backend command.
|
|
|
|
- Only set when using returnType "COMBINED_OUTPUT".
|
|
|
|
- error - set if rclone exits with an error code.
|
|
|
|
- returnType - one of ("COMBINED_OUTPUT", "STREAM", "STREAM_ONLY_STDOUT", "STREAM_ONLY_STDERR").
|
2020-07-14 20:09:59 +02:00
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
Example:
|
2020-07-14 20:09:59 +02:00
|
|
|
|
|
|
|
rclone rc core/command command=ls -a mydrive:/ -o max-depth=1
|
2021-01-09 14:26:06 +01:00
|
|
|
rclone rc core/command -a ls -a mydrive:/ -o max-depth=1
|
2020-07-14 20:09:59 +02:00
|
|
|
|
2021-08-16 11:30:01 +02:00
|
|
|
Returns:
|
2020-07-14 20:09:59 +02:00
|
|
|
|
|
|
|
` + "```" + `
|
|
|
|
{
|
|
|
|
"error": false,
|
|
|
|
"result": "<Raw command line output>"
|
|
|
|
}
|
|
|
|
|
2022-10-27 13:16:31 +02:00
|
|
|
OR
|
2020-07-14 20:09:59 +02:00
|
|
|
{
|
|
|
|
"error": true,
|
|
|
|
"result": "<Raw command line output>"
|
|
|
|
}
|
|
|
|
|
2020-09-03 12:47:48 +02:00
|
|
|
` + "```" + `
|
2020-07-14 20:09:59 +02:00
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:17:09 +02:00
|
|
|
returnType, err := in.GetString("returnType")
|
|
|
|
if err != nil {
|
|
|
|
returnType = "COMBINED_OUTPUT"
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:26:06 +01:00
|
|
|
var httpResponse http.ResponseWriter
|
2020-07-27 20:17:09 +02:00
|
|
|
httpResponse, err = in.GetHTTPResponseWriter()
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("response object is required\n" + err.Error())
|
2020-07-27 20:17:09 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 20:09:59 +02:00
|
|
|
var allArgs = []string{}
|
|
|
|
if command != "" {
|
2020-10-13 23:49:58 +02:00
|
|
|
// Add the command e.g.: ls to the args
|
2020-07-14 20:09:59 +02:00
|
|
|
allArgs = append(allArgs, command)
|
|
|
|
}
|
|
|
|
// Add all from arg
|
2021-03-21 13:13:09 +01:00
|
|
|
allArgs = append(allArgs, arg...)
|
2020-07-14 20:09:59 +02:00
|
|
|
|
2020-10-13 23:49:58 +02:00
|
|
|
// Add flags to args for e.g. --max-depth 1 comes in as { max-depth 1 }.
|
2020-07-14 20:09:59 +02:00
|
|
|
// 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...)
|
|
|
|
|
2020-07-27 20:17:09 +02:00
|
|
|
if returnType == "COMBINED_OUTPUT" {
|
|
|
|
// Run the command and get the output for error and stdout combined.
|
2020-07-14 20:09:59 +02:00
|
|
|
|
2020-07-27 20:17:09 +02:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return Params{
|
|
|
|
"result": string(out),
|
|
|
|
"error": true,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-07-14 20:09:59 +02:00
|
|
|
return Params{
|
2020-07-27 20:17:09 +02:00
|
|
|
"result": string(out),
|
|
|
|
"error": false,
|
2020-07-14 20:09:59 +02:00
|
|
|
}, nil
|
2020-07-27 20:17:09 +02:00
|
|
|
} else if returnType == "STREAM_ONLY_STDOUT" {
|
2021-01-09 14:26:06 +01:00
|
|
|
cmd.Stdout = httpResponse
|
2020-07-27 20:17:09 +02:00
|
|
|
} else if returnType == "STREAM_ONLY_STDERR" {
|
2021-01-09 14:26:06 +01:00
|
|
|
cmd.Stderr = httpResponse
|
2020-07-27 20:17:09 +02:00
|
|
|
} else if returnType == "STREAM" {
|
2021-01-09 14:26:06 +01:00
|
|
|
cmd.Stdout = httpResponse
|
|
|
|
cmd.Stderr = httpResponse
|
|
|
|
} else {
|
2022-06-08 22:54:39 +02:00
|
|
|
return nil, fmt.Errorf("unknown returnType %q", returnType)
|
2020-07-14 20:09:59 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 20:17:09 +02:00
|
|
|
err = cmd.Run()
|
|
|
|
return nil, err
|
2020-07-14 20:09:59 +02:00
|
|
|
}
|