2022-08-28 13:21:57 +02:00
|
|
|
// Package rcd provides the rcd command.
|
2018-10-27 17:02:45 +02:00
|
|
|
package rcd
|
|
|
|
|
|
|
|
import (
|
2020-11-05 16:18:51 +01:00
|
|
|
"context"
|
2018-10-27 17:02:45 +02:00
|
|
|
"log"
|
2021-03-02 11:09:33 +01:00
|
|
|
"sync"
|
2018-10-27 17:02:45 +02:00
|
|
|
|
2021-03-02 11:09:33 +01:00
|
|
|
sysdnotify "github.com/iguanesolutions/go-systemd/v5/notify"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs/rc/rcflags"
|
|
|
|
"github.com/rclone/rclone/fs/rc/rcserver"
|
2021-03-02 11:09:33 +01:00
|
|
|
"github.com/rclone/rclone/lib/atexit"
|
2022-12-11 15:47:47 +01:00
|
|
|
libhttp "github.com/rclone/rclone/lib/http"
|
2018-10-27 17:02:45 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-08-04 13:32:37 +02:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2018-10-27 17:02:45 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 13:32:37 +02:00
|
|
|
var commandDefinition = &cobra.Command{
|
2018-10-27 17:02:45 +02:00
|
|
|
Use: "rcd <path to files to serve>*",
|
|
|
|
Short: `Run rclone listening to remote control commands only.`,
|
|
|
|
Long: `
|
2019-01-29 15:12:26 +01:00
|
|
|
This runs rclone so that it only listens to remote control commands.
|
2018-10-27 17:02:45 +02:00
|
|
|
|
|
|
|
This is useful if you are controlling rclone via the rc API.
|
|
|
|
|
|
|
|
If you pass in a path to a directory, rclone will serve that directory
|
|
|
|
for GET requests on the URL passed in. It will also open the URL in
|
|
|
|
the browser when rclone is run.
|
2018-10-28 15:31:24 +01:00
|
|
|
|
|
|
|
See the [rc documentation](/rc/) for more info on the rc flags.
|
2022-12-11 15:47:47 +01:00
|
|
|
` + libhttp.Help + libhttp.TemplateHelp + libhttp.AuthHelp,
|
2022-11-26 23:40:49 +01:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.45",
|
|
|
|
},
|
2018-10-27 17:02:45 +02:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(0, 1, command, args)
|
|
|
|
if rcflags.Opt.Enabled {
|
|
|
|
log.Fatalf("Don't supply --rc flag when using rcd")
|
|
|
|
}
|
2019-08-04 13:32:37 +02:00
|
|
|
|
2018-10-27 17:02:45 +02:00
|
|
|
// Start the rc
|
|
|
|
rcflags.Opt.Enabled = true
|
|
|
|
if len(args) > 0 {
|
|
|
|
rcflags.Opt.Files = args[0]
|
|
|
|
}
|
2019-08-04 13:32:37 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
s, err := rcserver.Start(context.Background(), &rcflags.Opt)
|
2018-11-01 18:20:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to start remote control: %v", err)
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
log.Fatal("rc server not configured")
|
|
|
|
}
|
2019-08-04 13:32:37 +02:00
|
|
|
|
2021-03-02 11:09:33 +01:00
|
|
|
// Notify stopping on exit
|
|
|
|
var finaliseOnce sync.Once
|
|
|
|
finalise := func() {
|
|
|
|
finaliseOnce.Do(func() {
|
|
|
|
_ = sysdnotify.Stopping()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fnHandle := atexit.Register(finalise)
|
|
|
|
defer atexit.Unregister(fnHandle)
|
|
|
|
|
|
|
|
// Notify ready to systemd
|
|
|
|
if err := sysdnotify.Ready(); err != nil {
|
|
|
|
log.Fatalf("failed to notify ready to systemd: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:20:04 +01:00
|
|
|
s.Wait()
|
2021-03-02 11:09:33 +01:00
|
|
|
finalise()
|
2018-10-27 17:02:45 +02:00
|
|
|
},
|
|
|
|
}
|