2022-08-28 13:21:57 +02:00
|
|
|
// Package backend provides the backend command.
|
2020-04-28 13:58:34 +02:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/cmd/rc"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
options []string
|
2020-05-13 19:07:41 +02:00
|
|
|
useJSON bool
|
2020-04-28 13:58:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
|
|
|
cmdFlags := commandDefinition.Flags()
|
2021-08-16 11:30:01 +02:00
|
|
|
flags.StringArrayVarP(cmdFlags, &options, "option", "o", options, "Option in the form name=value or name")
|
|
|
|
flags.BoolVarP(cmdFlags, &useJSON, "json", "", useJSON, "Always output in JSON format")
|
2020-04-28 13:58:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var commandDefinition = &cobra.Command{
|
|
|
|
Use: "backend <command> remote:path [opts] <args>",
|
2021-11-04 12:50:43 +01:00
|
|
|
Short: `Run a backend-specific command.`,
|
2020-04-28 13:58:34 +02:00
|
|
|
Long: `
|
2021-11-04 12:50:43 +01:00
|
|
|
This runs a backend-specific command. The commands themselves (except
|
2020-04-28 13:58:34 +02:00
|
|
|
for "help" and "features") are defined by the backends and you should
|
|
|
|
see the backend docs for definitions.
|
|
|
|
|
|
|
|
You can discover what commands a backend implements by using
|
|
|
|
|
|
|
|
rclone backend help remote:
|
|
|
|
rclone backend help <backendname>
|
|
|
|
|
|
|
|
You can also discover information about the backend using (see
|
2022-01-12 19:51:26 +01:00
|
|
|
[operations/fsinfo](/rc/#operations-fsinfo) in the remote control docs
|
2020-04-28 13:58:34 +02:00
|
|
|
for more info).
|
|
|
|
|
|
|
|
rclone backend features remote:
|
|
|
|
|
2020-10-13 23:49:58 +02:00
|
|
|
Pass options to the backend command with -o. This should be key=value or key, e.g.:
|
2020-04-28 13:58:34 +02:00
|
|
|
|
|
|
|
rclone backend stats remote:path stats -o format=json -o long
|
|
|
|
|
|
|
|
Pass arguments to the backend by placing them on the end of the line
|
|
|
|
|
|
|
|
rclone backend cleanup remote:path file1 file2 file3
|
|
|
|
|
|
|
|
Note to run these commands on a running backend then see
|
2022-01-12 19:51:26 +01:00
|
|
|
[backend/command](/rc/#backend-command) in the rc docs.
|
2020-04-28 13:58:34 +02:00
|
|
|
`,
|
2022-11-26 23:40:49 +01:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.52",
|
|
|
|
},
|
2020-04-28 13:58:34 +02:00
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
2020-05-17 01:47:46 +02:00
|
|
|
cmd.CheckArgs(2, 1e6, command, args)
|
2020-04-28 13:58:34 +02:00
|
|
|
name, remote := args[0], args[1]
|
|
|
|
cmd.Run(false, false, command, func() error {
|
|
|
|
// show help if remote is a backend name
|
|
|
|
if name == "help" {
|
|
|
|
fsInfo, err := fs.Find(remote)
|
|
|
|
if err == nil {
|
|
|
|
return showHelp(fsInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create remote
|
|
|
|
fsInfo, configName, fsPath, config, err := fs.ConfigFs(remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := fsInfo.NewFs(context.Background(), configName, fsPath, config)
|
2020-04-28 13:58:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Run the command
|
|
|
|
var out interface{}
|
|
|
|
switch name {
|
|
|
|
case "help":
|
|
|
|
return showHelp(fsInfo)
|
|
|
|
case "features":
|
|
|
|
out = operations.GetFsInfo(f)
|
|
|
|
default:
|
|
|
|
doCommand := f.Features().Command
|
|
|
|
if doCommand == nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("%v: doesn't support backend commands", f)
|
2020-04-28 13:58:34 +02:00
|
|
|
}
|
|
|
|
arg := args[2:]
|
|
|
|
opt := rc.ParseOptions(options)
|
|
|
|
out, err = doCommand(context.Background(), name, arg, opt)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("command %q failed: %w", name, err)
|
2020-04-28 13:58:34 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
// Output the result
|
2020-05-13 19:07:41 +02:00
|
|
|
writeJSON := false
|
|
|
|
if useJSON {
|
|
|
|
writeJSON = true
|
|
|
|
} else {
|
|
|
|
switch x := out.(type) {
|
|
|
|
case nil:
|
|
|
|
case string:
|
|
|
|
fmt.Println(out)
|
|
|
|
case []string:
|
|
|
|
for _, line := range x {
|
|
|
|
fmt.Println(line)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
writeJSON = true
|
2020-04-28 13:58:34 +02:00
|
|
|
}
|
2020-05-13 19:07:41 +02:00
|
|
|
}
|
|
|
|
if writeJSON {
|
2020-04-28 13:58:34 +02:00
|
|
|
// Write indented JSON to the output
|
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
enc.SetIndent("", "\t")
|
|
|
|
err = enc.Encode(out)
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("failed to write JSON: %w", err)
|
2020-04-28 13:58:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// show help for a backend
|
|
|
|
func showHelp(fsInfo *fs.RegInfo) error {
|
|
|
|
cmds := fsInfo.CommandHelp
|
|
|
|
name := fsInfo.Name
|
|
|
|
if len(cmds) == 0 {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("%s backend has no commands", name)
|
2020-04-28 13:58:34 +02:00
|
|
|
}
|
2021-10-14 15:40:18 +02:00
|
|
|
fmt.Printf("## Backend commands\n\n")
|
2020-04-28 13:58:34 +02:00
|
|
|
fmt.Printf(`Here are the commands specific to the %s backend.
|
|
|
|
|
2020-05-25 08:05:53 +02:00
|
|
|
Run them with
|
2020-04-28 13:58:34 +02:00
|
|
|
|
|
|
|
rclone backend COMMAND remote:
|
|
|
|
|
|
|
|
The help below will explain what arguments each command takes.
|
|
|
|
|
2022-06-19 15:51:37 +02:00
|
|
|
See the [backend](/commands/rclone_backend/) command for more
|
2020-04-28 13:58:34 +02:00
|
|
|
info on how to pass options and arguments.
|
|
|
|
|
|
|
|
These can be run on a running backend using the rc command
|
2022-01-12 19:51:26 +01:00
|
|
|
[backend/command](/rc/#backend-command).
|
2020-04-28 13:58:34 +02:00
|
|
|
|
|
|
|
`, name)
|
|
|
|
for _, cmd := range cmds {
|
2021-10-14 15:40:18 +02:00
|
|
|
fmt.Printf("### %s\n\n", cmd.Name)
|
2020-04-28 13:58:34 +02:00
|
|
|
fmt.Printf("%s\n\n", cmd.Short)
|
|
|
|
fmt.Printf(" rclone backend %s remote: [options] [<arguments>+]\n\n", cmd.Name)
|
|
|
|
if cmd.Long != "" {
|
|
|
|
fmt.Printf("%s\n\n", cmd.Long)
|
|
|
|
}
|
|
|
|
if len(cmd.Opts) != 0 {
|
|
|
|
fmt.Printf("Options:\n\n")
|
|
|
|
|
|
|
|
ks := []string{}
|
|
|
|
for k := range cmd.Opts {
|
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
for _, k := range ks {
|
|
|
|
v := cmd.Opts[k]
|
|
|
|
fmt.Printf("- %q: %s\n", k, v)
|
|
|
|
}
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|