Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Rewrite using optparse gem #1

Open
wants to merge 1 commit into from

1 participant

@achied
  • More clean command line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 19, 2011
  1. @achied

    Rewrite using optparse

    achied authored
This page is out of date. Refresh to see the latest.
Showing with 53 additions and 33 deletions.
  1. +2 −9 README
  2. +51 −24 lib/nginx_config_generator.rb
View
11 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
View
75 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
+
Something went wrong with that request. Please try again.