-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirjson
47 lines (34 loc) · 834 Bytes
/
dirjson
1
2
3
4
5
6
7
8
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
#!/usr/bin/env ruby
class File
class Stat
def to_hash(*attrs)
h = {}
attrs.each do |attr|
h[attr] = self.send(attr)
end
h
end
end
end
class Dir
def self.vglob(path)
filelist = []
Dir.foreach(path) do |f|
next if f == '.' or f == '..'
filepath = File.join(path, f)
filestat = File.stat(filepath)
h = { name: f }.merge(filestat.to_hash(:atime, :ctime, :mtime))
if File.directory? filepath
list = vglob(filepath)
dsize = list.map{ |e| e[:size] }.reduce(:+)
filelist << h.merge({type: :d, size: dsize, contains: list})
else
filelist << h.merge({type: :f, size: filestat.size})
end
end
filelist
end
end
require 'json'
path = ARGV.shift
puts Dir.vglob(path).to_json