Class: DavCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/davclient/davcli.rb

Overview

DavClient Command Line Utility

Class Method Summary collapse

Class Method Details

.cat(args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/davclient/davcli.rb', line 64

def self.cat(args)
  if(show_help?(args))
    puts "Usage: #{$0} cat [url|filename]"
    puts
    puts "Concatenate and print remote files to local terminal."
  end
  if(args.size == 1)
    url = args[0]
    puts WebDAV.get(url)
  else
    puts "Illegal arguments: " + args[1..100].join(" ")
    puts "Usage: #{$0} cat [url|filename]"
  end
end

.cd(args) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/davclient/davcli.rb', line 101

def self.cd(args)
  if(show_help?(args))
    puts "Usage: #{$0} cd [url|remote-path]"
    puts
    puts "Change current working working url."
    puts
    puts "Examples: "
    puts "     #{$0} cd https://www.webdav.org/"
    puts "     #{$0} cd ../test"
    puts
    exit
  end
  url = args[0]
  if(url == nil)then
    puts "#{$0} cd: Missing mandatory url."
    exit
  end
  begin
    WebDAV.cd(url)
    puts "Changing WebDAV URL to: " + WebDAV.CWURL
  rescue Exception => exception
    puts exception
  end
end

.cp(args) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/davclient/davcli.rb', line 179

def self.cp(args)
  if(args.size == 2 )
    WebDAV.cp(args[0], args[1])
  else
    puts "Usage '#{$0} cp src dest"
    puts
    puts "Copy resources on remote server."
  end
end

.dav(args) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/davclient/davcli.rb', line 223

def self.dav(args)

  $0 = $0.sub(/.*\//,"").sub(/.rb$/,"")

  command =  args[0]

  if(args.size == 0 or command == "-h" or command =~ /help/ or command =~ /\?/) then
    print_dav_usage
  end

  if(command == "-v" or command =~ /version/ ) then
    puts "#{$0} version " + WebDAV.version
    exit
  end

  args = args[1..100]
  case command
    when "put" then
      PutCLI.put(args)
    when "get" then
      get(args)
    when "edit" then
      edit(args)
    when "cat" then
      cat(args)
    when "ls" then
      LsCLI.ls(args)
    when "pwd"
      pwd(args)
    when "cd"
      cd(args)
    when "cp"
      cp(args)
    when "copy"
      cp(args)
    when "mv"
      mv(args)
    when "move"
      mv(args)
    when "mkcol"
       mkcol(args)
    when "mkdir"
       mkcol(args)
    when "put"
       PutCLI.put(args)
    when "delete"
      delete(args)
    when "del"
      delete(args)
    when "rm"
      delete(args)
    when "propfind"
      propfind(args)
    when "props"
      propfind(args)
    when "options"
      options(args)
    else
      puts "Unknown command :'" + command + "'"
      print_dav_usage
    end
end

.delete(args) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/davclient/davcli.rb', line 151

def self.delete(args)
  if(show_help?(args) or args.size != 1)
    puts "Usage: #{$0} delete [url|path]"
    puts
    puts "Delete remote collection (folder) or file."
  else
    url = WebDAV.delete(args[0])
  end
end

.edit(args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/davclient/davcli.rb', line 17

def self.edit(args)
  if(show_help?(args))
    print_edit_usage
  else
    if(args.size == 1)
      url = args[0]
      content = WebDAV.get(url)
      tmp_filename = WebDAV.tmp_folder + File.basename(url)
      File.open(tmp_filename, 'w') {|f| f.write(content) }
      system("emacs --quick -nw " + tmp_filename)
      new_content = nil
      File.open(tmp_filename, 'r') {|f| new_content = f.read() }
      WebDAV.put_string(url, new_content)
    else
      puts "Illegal arguments: " + args[1..100].join(" ")
      print_edit_usage
    end
  end
end

.get(args) ⇒ Object

TODO

- Handle glob (ie *.gif) => Tell user to quote to avoid shell glob: dav get "*.html"


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/davclient/davcli.rb', line 39

def self.get(args)

  puts "DEBUG:"
  if(args.size == 1 or args.size == 2 )
    url = args[0]
    content =  WebDAV.get(url)
    filename = ""
    if(args.size == 1)
      filename = File.basename(url)
    else
      # Handle relative paths in local filenames or local dir
      path = Pathname.new(Pathname.pwd)
      path = path + args[1]
      filename = path.to_s
      if(args[1] =~ /\/$/ or args[1] =~ /\.$/)
        path = path + filename = filename + "/" + File.basename(url)
      end
    end
    File.open(filename, 'w') {|f| f.write(content) }
  else
    puts "Illegal arguments: " + args[1..100].join(" ")
    puts "#{$0}: usage '#{$0} get remote-url [local]"
  end
end

.ls(args) ⇒ Object



175
176
177
# File 'lib/davclient/davcli.rb', line 175

def self.ls(args)
  LsCLI.ls(args)
end

.mkcol(args) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/davclient/davcli.rb', line 126

def self.mkcol(args)
  if(show_help?(args))
    puts "Usage: #{$0} mkcol [url|remote-path]"
    puts
    puts "Create collection (folder) on remote server."
    puts "The command 'mkdir' is an alias for 'mkcol'."
    puts
    puts "Examples: "
    puts "     #{$0} mkcol new_collection"
    puts "     #{$0} mkcol https://www.webdav.org/new_collection/"
    puts "     #{$0} mkcol ../new_collection"
    puts
    exit
  end
  if(args.size == 1 )
    if( args[0] =~ /^http/ || WebDAV.CWURL )
      WebDAV.mkcol(args[0])
    else
      puts "Error: #{$0} mkcol: No working url set. Use '#{$0} cd url' to set url."
    end
  else
    puts "#{$0}: usage '#{$0} mkcol [url|path]"
  end
end

.mv(args) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/davclient/davcli.rb', line 190

def self.mv(args)
  if(args.size == 2 )
    WebDAV.mv(args[0], args[1])
  else
    puts "Usage '#{$0} copy mv dest"
    puts
    puts "Move resources on remote server."
  end
end

.options(args) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/davclient/davcli.rb', line 161

def self.options(args)
  if((args.size == 0 or args.size == 1) and !show_help?(args))
    puts WebDAV.options(args[0])
  else
    puts "Usage: #{$0} options [url]"
    puts
    puts "Prints remote server options and http headers. "
  end
end


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/davclient/davcli.rb', line 200

def self.print_dav_usage
  puts "usage: #{$0} COMMAND [ARGS]"
  puts ""
  puts "Available #{$0} commands:"
  puts "   ls        List files on webdav server"
  puts "   pwd       Print current working url"
  puts "   cd        Change current working url"
  puts "   cp        Copy resource"
  puts "   mv        Move resource"
  puts "   rm        Remove resource"
  puts "   cat       Print content of resource"
  puts "   mkdir     Create remote collection (directory) (mkcol alias)"
  puts "   get       Download resource"
  puts "   put       Upload local file"
  puts "   propfind  Print webdav properties for url"
  puts "   mkcol     Make collection"
  puts "   options   Display webservers WebDAV options"
  puts "   edit      Edit contents of remote file with editor"
  puts ""
  puts "See '#{$0} COMMAND -h' for more information on a specific command."
  exit
end


9
10
11
12
13
14
15
# File 'lib/davclient/davcli.rb', line 9

def self.print_edit_usage
  puts "Usage: #{$0} edit [url|resource-name]"
  puts
  puts "Edit remote file in editor. File is transfered back to "
  puts "server after execution of editor is ended. "
  puts
end

.propfind(args) ⇒ Object



171
172
173
# File 'lib/davclient/davcli.rb', line 171

def self.propfind(args)
  PropfindCLI.propfind(args)
end

.pwd(args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/davclient/davcli.rb', line 85

def self.pwd(args)
  if(show_help?(args))
    puts "Usage: #{$0} pwd"
    puts ""
    puts "Print current working url."
    exit
  end
  cwurl = WebDAV.CWURL
  if(cwurl)
    puts cwurl
  else
    puts "#{$0}: No working url set. Use 'dav cd url' to set url"
  end

end

.show_help?(args) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/davclient/davcli.rb', line 79

def self.show_help?(args)
  return (args.grep("-?").size > 0 or
          args.grep("-h").size > 0 or
          args.grep("--help").size > 0 )
end