rcd.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Package rcd provides the rcd command.
  2. package rcd
  3. import (
  4. "context"
  5. "log"
  6. "github.com/rclone/rclone/cmd"
  7. "github.com/rclone/rclone/fs/rc/rcflags"
  8. "github.com/rclone/rclone/fs/rc/rcserver"
  9. libhttp "github.com/rclone/rclone/lib/http"
  10. "github.com/rclone/rclone/lib/systemd"
  11. "github.com/spf13/cobra"
  12. )
  13. func init() {
  14. cmd.Root.AddCommand(commandDefinition)
  15. }
  16. var commandDefinition = &cobra.Command{
  17. Use: "rcd <path to files to serve>*",
  18. Short: `Run rclone listening to remote control commands only.`,
  19. Long: `This runs rclone so that it only listens to remote control commands.
  20. This is useful if you are controlling rclone via the rc API.
  21. If you pass in a path to a directory, rclone will serve that directory
  22. for GET requests on the URL passed in. It will also open the URL in
  23. the browser when rclone is run.
  24. See the [rc documentation](/rc/) for more info on the rc flags.
  25. ` + libhttp.Help(rcflags.FlagPrefix) + libhttp.TemplateHelp(rcflags.FlagPrefix) + libhttp.AuthHelp(rcflags.FlagPrefix),
  26. Annotations: map[string]string{
  27. "versionIntroduced": "v1.45",
  28. "groups": "RC",
  29. },
  30. Run: func(command *cobra.Command, args []string) {
  31. cmd.CheckArgs(0, 1, command, args)
  32. if rcflags.Opt.Enabled {
  33. log.Fatalf("Don't supply --rc flag when using rcd")
  34. }
  35. // Start the rc
  36. rcflags.Opt.Enabled = true
  37. if len(args) > 0 {
  38. rcflags.Opt.Files = args[0]
  39. }
  40. s, err := rcserver.Start(context.Background(), &rcflags.Opt)
  41. if err != nil {
  42. log.Fatalf("Failed to start remote control: %v", err)
  43. }
  44. if s == nil {
  45. log.Fatal("rc server not configured")
  46. }
  47. // Notify stopping on exit
  48. defer systemd.Notify()()
  49. s.Wait()
  50. },
  51. }