Skip to content

Instantly share code, notes, and snippets.

@lkuty
Created August 3, 2023 13:08
Show Gist options
  • Save lkuty/9a40bc08e6263adc855ff6084f9f38ea to your computer and use it in GitHub Desktop.
Save lkuty/9a40bc08e6263adc855ff6084f9f38ea to your computer and use it in GitHub Desktop.
Ruby version of sync_loop_unix.sh from imapsync to handle double quotes in $extra
#!/home/xxx/.rbenv/versions/3.2.0/bin/ruby
require 'open3'
def ms2str(ms)
return nil if ms.nil?
x, ms = ms.divmod(1000)
x, s = x.divmod(60)
x, m = x.divmod(60)
d, h = x.divmod(24)
s = [d,h,m,s,ms.to_i]
.zip(["d", "h", "m", "s", "ms"])
.filter { |duration, unit| duration != 0 }
.map { |duration, unit| "#{duration} #{unit}" }
.join(" ")
if s.empty? then "< 1 ms" else s end
end
line_cnt = 0
IO.foreach("/home/xxx/bin/file.txt") do |line|
line_cnt += 1
next if line.match(/^\s*#|^\s*$/)
a = line.chomp.split(";")
h1, u1, p1, h2, u2, p2, extra = a
# puts "==== Starting imapsync with --host1 #{h1} --user1 #{u1} --host2 #{h2} --user2 #{u2} #{extra} ===="
cmd = "imapsync --host1 \"#{h1}\" --user1 \"#{u1}\" --password1 \"#{p1}\" --host2 \"#{h2}\" --user2 \"#{u2}\" --password2 \"#{p2}\" #{extra}"
t = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
o, e, s = Open3.capture3(cmd)
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) - t
if s.to_i == 0
# puts "success sync for line #{line_cnt}"
# puts "ok for #{u1} in #{ms2str(elapsed)}"
# STDOUT.puts o
nil
else
# STDERR.puts "failure for #{h1};#{u1} -> #{h2};#{u2} with #{extra} "
STDERR.puts "ko! for #{u1} in #{ms2str(elapsed)}"
STDERR.puts o
end
# puts "==== Ended imapsync in #{ms2str(elapsed)} with --host1 #{h1} --user1 #{u1} --host2 #{h2} --user2 #{u2} #{extra} ===="
# puts "\n\n\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment