Class: Knifecmd
Overview
Knifecmd
Used as the base for the knife commands.
Stores information about the current lookup and the Command tree
Constant Summary
Constants included from Dbg
Dbg::DefaultIO, Dbg::DefaultLevel
Instance Attribute Summary collapse
-
#ambiguities ⇒ Object
Returns the value of attribute ambiguities.
-
#cmd_found ⇒ Object
Returns the value of attribute cmd_found.
-
#cmd_left ⇒ Object
Returns the value of attribute cmd_left.
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#knife_commands ⇒ Object
Returns the value of attribute knife_commands.
-
#shortcuts ⇒ Object
Returns the value of attribute shortcuts.
Instance Method Summary collapse
-
#clear_config ⇒ Object
Remove a built config file.
-
#cmd_split(split_command_array) ⇒ Object
If the Command lookup needs to split a command remove the command from what’s left and split it into the componenets.
-
#found_cmd(command) ⇒ Object
if the Command lookup find a command, add it to found and take it away from whats left.
-
#found_shortcut(shortcut) ⇒ Object
If the Command lookup hits a shortcut, adjust local variables to match.
-
#found_string ⇒ Object
Return the found command as a space seperated string.
-
#initialize(argopts = {}) ⇒ Knifecmd
constructor
A new instance of Knifecmd.
-
#load_commands ⇒ Object
Turn the knife command arrays into a Command tree.
-
#lookup(commands = @commands) ⇒ Object
Start at the root command and go down the tree looking for each command.
-
#lookup_config ⇒ Object
Look up the knife commands from a prebuilt kknife config.
-
#lookup_files ⇒ Object
Look up the knife commands from knife library files.
- #print ⇒ Object
-
#print_tree ⇒ Object
Start with out root Command and print the command tree.
-
#reset_found(commands = []) ⇒ Object
Reset the command found instance variables.
-
#resolve(commands) ⇒ Object
Resolve a list of commands into real commands.
-
#run(commands = ARGV) ⇒ Object
Run the knife command.
-
#write_config ⇒ Object
Write out a built config file.
Methods included from Dbg
create, #dbg, #debug_logger, debug_logger, replace
Constructor Details
permalink #initialize(argopts = {}) ⇒ Knifecmd
Returns a new instance of Knifecmd.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/kknife/knifecmd.rb', line 35 def initialize( argopts = {} ) # Root command is k.. or knife @root = 'knife' opts = { :commands => ARGV.dup.freeze, :cmd_found => [], :cmd_left => ARGV.dup, :shortcuts => {}, :ambiguities => {}, :config_build => '~/.chef/.kknife.built.config', :config_user => '~/.chef/.kknife.user.config', }.merge argopts @commands = opts[:commands] @cmd_found = opts[:cmd_found] @cmd_left = opts[:cmd_left] @shortcuts = opts[:shortcuts] @ambiguities = opts[:ambiguities] @config_build = File. opts[:config_build] @config_user = File. opts[:config_user] @cmd_root = Command.new( @root, :controller => self ) # Instead of trawling for chef files each time we can load a previous config lookup_files unless lookup_config # Now process the knife commands into a tree of Command load_commands # Then setup to find commands reset_found end |
Instance Attribute Details
permalink #ambiguities ⇒ Object
Returns the value of attribute ambiguities.
33 34 35 |
# File 'lib/kknife/knifecmd.rb', line 33 def ambiguities @ambiguities end |
permalink #cmd_found ⇒ Object
Returns the value of attribute cmd_found.
33 34 35 |
# File 'lib/kknife/knifecmd.rb', line 33 def cmd_found @cmd_found end |
permalink #cmd_left ⇒ Object
Returns the value of attribute cmd_left.
33 34 35 |
# File 'lib/kknife/knifecmd.rb', line 33 def cmd_left @cmd_left end |
permalink #commands ⇒ Object
Returns the value of attribute commands.
33 34 35 |
# File 'lib/kknife/knifecmd.rb', line 33 def commands @commands end |
permalink #knife_commands ⇒ Object
Returns the value of attribute knife_commands.
33 34 35 |
# File 'lib/kknife/knifecmd.rb', line 33 def knife_commands @knife_commands end |
permalink #shortcuts ⇒ Object
Returns the value of attribute shortcuts.
33 34 35 |
# File 'lib/kknife/knifecmd.rb', line 33 def shortcuts @shortcuts end |
Instance Method Details
permalink #clear_config ⇒ Object
Remove a built config file
96 97 98 |
# File 'lib/kknife/knifecmd.rb', line 96 def clear_config FileUtils.rm @config_build if File.exists? @config_build end |
permalink #cmd_split(split_command_array) ⇒ Object
If the Command lookup needs to split a command remove the command from what’s left and split it into the componenets
153 154 155 156 157 158 159 |
# File 'lib/kknife/knifecmd.rb', line 153 def cmd_split( split_command_array ) # first remove the previous entry with _ @cmd_left.shift # then prefix the new split entires @cmd_left.unshift *split_command_array end |
permalink #found_cmd(command) ⇒ Object
if the Command lookup find a command, add it to found and take it away from whats left
164 165 166 167 168 169 |
# File 'lib/kknife/knifecmd.rb', line 164 def found_cmd( command ) dbg 'found_cmd b4 ', command, @cmd_found.join(','), '|', @cmd_left.join(',') @cmd_found.push command @cmd_left.shift dbg 'found_cmd aft', command, @cmd_found.join(','), '|', @cmd_left.join(',') end |
permalink #found_shortcut(shortcut) ⇒ Object
If the Command lookup hits a shortcut, adjust local variables to match
174 175 176 177 178 179 |
# File 'lib/kknife/knifecmd.rb', line 174 def found_shortcut( shortcut ) dbg 'found_shortcut b4 ', shortcut.join(','), '|', @cmd_left.join(',') @cmd_left.shift @cmd_left.unshift *shortcut dbg 'found_shortcut aft', shortcut.join(','), '|', @cmd_left.join(',') end |
permalink #found_string ⇒ Object
Return the found command as a space seperated string
190 191 192 |
# File 'lib/kknife/knifecmd.rb', line 190 def found_string ([ @root ] + @cmd_found ).join(' ') end |
permalink #load_commands ⇒ Object
Turn the knife command arrays into a Command tree
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/kknife/knifecmd.rb', line 102 def load_commands # loop over the commands, put them into Command @knife_commands.each do |category,command_arr| dbg 'category', category command_arr.each do |command| dbg 'command', command commands = command.split( /_/ ) @cmd_root.add_command commands end end end |
permalink #lookup(commands = @commands) ⇒ Object
Start at the root command and go down the tree looking for each command
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/kknife/knifecmd.rb', line 125 def lookup( commands = @commands ) dbg 'lookup', commands reset_found commands begin @cmd_root.process_lookup commands rescue Command::AmbiguousCommand => e abort "error looking up [#{commands.to_s}]. #{e}" rescue Command::NotFoundCommand => e abort "error looking up [#{commands.to_s}]. #{e}" rescue Command::NoMoreSubCommands dbg "end of knife lookups", @cmd_found.join(','), @cmd_left.join(',') rescue Command::NoMoreSuppliedCommands dbg "end of argv lookups", @cmd_found.join(','), @cmd_left.join(',') end @cmd_found end |
permalink #lookup_config ⇒ Object
Look up the knife commands from a prebuilt kknife config
72 73 74 75 76 77 |
# File 'lib/kknife/knifecmd.rb', line 72 def lookup_config # pull in a config if it exists return false unless File.exists? @config_build @knife_commands = JSON.parse File.open( @config_build, 'r' ){|f| f.read } end |
permalink #lookup_files ⇒ Object
Look up the knife commands from knife library files
81 82 83 84 85 |
# File 'lib/kknife/knifecmd.rb', line 81 def lookup_files # pull in the standard knife files Chef::Knife.load_commands @knife_commands = Chef::Knife.subcommands_by_category end |
permalink #print ⇒ Object
[View source]
119 120 121 |
# File 'lib/kknife/knifecmd.rb', line 119 def print @cmd_root.pp_single end |
permalink #print_tree ⇒ Object
Start with out root Command and print the command tree
116 117 118 |
# File 'lib/kknife/knifecmd.rb', line 116 def print_tree @cmd_root.pp end |
permalink #reset_found(commands = []) ⇒ Object
Reset the command found instance variables
183 184 185 186 |
# File 'lib/kknife/knifecmd.rb', line 183 def reset_found( commands = [] ) @cmd_found = [] @cmd_left = commands.dup end |
permalink #resolve(commands) ⇒ Object
Resolve a list of commands into real commands
196 197 198 199 |
# File 'lib/kknife/knifecmd.rb', line 196 def resolve( commands ) lookup commands @cmd_found + @cmd_left end |
permalink #run(commands = ARGV) ⇒ Object
Run the knife command
202 203 204 205 206 207 208 209 210 |
# File 'lib/kknife/knifecmd.rb', line 202 def run( commands = ARGV ) # mess with argv ARGV.replace resolve( commands ) # Run knife directly Chef::Application::Knife.new.run end |
permalink #write_config ⇒ Object
Write out a built config file
89 90 91 92 |
# File 'lib/kknife/knifecmd.rb', line 89 def write_config raise "No directory [#{File.dirname( @config_build )}]" unless Dir.exists? File.dirname( @config_build ) File.open( @config_build, 'w' ){|f| f.write @knife_commands.to_json } end |