Skip to content

Commit

Permalink
Rewrite using optparse
Browse files Browse the repository at this point in the history
  • Loading branch information
achied committed Aug 19, 2011
1 parent 183d858 commit 5a9edbf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
11 changes: 2 additions & 9 deletions README
Expand Up @@ -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
75 changes: 51 additions & 24 deletions 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

0 comments on commit 5a9edbf

Please sign in to comment.