#!/usr/bin/env ruby require 'uri' require 'rubygems' require 'net/dav' dav_user = ENV['DAVUSER'] dav_pass = ENV['DAVPASS'] disable_basic_auth = (ENV['DISABLE_BASIC_AUTH'] && ENV['DISABLE_BASIC_AUTH'] != "0") enable_curl = !(ENV['DISABLE_CURL'] && ENV['DISABLE_CURL'] != "0") cmd = $*[0] def print_usage puts "usage: #{$0} COMMAND [ARGS]" puts "" puts "Available commands:" puts " ls List file or directory at URL" puts " lsr List file or directory recursively at URL" puts " get Get file from URL to stdout or to FILE" puts " put Put file from FILE to URL" puts " mkdir Create directory at URL" puts " gsub Replace content at URL from REGEXP to VALUE" exit end if $*.size < 2 print_usage end case cmd when 'put' url = URI.parse $*[2] file = $*[1] when 'get' url = URI.parse $*[1] file = $*[2] else url = URI.parse $*[1] end dav = Net::DAV.new(url, :curl => enable_curl) dav.disable_basic_auth = disable_basic_auth if (url.scheme == 'https') dav.verify_callback = lambda do |preverify_ok, x509_store| puts "preverify = #{preverify_ok}" if $DEBUG puts "cert = #{x509_store.current_cert.inspect}" if $DEBUG preverify_ok end dav.verify_server = false end res = dav.start { |dav| dav.credentials(dav_user, dav_pass) if dav_user case cmd when 'put' File.open(file, "r") do |stream| dav.put(url.path, stream, File.size(file)) end when 'get' if file.nil? dav.get(url.path) do |str| $stdout.print str end else File.open(file, "w") do |stream| dav.get(url.path) do |str| stream.print str end end end when 'lsr' dav.find(url.path, :recursive => true) do |item| puts "#{item.size}\t#{item.uri}" end when 'ls' dav.find(url.path) do |item| puts "#{item.size}\t#{item.uri}" end when 'mkdir' dav.mkdir(url.path) when 'gsub' re = Regexp.compile($*[2]) val = $*[3] dav.find(url.path) do |item| if (item.type == :file) item.content = item.content.gsub(re, val) end end else print_usage end }