2018-03-05 12:44:16 +01:00
|
|
|
package rc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2018-03-05 12:44:16 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-05-29 11:45:59 +02:00
|
|
|
"io/ioutil"
|
2018-03-05 12:44:16 +01:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/fshttp"
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2018-03-05 12:44:16 +01:00
|
|
|
"github.com/spf13/cobra"
|
2018-04-26 17:59:10 +02:00
|
|
|
"github.com/spf13/pflag"
|
2018-03-05 12:44:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-10-26 15:45:44 +02:00
|
|
|
noOutput = false
|
|
|
|
url = "http://localhost:5572/"
|
|
|
|
jsonInput = ""
|
2018-11-03 13:40:50 +01:00
|
|
|
authUser = ""
|
|
|
|
authPass = ""
|
2019-06-08 09:45:55 +02:00
|
|
|
loopback = false
|
2018-03-05 12:44:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-11 17:58:11 +02:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
|
|
|
commandDefinition.Flags().BoolVarP(&noOutput, "no-output", "", noOutput, "If set don't output the JSON result.")
|
|
|
|
commandDefinition.Flags().StringVarP(&url, "url", "", url, "URL to connect to rclone remote control.")
|
|
|
|
commandDefinition.Flags().StringVarP(&jsonInput, "json", "", jsonInput, "Input JSON - use instead of key=value args.")
|
|
|
|
commandDefinition.Flags().StringVarP(&authUser, "user", "", "", "Username to use to rclone remote control.")
|
|
|
|
commandDefinition.Flags().StringVarP(&authPass, "pass", "", "", "Password to use to connect to rclone remote control.")
|
|
|
|
commandDefinition.Flags().BoolVarP(&loopback, "loopback", "", false, "If set connect to this rclone instance not via HTTP.")
|
2018-03-05 12:44:16 +01:00
|
|
|
}
|
|
|
|
|
2019-10-11 17:58:11 +02:00
|
|
|
var commandDefinition = &cobra.Command{
|
2018-03-05 12:44:16 +01:00
|
|
|
Use: "rc commands parameter",
|
|
|
|
Short: `Run a command against a running rclone.`,
|
|
|
|
Long: `
|
2018-11-03 13:40:50 +01:00
|
|
|
|
|
|
|
This runs a command against a running rclone. Use the --url flag to
|
|
|
|
specify an non default URL to connect on. This can be either a
|
|
|
|
":port" which is taken to mean "http://localhost:port" or a
|
|
|
|
"host:port" which is taken to mean "http://host:port"
|
|
|
|
|
|
|
|
A username and password can be passed in with --user and --pass.
|
|
|
|
|
|
|
|
Note that --rc-addr, --rc-user, --rc-pass will be read also for --url,
|
|
|
|
--user, --pass.
|
2018-03-05 12:44:16 +01:00
|
|
|
|
|
|
|
Arguments should be passed in as parameter=value.
|
|
|
|
|
|
|
|
The result will be returned as a JSON object by default.
|
|
|
|
|
2018-10-26 15:45:44 +02:00
|
|
|
The --json parameter can be used to pass in a JSON blob as an input
|
|
|
|
instead of key=value arguments. This is the only way of passing in
|
|
|
|
more complicated values.
|
|
|
|
|
2019-06-08 09:45:55 +02:00
|
|
|
Use --loopback to connect to the rclone instance running "rclone rc".
|
|
|
|
This is very useful for testing commands without having to run an
|
|
|
|
rclone rc server, eg:
|
|
|
|
|
|
|
|
rclone rc --loopback operations/about fs=/
|
|
|
|
|
2018-05-29 11:45:59 +02:00
|
|
|
Use "rclone rc" to see a list of all possible commands.`,
|
2018-03-05 12:44:16 +01:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
2019-09-05 14:59:06 +02:00
|
|
|
cmd.CheckArgs(0, 1e9, command, args)
|
2018-03-05 12:44:16 +01:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2019-09-04 21:21:10 +02:00
|
|
|
ctx := context.Background()
|
2018-11-03 13:40:50 +01:00
|
|
|
parseFlags()
|
2018-03-05 12:44:16 +01:00
|
|
|
if len(args) == 0 {
|
2019-09-04 21:21:10 +02:00
|
|
|
return list(ctx)
|
2018-03-05 12:44:16 +01:00
|
|
|
}
|
2019-09-04 21:21:10 +02:00
|
|
|
return run(ctx, args)
|
2018-03-05 12:44:16 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-11-03 13:40:50 +01:00
|
|
|
// Parse the flags
|
|
|
|
func parseFlags() {
|
|
|
|
// set alternates from alternate flags
|
|
|
|
setAlternateFlag("rc-addr", &url)
|
|
|
|
setAlternateFlag("rc-user", &authUser)
|
|
|
|
setAlternateFlag("rc-pass", &authPass)
|
|
|
|
// If url is just :port then fix it up
|
|
|
|
if strings.HasPrefix(url, ":") {
|
|
|
|
url = "localhost" + url
|
|
|
|
}
|
|
|
|
// if url is just host:port add http://
|
|
|
|
if !strings.HasPrefix(url, "http:") && !strings.HasPrefix(url, "https:") {
|
|
|
|
url = "http://" + url
|
|
|
|
}
|
|
|
|
// if url doesn't end with / add it
|
|
|
|
if !strings.HasSuffix(url, "/") {
|
|
|
|
url += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user set flagName set the output to its value
|
|
|
|
func setAlternateFlag(flagName string, output *string) {
|
|
|
|
if rcFlag := pflag.Lookup(flagName); rcFlag != nil && rcFlag.Changed {
|
|
|
|
*output = rcFlag.Value.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 12:44:16 +01:00
|
|
|
// do a call from (path, in) to (out, err).
|
|
|
|
//
|
|
|
|
// if err is set, out may be a valid error return or it may be nil
|
2019-09-04 21:21:10 +02:00
|
|
|
func doCall(ctx context.Context, path string, in rc.Params) (out rc.Params, err error) {
|
2019-06-08 09:45:55 +02:00
|
|
|
// If loopback set, short circuit HTTP request
|
|
|
|
if loopback {
|
|
|
|
call := rc.Calls.Get(path)
|
|
|
|
if call == nil {
|
|
|
|
return nil, errors.Errorf("method %q not found", path)
|
|
|
|
}
|
2019-08-13 12:51:16 +02:00
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "loopback call failed")
|
|
|
|
}
|
|
|
|
// Reshape (serialize then deserialize) the data so it is in the form expected
|
|
|
|
err = rc.Reshape(&out, out)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "loopback reshape failed")
|
|
|
|
}
|
|
|
|
return out, nil
|
2019-06-08 09:45:55 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 12:44:16 +01:00
|
|
|
// Do HTTP request
|
|
|
|
client := fshttp.NewClient(fs.Config)
|
|
|
|
url += path
|
|
|
|
data, err := json.Marshal(in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to encode JSON")
|
|
|
|
}
|
2018-11-03 13:40:50 +01:00
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to make request")
|
|
|
|
}
|
2019-09-04 21:21:10 +02:00
|
|
|
req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext
|
2018-11-03 13:40:50 +01:00
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
if authUser != "" || authPass != "" {
|
|
|
|
req.SetBasicAuth(authUser, authPass)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
2018-03-05 12:44:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "connection failed")
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(resp.Body, &err)
|
|
|
|
|
2018-05-29 11:45:59 +02:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
var body []byte
|
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
var bodyString string
|
|
|
|
if err == nil {
|
|
|
|
bodyString = string(body)
|
|
|
|
} else {
|
|
|
|
bodyString = err.Error()
|
|
|
|
}
|
|
|
|
bodyString = strings.TrimSpace(bodyString)
|
|
|
|
return nil, errors.Errorf("Failed to read rc response: %s: %s", resp.Status, bodyString)
|
|
|
|
}
|
|
|
|
|
2018-03-05 12:44:16 +01:00
|
|
|
// Parse output
|
|
|
|
out = make(rc.Params)
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&out)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to decode JSON")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check we got 200 OK
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
err = errors.Errorf("operation %q failed: %v", path, out["error"])
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the remote control command passed in
|
2019-09-04 21:21:10 +02:00
|
|
|
func run(ctx context.Context, args []string) (err error) {
|
2018-03-05 12:44:16 +01:00
|
|
|
path := strings.Trim(args[0], "/")
|
|
|
|
|
|
|
|
// parse input
|
|
|
|
in := make(rc.Params)
|
2018-10-26 15:45:44 +02:00
|
|
|
params := args[1:]
|
|
|
|
if jsonInput == "" {
|
|
|
|
for _, param := range params {
|
|
|
|
equals := strings.IndexRune(param, '=')
|
|
|
|
if equals < 0 {
|
|
|
|
return errors.Errorf("no '=' found in parameter %q", param)
|
|
|
|
}
|
|
|
|
key, value := param[:equals], param[equals+1:]
|
|
|
|
in[key] = value
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(params) > 0 {
|
|
|
|
return errors.New("can't use --json and parameters together")
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(jsonInput), &in)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "bad --json input")
|
2018-03-05 12:44:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the call
|
2019-09-04 21:21:10 +02:00
|
|
|
out, callErr := doCall(ctx, path, in)
|
2018-03-05 12:44:16 +01:00
|
|
|
|
|
|
|
// Write the JSON blob to stdout if required
|
|
|
|
if out != nil && !noOutput {
|
2018-03-14 20:48:01 +01:00
|
|
|
err := rc.WriteJSON(os.Stdout, out)
|
2018-03-05 12:44:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to output JSON")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return callErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the available commands to stdout
|
2019-09-04 21:21:10 +02:00
|
|
|
func list(ctx context.Context) error {
|
|
|
|
list, err := doCall(ctx, "rc/list", nil)
|
2018-03-05 12:44:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to list")
|
|
|
|
}
|
|
|
|
commands, ok := list["commands"].([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return errors.New("bad JSON")
|
|
|
|
}
|
|
|
|
for _, command := range commands {
|
|
|
|
info, ok := command.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return errors.New("bad JSON")
|
|
|
|
}
|
2019-08-13 12:54:13 +02:00
|
|
|
fmt.Printf("### %s: %s {#%s}\n\n", info["Path"], info["Title"], info["Path"])
|
2018-03-05 12:44:16 +01:00
|
|
|
fmt.Printf("%s\n\n", info["Help"])
|
2018-11-03 13:40:50 +01:00
|
|
|
if authRequired := info["AuthRequired"]; authRequired != nil {
|
|
|
|
if authRequired.(bool) {
|
|
|
|
fmt.Printf("Authentication is required for this call.\n\n")
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 12:44:16 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|