summaryrefslogtreecommitdiffstats
path: root/lib/puppet/application/interface_base.rb
diff options
context:
space:
mode:
authorLuke Kanies <luke@puppetlabs.com>2011-02-22 09:25:21 -0800
committerLuke Kanies <luke@puppetlabs.com>2011-02-22 09:25:21 -0800
commit0cbdbce0f518d43f0d0160a58dd5ec7253a5af87 (patch)
treefefe53a0f149b0ebbec6154f2469fc7a48591684 /lib/puppet/application/interface_base.rb
parentef289e5b036d7e9716d611af09d4414de42e31ed (diff)
downloadpuppet-0cbdbce0f518d43f0d0160a58dd5ec7253a5af87.tar.gz
puppet-0cbdbce0f518d43f0d0160a58dd5ec7253a5af87.tar.xz
puppet-0cbdbce0f518d43f0d0160a58dd5ec7253a5af87.zip
Renaming 'data_baseclass' to 'interface_base'
Signed-off-by: Luke Kanies <luke@puppetlabs.com>
Diffstat (limited to 'lib/puppet/application/interface_base.rb')
-rw-r--r--lib/puppet/application/interface_base.rb90
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/puppet/application/interface_base.rb b/lib/puppet/application/interface_base.rb
new file mode 100644
index 000000000..1dd1f76c2
--- /dev/null
+++ b/lib/puppet/application/interface_base.rb
@@ -0,0 +1,90 @@
+require 'puppet/application'
+require 'puppet/interface'
+
+class Puppet::Application::InterfaceBase < Puppet::Application
+ should_parse_config
+ run_mode :agent
+
+ def preinit
+ super
+ trap(:INT) do
+ $stderr.puts "Cancelling Interface"
+ exit(0)
+ end
+ end
+
+ option("--debug", "-d") do |arg|
+ Puppet::Util::Log.level = :debug
+ end
+
+ option("--verbose", "-v") do
+ Puppet::Util::Log.level = :info
+ end
+
+ option("--from TERMINUS", "-f") do |arg|
+ @from = arg
+ end
+
+ option("--format FORMAT") do |arg|
+ @format = arg.to_sym
+ end
+
+ # XXX this doesn't work, I think
+ option("--list") do
+ indirections.each do |ind|
+ begin
+ classes = terminus_classes(ind.to_sym)
+ rescue => detail
+ $stderr.puts "Could not load terminuses for #{ind}: #{detail}"
+ next
+ end
+ puts "%-30s: #{classes.join(", ")}" % ind
+ end
+ exit(0)
+ end
+
+ option("--mode RUNMODE", "-r") do |arg|
+ raise "Invalid run mode #{arg}; supported modes are user, agent, master" unless %w{user agent master}.include?(arg)
+ self.class.run_mode(arg.to_sym)
+ set_run_mode self.class.run_mode
+ end
+
+
+ attr_accessor :interface, :from, :type, :verb, :name, :arguments, :indirection, :format
+
+ def main
+ # Call the method associated with the provided action (e.g., 'find').
+ result = interface.send(verb, name, *arguments)
+ render_method = Puppet::Network::FormatHandler.format(format).render_method
+ puts result.send(render_method) if result
+ end
+
+ def setup
+
+ Puppet::Util::Log.newdestination :console
+
+ @verb, @name, @arguments = command_line.args
+ @arguments ||= []
+
+ @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym
+
+ @interface = Puppet::Interface.interface(@type).new
+ @format ||= @interface.class.default_format || :pson
+
+ validate
+
+ raise "Could not find data type #{type} for application #{self.class.name}" unless interface.indirection
+
+ @interface.set_terminus(from) if from
+ end
+
+ def validate
+ unless verb
+ raise "You must specify #{interface.actions.join(", ")} as a verb; 'save' probably does not work right now"
+ end
+
+ unless interface.action?(verb)
+ raise "Command '#{verb}' not found for #{type}"
+ end
+ end
+end