I want to recursively build a json tree of directories and permissions beginning with one input directory. For example, having input "/initial_directory", I expect the following output:
{
"initial_directory": {
"permissions": "755",
"children": {
"file_a": {
"permissions": "755"
},
"directory_two": {
"permissions": "600",
"children": {
"file_b": {
"permissions": "777"
}
}
}
}
}
}
This is my code so far:
def build_json(directory)
json = {}
Dir.foreach(directory) do |file|
perm, file = (`stat -f '%A %N' #{file} `).split(' ')
json[file] = perm
json
end
end
I want to update this to recursively get all the child directories. My problem is that I need to know the json path upfront for this. Is there a better approach to achieve this?