Last active
March 20, 2018 13:10
-
-
Save erran/af0639088bb742963db6728543b82b55 to your computer and use it in GitHub Desktop.
Replace a HTML tag (<terraform-docs>) with markdown documentation via terraform-docs.
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
#!/usr/bin/env ruby | |
require 'optparse' | |
options = { output: STDOUT } | |
OptionParser.new do |opts| | |
opts.banner = 'Usage: replace.rb <source-file.md> [options]' | |
opts.on("-o", "--output=OUTPUT", "The file path to output results from (default: STDOUT)") do |v| | |
options[:output] = File.open(options[:output], 'w') | |
end | |
opts.on("-m", "--module-path=PATH", "The path to a module to generate documentation for (default: dirname of source file)") do |v| | |
options[:module_path] = v | |
end | |
opts.on("-O", "--overwrite", "Whether to overwrite the source file (default: false)") do |v| | |
options[:overwrite] = true | |
end | |
opts.on("-h", "--help", "Show this help message") do | |
puts opts | |
exit | |
end | |
end.parse! | |
unless system('which terraform-docs > /dev/null 2>&1') | |
warn 'Please install terraform-docs and ensure it is on your path.' | |
exit 1 | |
end | |
source = ARGV[0] || '-' | |
if source == '-' | |
warn 'Reading STDIN...' | |
options[:module_path] ||= '.' | |
original = STDIN.read | |
else | |
options[:module_path] ||= File.dirname(source) | |
original = File.read(source) | |
end | |
output = options[:output] | |
if options[:overwrite] && source != '-' | |
output = File.open(source, 'w') | |
end | |
replace_tag_regexp = %r{ | |
<terraform-docs/>| # Place holder tag. | |
<terraform-docs>.*</terraform-docs> # Open and closing tags including children/text. | |
}mx | |
terraform_docs_output = `terraform-docs md #{options[:module_path]}` | |
wrapped_markdown = "<terraform-docs>\n#{terraform_docs_output}\n</terraform-docs>" | |
output.puts original.gsub(replace_tag_regexp, wrapped_markdown) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment