Last active
April 27, 2023 02:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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