1818require 'optparse'
1919require 'chef'
2020require 'chef/exceptions'
21- require 'chef/logger '
21+ require 'chef/log '
2222
2323class Chef ::Application
24- attr_accessor :argv , :config , :options , :opt_parser
2524
26- def initialize ( )
25+ attr_accessor :argv , :config , :options
26+
27+ def initialize
2728 @argv = ARGV . dup
28- @config = { :config_file => "/etc/chef/default.rb" }
29- @options = nil
30- @opt_parser = nil
29+ @config = { }
30+ @options = { }
3131
3232 trap ( "INT" ) do
33- Chef . fatal! ( "SIGINT received, stopping" , 2 )
33+ Chef :: Application . fatal! ( "SIGINT received, stopping" , 2 )
3434 end
3535
3636 trap ( "HUP" ) do
@@ -40,24 +40,26 @@ def initialize()
4040
4141 at_exit do
4242 # tear down the logger and shit
43- end
44-
43+ end
4544 end
4645
46+ # Reconfigure the application. You'll want to override this.
4747 def reconfigure
4848 configure_opt_parser
4949 configure_chef
50- parse_arguments
5150 configure_logging
5251 end
5352
53+ # Kick off the application
5454 def run
5555 reconfigure
56- get_this_party_started
56+ setup_application
57+ run_application
5758 end
5859
60+ # Build an Option Parser, merge the default options and then parse the command line arguments into the config hash
5961 def configure_opt_parser
60- @opt_parser = OptionParser . new do |opts |
62+ OptionParser . new do |opts |
6163 opts . banner = "Usage: #{ $0} (options)"
6264
6365 default_options = {
@@ -79,53 +81,49 @@ def configure_opt_parser
7981 }
8082
8183 # just a step to the left
82- @options = default_options . merge ( @options ) if @options
83-
84- # setup our instance config hash
84+ @options = default_options . merge ( @options )
85+
86+ # Add the CLI options into OptionParser
8587 @options . each do |opt_key , opt_val |
8688 opts . on ( opt_val [ :short ] , opt_val [ :long ] , opt_val [ :description ] ) do |c |
89+ # Update our internal Chef::Config hash, to be merged later.
8790 @config [ opt_key ] = ( opt_val [ :proc ] && opt_val [ :proc ] . call ( c ) ) || c
8891 end
8992 end
9093
9194 opts . on_tail ( "-h" , "--help" , "Show this message" ) do
9295 puts opts
93- exit 0
96+ self . fatal! ( "Exiting" , 0 )
9497 end
95- end
98+ end . parse! ( @argv )
9699 end
97100
98101 # Parse the configuration file
99102 def configure_chef
100- Chef ::Config . from_file ( @config [ :config_file ] )
103+ Chef ::Config . from_file ( @config [ :config_file ] ) if @config [ :config_file ]
101104 Chef ::Config . configure { |c | c . merge! ( @config ) . rehash }
102105 end
103106
104107 # Initialize and configure the logger
105108 def configure_logging
106109 Chef ::Log . init ( Chef ::Config [ :log_location ] )
107- Chef ::Log . level ( Chef ::Config [ :log_level ] )
108110 end
109111
110- # Parse ARGV for arguments
111- def parse_arguments
112- raise Chef ::Exceptions ::Application , "#{ self . to_s } : option parser has not been configured" unless @opt_parser
113- @opt_parser . parse! ( @argv )
112+ class << self
113+ # Log a fatal error message and exit the application
114+ def fatal! ( msg , err = -1 )
115+ Chef ::Log . fatal ( msg )
116+ Process . exit err
117+ end
114118 end
115119
116- # Log a fatal error message and exit the process
117- def fatal! ( msg , err = -1 )
118- Chef ::Log . fatal ( msg )
119- exit err
120+ # Called prior to starting the application, by the run method
121+ def setup_application
122+ raise Chef ::Exceptions ::Application , "#{ self . to_s } : you must override setup_application"
120123 end
121124
122- private
123-
124- def get_this_party_started
125- raise Chef ::Exceptions ::Application , "#{ self . to_s } : you must override get_this_party_started"
125+ # Actually run the application
126+ def run_application
127+ raise Chef ::Exceptions ::Application , "#{ self . to_s } : you must override run_application"
126128 end
127-
128- end
129-
130-
131- Chef ::Application . new . run
129+ end
0 commit comments