Class: PutCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/davclient/dav-put.rb

Class Method Summary collapse

Class Method Details

.put(args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/davclient/dav-put.rb', line 9

def self.put(args)

  options = read_options(args)
  url = args[0]
  if(options[:string])then

    # Put string
    if(!url.match(/^http.*\/\/([^\/]*)/) and !WebDAV.CWURL)
      raise "Error: No current working url set. Use '#{$0} cd url' to set url."
    end

    begin
      WebDAV.put_string(url,options[:string])
    rescue
      puts $0 + ": " + $!
    end
    puts "Published content to: " + url
  else

    # Put files(s)

    #  puts "DEBUG: size:" + args.size.to_s

    if(args.size == 1 )
      local_file = args[0]
      if(not(File.exists?(local_file)))
        raise "File not found: #{local_file}"
      end
      if(!WebDAV.CWURL)
        raise "Error: No current working url set. Use '#{$0} cd url' to set url."
      end

      WebDAV.put(WebDAV.CWURL, local_file)

    elsif(args.size == 2 and args[1].match(/^http.*\/\/([^\/]*)/) )
      local_file = args[0]
      url = args[1]

      if(not(File.exists?(local_file)))
        raise "File not found: #{local_file}"
      end

      if(WebDAV.isCollection?(url))
        url += File.basename(local_file)
      end

      WebDAV.put(url, local_file)

    else

      # Put more than one file

      if(args.last.match(/^http.*\/\/([^\/]*)/) )
         url = args.last
         if(!WebDAV.isCollection?(url))
           raise "Destination collection not found: " + url
         end
         args = args[0..(args.size-2)]
      else
        url = WebDAV.CWURL
      end

      count = 0
      args.each do | arg|
        # puts "arg:" + arg
        if(File.ftype(arg) == 'directory')
          raise "Upload directories not implemented"
        end
        if(File.exists?(arg))
          basename = File.basename(arg)
          WebDAV.put(url + basename, arg)
          count = count + 1
        else
          raise "Error: File not found " + arg
        end
      end
      puts "Published #{count} files to #{url}"
    end
  end

end