github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/cmd/test/changenotify/changenotify.go (about)

     1  // Package changenotify tests rclone's changenotify support
     2  package changenotify
     3  
     4  import (
     5  	"context"
     6  	"errors"
     7  	"time"
     8  
     9  	"github.com/rclone/rclone/cmd"
    10  	"github.com/rclone/rclone/cmd/test"
    11  	"github.com/rclone/rclone/fs"
    12  	"github.com/rclone/rclone/fs/config/flags"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var (
    17  	pollInterval = 10 * time.Second
    18  )
    19  
    20  func init() {
    21  	test.Command.AddCommand(commandDefinition)
    22  	cmdFlags := commandDefinition.Flags()
    23  	flags.DurationVarP(cmdFlags, &pollInterval, "poll-interval", "", pollInterval, "Time to wait between polling for changes", "")
    24  }
    25  
    26  var commandDefinition = &cobra.Command{
    27  	Use:   "changenotify remote:",
    28  	Short: `Log any change notify requests for the remote passed in.`,
    29  	Annotations: map[string]string{
    30  		"versionIntroduced": "v1.56",
    31  	},
    32  	RunE: func(command *cobra.Command, args []string) error {
    33  		cmd.CheckArgs(1, 1, command, args)
    34  		f := cmd.NewFsSrc(args)
    35  		ctx := context.Background()
    36  
    37  		// Start polling function
    38  		features := f.Features()
    39  		if do := features.ChangeNotify; do != nil {
    40  			pollChan := make(chan time.Duration)
    41  			do(ctx, changeNotify, pollChan)
    42  			pollChan <- pollInterval
    43  			fs.Logf(nil, "Waiting for changes, polling every %v", pollInterval)
    44  		} else {
    45  			return errors.New("poll-interval is not supported by this remote")
    46  		}
    47  		select {}
    48  	},
    49  }
    50  
    51  // changeNotify invalidates the directory cache for the relativePath
    52  // passed in.
    53  //
    54  // if entryType is a directory it invalidates the parent of the directory too.
    55  func changeNotify(relativePath string, entryType fs.EntryType) {
    56  	fs.Logf(nil, "%q: %v", relativePath, entryType)
    57  }