From 5a9edbf2ccbdc98ab8f81909f6cf45de575b1a57 Mon Sep 17 00:00:00 2001 From: Eduardo Aceituno Date: Fri, 19 Aug 2011 13:59:07 +0200 Subject: [PATCH] Rewrite using optparse --- README | 11 +---- lib/nginx_config_generator.rb | 75 ++++++++++++++++++++++++----------- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/README b/README index 0b179be..99d0803 100644 --- a/README +++ b/README @@ -4,17 +4,10 @@ Check it: http://errtheblog.com/post/3908 Use it: - $ generate_nginx_config nginx_config.yml /etc/nginx.conf - -See an example config file: - $ generate_nginx_config --example - -You can set two environment variables: - - NGINX_CONFIG_YAML to specify the YAML config file - - NGINX_CONFIG_FILE to specify the nginx config file + $ generate_nginx_config -c nginx_config.yml -t nginx.erb -o /etc/nginx.conf --overwrite By default, generate_nginx_config won't overwrite your OUT file. To rock this behavior, -pass in --overwrite or -o or -y or --force or -f. Whichever. +pass in --overwrite. Whichever. >> Chris Wanstrath => chris[at]ozmm[dot]org diff --git a/lib/nginx_config_generator.rb b/lib/nginx_config_generator.rb index 737081f..222fc80 100644 --- a/lib/nginx_config_generator.rb +++ b/lib/nginx_config_generator.rb @@ -1,35 +1,62 @@ #! /usr/bin/env ruby -%w(erb yaml).each &method(:require) +require 'erb' +require 'yaml' +require 'optparse' -def error(message) puts(message) || exit end -def file(file) "#{File.dirname(__FILE__)}/#{file}" end -if ARGV.include? '--example' - example = file:'config.yml.example' - error open(example).read -end +options = {} -env_in = ENV['NGINX_CONFIG_YAML'] -env_out = ENV['NGINX_CONFIG_FILE'] +opts_parser = OptionParser.new do |opts| + opts.banner="Usage: generate_nginx_config [options]" -error "Usage: generate_nginx_config [config file] [out file]" if ARGV.empty? && !env_in + opts.on('-c','--config config_file','Usage config_file file') do |config_file| + options[:config_file] = config_file + end -overwrite = %w(-y -o -f --force --overwrite).any? { |f| ARGV.delete(f) } + opts.on('-t','--template template_file','Usage template_file file') do |template_file| + options[:template_file] = template_file + end -config = YAML.load(ERB.new(File.read(env_in || ARGV.shift || 'config.yml')).result) -template = if custom_template_index = (ARGV.index('--template') || ARGV.index('-t')) - custom = ARGV[custom_template_index+1] - error "=> Specified template file #{custom} does not exist." unless File.exist?(custom) - ARGV.delete_at(custom_template_index) # delete the --argument - ARGV.delete_at(custom_template_index) # and its value - custom -else - file:'nginx.erb' + opts.on('-o','--output output_file','Write configuration to output_file') do |output_file| + options[:output_file] = output_file + end + + options[:overwrite] = nil + opts.on('--overwrite','Overwrite output file configuration') do |overwrite| + options[:overwrite] = true + end + + opts.on('-h', '--help','Display this screen') do + puts opts + exit + end +end + +begin + opts_parser.parse! + mandatory = [:config_file,:template_file] + missing = mandatory.select{ |param| options[param].nil? } + if not missing.empty? + puts "Missing options: #{missing.join(', ')}" + puts opts_parser + exit + end + +rescue OptionParser::InvalidOption, OptionParser::MissingArgument + puts $!.to_s + puts opts_parser + exit end -if File.exists?(out_file = env_out || ARGV.shift || 'nginx.conf') && !overwrite - error "=> #{out_file} already exists, won't overwrite it. Quitting." +puts "Performing task with options: #{options.inspect}" + +config = YAML.load(File.read(options[:config_file])) + +if File.exists?(options[:output_file] || 'nginx.conf') && !options[:overwrite] + puts "=> #{options[:output_file] || 'nginx.conf'} already exists, won't overwrite it. Quitting." + exit else - open(out_file, 'w+').write(ERB.new(File.read(template), nil, '>').result(binding)) - error "=> Wrote #{out_file} successfully." + open('nginx.conf', 'w+').write(ERB.new(File.new(options[:template_file]).read,nil,'>').result(binding)) + puts "=> Wrote #{options[:output_file] || 'nginx.conf'} successfully." end +