2018-03-05 11:44:16 +00:00
|
|
|
package rc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2018-03-05 11:44:16 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-05-29 09:45:59 +00:00
|
|
|
"io/ioutil"
|
2018-03-05 11:44:16 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-28 17:47:38 +00: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 11:44:16 +00:00
|
|
|
"github.com/spf13/cobra"
|
2018-04-26 15:59:10 +00:00
|
|
|
"github.com/spf13/pflag"
|
2018-03-05 11:44:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-10-26 13:45:44 +00:00
|
|
|
noOutput = false
|
|
|
|
url = "http://localhost:5572/"
|
|
|
|
jsonInput = ""
|
2018-11-03 12:40:50 +00:00
|
|
|
authUser = ""
|
|
|
|
authPass = ""
|
2019-06-08 07:45:55 +00:00
|
|
|
loopback = false
|
2018-03-05 11:44:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmd.Root.AddCommand(commandDefintion)
|
|
|
|
commandDefintion.Flags().BoolVarP(&noOutput, "no-output", "", noOutput, "If set don't output the JSON result.")
|
|
|
|
commandDefintion.Flags().StringVarP(&url, "url", "", url, "URL to connect to rclone remote control.")
|
2018-10-26 13:45:44 +00:00
|
|
|
commandDefintion.Flags().StringVarP(&jsonInput, "json", "", jsonInput, "Input JSON - use instead of key=value args.")
|
2018-11-03 12:40:50 +00:00
|
|
|
commandDefintion.Flags().StringVarP(&authUser, "user", "", "", "Username to use to rclone remote control.")
|
|
|
|
commandDefintion.Flags().StringVarP(&authPass, "pass", "", "", "Password to use to connect to rclone remote control.")
|
2019-06-08 07:45:55 +00:00
|
|
|
commandDefintion.Flags().BoolVarP(&loopback, "loopback", "", false, "If set connect to this rclone instance not via HTTP.")
|
2018-03-05 11:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var commandDefintion = &cobra.Command{
|
|
|
|
Use: "rc commands parameter",
|
|
|
|
Short: `Run a command against a running rclone.`,
|
|
|
|
Long: `
|
2018-11-03 12:40:50 +00: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 11:44:16 +00:00
|
|
|
|
|
|
|
Arguments should be passed in as parameter=value.
|
|
|
|
|
|
|
|
The result will be returned as a JSON object by default.
|
|
|
|
|
2018-10-26 13:45:44 +00: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 07:45:55 +00: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 09:45:59 +00:00
|
|
|
Use "rclone rc" to see a list of all possible commands.`,
|
2018-03-05 11:44:16 +00:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(0, 1E9, command, args)
|
|
|
|
cmd.Run(false, false, command, func() error {
|
2018-11-03 12:40:50 +00:00
|
|
|
parseFlags()
|
2018-03-05 11:44:16 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
return list()
|
|
|
|
}
|
|
|
|
return run(args)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-11-03 12:40:50 +00: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 11:44:16 +00: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
|
|
|
|
func doCall(path string, in rc.Params) (out rc.Params, err error) {
|
2019-06-08 07:45:55 +00: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-06-17 08:34:30 +00:00
|
|
|
return call.Fn(context.Background(), in)
|
2019-06-08 07:45:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 11:44:16 +00: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 12:40:50 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to make request")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
if authUser != "" || authPass != "" {
|
|
|
|
req.SetBasicAuth(authUser, authPass)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
2018-03-05 11:44:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "connection failed")
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(resp.Body, &err)
|
|
|
|
|
2018-05-29 09:45:59 +00: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 11:44:16 +00: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
|
|
|
|
func run(args []string) (err error) {
|
|
|
|
path := strings.Trim(args[0], "/")
|
|
|
|
|
|
|
|
// parse input
|
|
|
|
in := make(rc.Params)
|
2018-10-26 13:45:44 +00: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 11:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the call
|
|
|
|
out, callErr := doCall(path, in)
|
|
|
|
|
|
|
|
// Write the JSON blob to stdout if required
|
|
|
|
if out != nil && !noOutput {
|
2018-03-14 19:48:01 +00:00
|
|
|
err := rc.WriteJSON(os.Stdout, out)
|
2018-03-05 11:44:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to output JSON")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return callErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the available commands to stdout
|
|
|
|
func list() error {
|
|
|
|
list, err := doCall("rc/list", nil)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
fmt.Printf("### %s: %s\n\n", info["Path"], info["Title"])
|
|
|
|
fmt.Printf("%s\n\n", info["Help"])
|
2018-11-03 12:40:50 +00:00
|
|
|
if authRequired := info["AuthRequired"]; authRequired != nil {
|
|
|
|
if authRequired.(bool) {
|
|
|
|
fmt.Printf("Authentication is required for this call.\n\n")
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 11:44:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|