Skip to content

Instantly share code, notes, and snippets.

@sei0o
Last active April 27, 2023 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sei0o/d48e96635da5cc9598a281b7a014cf48 to your computer and use it in GitHub Desktop.
Save sei0o/d48e96635da5cc9598a281b7a014cf48 to your computer and use it in GitHub Desktop.
Turns YAML into a tree of files and directories. Use with fzf, rg, etc.
# usage: ruby yamltodir.rb some.yaml dest_dir
require 'yaml'
require 'fileutils'
def create_files(data, path='')
if data.is_a? Hash
FileUtils.mkdir_p path
data.each do |key, value|
escaped_key = key.gsub('/', '_')
create_files(value, "#{path}/#{escaped_key}")
end
if data.size == 0
File.write "#{path}/EMPTY", ""
end
return
end
if data.is_a? Array
FileUtils.mkdir_p path
data.each_with_index do |v, i|
create_files v, "#{path}/_#{i}"
end
return
end
File.write(path, data.to_s)
end
yaml_data = YAML.load_file ARGV[0]
dest = ARGV[1] || "."
create_files yaml_data, dest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment