github.com/divyam234/rclone@v1.64.1/cmd/rcd/rcd.go (about)

     1  // Package rcd provides the rcd command.
     2  package rcd
     3  
     4  import (
     5  	"context"
     6  	"log"
     7  	"sync"
     8  
     9  	sysdnotify "github.com/iguanesolutions/go-systemd/v5/notify"
    10  	"github.com/divyam234/rclone/cmd"
    11  	"github.com/divyam234/rclone/fs/rc/rcflags"
    12  	"github.com/divyam234/rclone/fs/rc/rcserver"
    13  	"github.com/divyam234/rclone/lib/atexit"
    14  	libhttp "github.com/divyam234/rclone/lib/http"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  func init() {
    19  	cmd.Root.AddCommand(commandDefinition)
    20  }
    21  
    22  var commandDefinition = &cobra.Command{
    23  	Use:   "rcd <path to files to serve>*",
    24  	Short: `Run rclone listening to remote control commands only.`,
    25  	Long: `
    26  This runs rclone so that it only listens to remote control commands.
    27  
    28  This is useful if you are controlling rclone via the rc API.
    29  
    30  If you pass in a path to a directory, rclone will serve that directory
    31  for GET requests on the URL passed in.  It will also open the URL in
    32  the browser when rclone is run.
    33  
    34  See the [rc documentation](/rc/) for more info on the rc flags.
    35  ` + libhttp.Help(rcflags.FlagPrefix) + libhttp.TemplateHelp(rcflags.FlagPrefix) + libhttp.AuthHelp(rcflags.FlagPrefix),
    36  	Annotations: map[string]string{
    37  		"versionIntroduced": "v1.45",
    38  		"groups":            "RC",
    39  	},
    40  	Run: func(command *cobra.Command, args []string) {
    41  		cmd.CheckArgs(0, 1, command, args)
    42  		if rcflags.Opt.Enabled {
    43  			log.Fatalf("Don't supply --rc flag when using rcd")
    44  		}
    45  
    46  		// Start the rc
    47  		rcflags.Opt.Enabled = true
    48  		if len(args) > 0 {
    49  			rcflags.Opt.Files = args[0]
    50  		}
    51  
    52  		s, err := rcserver.Start(context.Background(), &rcflags.Opt)
    53  		if err != nil {
    54  			log.Fatalf("Failed to start remote control: %v", err)
    55  		}
    56  		if s == nil {
    57  			log.Fatal("rc server not configured")
    58  		}
    59  
    60  		// Notify stopping on exit
    61  		var finaliseOnce sync.Once
    62  		finalise := func() {
    63  			finaliseOnce.Do(func() {
    64  				_ = sysdnotify.Stopping()
    65  			})
    66  		}
    67  		fnHandle := atexit.Register(finalise)
    68  		defer atexit.Unregister(fnHandle)
    69  
    70  		// Notify ready to systemd
    71  		if err := sysdnotify.Ready(); err != nil {
    72  			log.Fatalf("failed to notify ready to systemd: %v", err)
    73  		}
    74  
    75  		s.Wait()
    76  		finalise()
    77  	},
    78  }