efa9ed56b1ba0abb6346d7cb9cef02d596782294
[packages/precise/mcollective.git] / plugins / mcollective / application / completion.rb
1 module MCollective
2   class Application::Completion<MCollective::Application
3     description "Helper for shell completion systems"
4
5     exclude_argument_sections "common", "filter", "rpc"
6
7     option :list_agents,
8            :description => "List all known agents",
9            :arguments => "--list-agents",
10            :required => false,
11            :type => :boolean
12
13     option :list_actions,
14            :description => "List all actions for an agent",
15            :arguments => "--list-actions",
16            :required => false,
17            :type => :boolean
18
19     option :list_inputs,
20            :description => "List all inputs for an action",
21            :arguments => "--list-inputs",
22            :required => false,
23            :type => :boolean
24
25     option :list_applications,
26            :description => "List all known applications",
27            :arguments => "--list-applications",
28            :required => false,
29            :type => :boolean
30
31     option :agent,
32            :description => "The agent to operate on",
33            :arguments => "--agent AGENT",
34            :required => false
35
36     option :action,
37            :description => "The action to operate on",
38            :arguments => "--action ACTION",
39            :required => false
40
41     def list_agents
42       if options[:verbose]
43         PluginManager.find(:agent, "ddl").each do |agent|
44           begin
45             ddl = DDL.new(agent)
46             puts "%s:%s" % [ agent, ddl.meta[:description] ]
47           rescue
48           end
49         end
50       else
51         PluginManager.find(:agent, "ddl").each {|p| puts p}
52       end
53     end
54
55     def list_actions
56       abort "Please specify an agent to list actions for" unless configuration[:agent]
57
58       if options[:verbose]
59         ddl = DDL.new(configuration[:agent], :agent)
60
61         ddl.actions.sort.each do |action|
62           puts "%s:%s" % [action, ddl.action_interface(action)[:description]]
63         end
64       else
65         DDL.new(configuration[:agent], :agent).actions.sort.each {|a| puts a}
66       end
67     rescue
68     end
69
70     def list_inputs
71       abort "Please specify an action and agent to list inputs for" unless configuration[:agent] && configuration[:action]
72
73       if options[:verbose]
74         ddl = DDL.new(configuration[:agent], :agent)
75         action = ddl.action_interface(configuration[:action])
76         action[:input].keys.sort.each do |input|
77           puts "%s:%s" % [input, action[:input][input][:description]]
78         end
79       else
80         DDL.new(configuration[:agent], :agent).action_interface(configuration[:action])[:input].keys.sort.each {|i| puts i}
81       end
82     rescue
83     end
84
85     def list_applications
86       if options[:verbose]
87         Applications.list.each do |app|
88           puts "%s:%s" % [app, Applications[app].application_description]
89         end
90       else
91         Applications.list.each {|a| puts a}
92       end
93     end
94
95     def main
96       actions = configuration.keys.map{|k| k.to_s}.grep(/^list_/)
97
98       abort "Please choose either --list-[agents|actions|inputs|applications]" if actions.empty?
99       abort "Please choose only one of --list-[agents|actions|inputs|applications]" if actions.size > 1
100
101       send actions.first
102     end
103   end
104 end