diff --git a/README.md b/README.md index 12bb64f..fe2a6a0 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ The constructor accepts the following named arguments: | `:subdirs_only` | Optional | `Boolean` | An option to have `rsync` only sync files that are in subfolders of `:src_dir`. Defaults to `false`. | | `:log_dir` | Optional | `String` | `rsync_wrapper` has `rsync` pipe its results into a logfile so that the ruby code can then parse this file (and then deletes it). This option should be set to the absolute path of the directory where the temporary logfile will be stored. | | `:logfile` | Optional | `String` | If the `:log_dir` is not specified, you can provide an explicit path for `rsync_wrapper`'s temporary logfile. | +| `:bwlimit`|Optional|`Integer`|From rsync's man pages: This option allows you to specify a maximum transfer rate in kilobytes per second. This option is most effective when using rsync with large files (several megabytes and up). Due to the nature of rsync transfers, blocks of data are sent, then if rsync determines the transfer was too fast, it will wait before sending the next data block. The result is an average transfer rate equaling the specified limit. A value of zero specifies no limit.| To execute `rsync`, invoke the `sync!` method which accepts a block with 2 parameters: @@ -50,8 +51,8 @@ To execute `rsync`, invoke the `sync!` method which accepts a block with 2 param Example: ```ruby -source_directory = '/Users/seanhuber/Documents/my_pdfs' -destination_directory = '/Users/seanhuber/Documents/my_pdfs (copied)' +source_directory = '/path/of/directory/you/want/to/sync' +destination_directory = '/path/of/directory/to/sync/to' rsync = Rsync.new(src_dir: source_directory, dest_dir: destination_directory) diff --git a/lib/rsync_wrapper/rsync.rb b/lib/rsync_wrapper/rsync.rb index 068e281..6b0fe48 100644 --- a/lib/rsync_wrapper/rsync.rb +++ b/lib/rsync_wrapper/rsync.rb @@ -18,6 +18,11 @@ def initialize **opts else File.join(Dir.pwd, "rsync-#{SecureRandom.uuid}.log") end + + @optional_arguments = [ + :bwlimit, + # TODO: add rsync's other arguments that wouldn't interfere with the design of this gem https://linux.die.net/man/1/rsync + ].map{|arg| opts[arg] ? [arg, opts[arg]] : nil}.compact.to_h end def sync! &block @@ -31,6 +36,9 @@ def exec_rsync rsync_opts = ['-ri', "--log-file '#{@logfile}'", '--size-only', '--prune-empty-dirs'] rsync_opts += @inclusions.map{|inc| "--include '#{inc}'"} rsync_opts += @exclusions.map{|exc| "--exclude '#{exc}'"} + + rsync_opts += @optional_arguments.map{|k,v| "--#{k}=#{v}"} + cmd = "rsync #{rsync_opts.join(' ')} \"#{@dirs[:src_dir]}\" \"#{@dirs[:dest_dir]}\" > /dev/null 2>&1" `#{cmd}` end diff --git a/lib/rsync_wrapper/version.rb b/lib/rsync_wrapper/version.rb index be3436a..fb2fc2c 100644 --- a/lib/rsync_wrapper/version.rb +++ b/lib/rsync_wrapper/version.rb @@ -1,3 +1,3 @@ module RsyncWrapper - VERSION = '1.0.1' + VERSION = '1.1.0' end diff --git a/spec/rsync_spec.rb b/spec/rsync_spec.rb index 922c2a6..3c12eb0 100644 --- a/spec/rsync_spec.rb +++ b/spec/rsync_spec.rb @@ -65,4 +65,19 @@ FileUtils.rm_rf dest_dir end + + it 'accepts a bwlimit optional argument' do + rsync = Rsync.new( + src_dir: File.join(FileUtils.pwd, 'spec', 'test_src_dir'), + dest_dir: File.join(FileUtils.pwd, 'spec', 'test_dest_dir'), + include_extensions: [:doc, :docx, :pdf], + subdirs_only: true, + logfile: File.join(FileUtils.pwd, 'spec', 'dummy_rsync_output.log'), + bwlimit: 10000 + ) + expect(rsync).to receive(:`).with("rsync -ri --log-file '#{FileUtils.pwd}/spec/dummy_rsync_output.log' --size-only --prune-empty-dirs --include '*.doc' --include '*.docx' --include '*.pdf' --include '*/' --exclude '*' --bwlimit=10000 \"#{FileUtils.pwd}/spec/test_src_dir\" \"#{FileUtils.pwd}/spec/test_dest_dir\" > /dev/null 2>&1") + + expect(rsync).to receive(:parse_logfile).and_return(nil) + rsync.sync! + end end