Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite using optparse gem #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions README
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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