Class: Berkshelf::Cli
Overview
Class Method Summary collapse
Instance Method Summary collapse
- #configure(path = Berkshelf::Config.path) ⇒ Object
- #cookbook(name) ⇒ Object
- #init(path = Dir.pwd) ⇒ Object
-
#initialize(*args) ⇒ Cli
constructor
A new instance of Cli.
- #install ⇒ Object
- #list ⇒ Object
- #open(name) ⇒ Object
- #outdated(*cookbook_names) ⇒ Object
- #show(name = nil) ⇒ Object
- #update(*cookbook_names) ⇒ Object
- #upload(*cookbook_names) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(*args) ⇒ Cli
Returns a new instance of Cli.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/berkshelf/cli.rb', line 15 def initialize(*args) super(*args) if @options[:config] unless File.exist?(@options[:config]) raise BerksConfigNotFound, "You specified a path to a configuration file that did not exist: '#{@options[:config]}'" end Berkshelf::Config.path = @options[:config] end Berkshelf.set_format @options[:format] @options = .dup # unfreeze frozen options Hash from Thor end |
Class Method Details
Instance Method Details
#configure(path = Berkshelf::Config.path) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/berkshelf/cli.rb', line 55 def configure(path = Berkshelf::Config.path) path = File.(path) if File.exist?(path) && ![:force] raise Berkshelf::ConfigExists, "A configuration file already exists. Re-run with the --force flag if you wish to overwrite it." end @config = Berkshelf::Config.new(path) [ "chef.chef_server_url", "chef.node_name", "chef.client_key", "chef.validation_client_name", "chef.validation_key_path", "vagrant.vm.box", "vagrant.vm.box_url", ].each do |attribute| default = @config.get_attribute(attribute) = "Enter value for #{attribute}" << " (default: '#{default}')" if default << ": " input = Berkshelf.ui.ask() if input.present? @config.set_attribute(attribute, input) end end unless @config.valid? raise InvalidConfiguration.new(@config.errors) end @config.save Berkshelf.formatter.msg "Config written to: '#{path}'" end |
#cookbook(name) ⇒ Object
368 369 370 371 372 373 374 375 376 377 |
# File 'lib/berkshelf/cli.rb', line 368 def cookbook(name) Berkshelf.formatter.deprecation "--git is now the default" if [:git] Berkshelf.formatter.deprecation "--vagrant is now the default" if [:vagrant] unless Config.instance.valid? raise InvalidConfiguration.new(Config.instance.errors) end ::Berkshelf::CookbookGenerator.new([name, File.join(Dir.pwd, name)], ).invoke_all end |
#init(path = Dir.pwd) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/berkshelf/cli.rb', line 279 def init(path = Dir.pwd) Berkshelf.formatter.deprecation "--git is now the default" if [:git] Berkshelf.formatter.deprecation "--vagrant is now the default" if [:vagrant] if File.chef_cookbook?(path) [:chefignore] = true [:metadata_entry] = true end ::Berkshelf::InitGenerator.new([path], ).invoke_all ::Berkshelf.formatter.msg "Successfully initialized" end |
#install ⇒ Object
130 131 132 133 |
# File 'lib/berkshelf/cli.rb', line 130 def install berksfile = ::Berkshelf::Berksfile.from_file([:berksfile]) berksfile.install() end |
#list ⇒ Object
300 301 302 303 304 305 306 307 |
# File 'lib/berkshelf/cli.rb', line 300 def list berksfile = ::Berkshelf::Berksfile.from_file([:berksfile]) Berkshelf.ui.say "Cookbooks installed by your Berksfile:" Berkshelf.ui.mute { berksfile.resolve }.sort.each do |cookbook| Berkshelf.ui.say " * #{cookbook.cookbook_name} (#{cookbook.version})" end end |
#open(name) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/berkshelf/cli.rb', line 96 def open(name) editor = [ENV['BERKSHELF_EDITOR'], ENV['VISUAL'], ENV['EDITOR']].find{|e| !e.nil? && !e.empty? } raise ArgumentError, "To open a cookbook, set $EDITOR or $BERKSHELF_EDITOR" unless editor cookbook = Berkshelf.cookbook_store.cookbooks(name).last raise CookbookNotFound, "Cookbook '#{name}' not found in any of the sources!" unless cookbook Dir.chdir(cookbook.path) do command = "#{editor} #{cookbook.path}" success = system(command) raise CommandUnsuccessful, "Could not run `#{command}`" unless success end end |
#outdated(*cookbook_names) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/berkshelf/cli.rb', line 235 def outdated(*cookbook_names) berksfile = ::Berkshelf::Berksfile.from_file([:berksfile]) Berkshelf.formatter.msg "Listing outdated cookbooks with newer versions available..." Berkshelf.formatter.msg "BETA: this feature will only pull differences from the community site and will" Berkshelf.formatter.msg "BETA: ignore all other sources you may have defined" Berkshelf.formatter.msg "" = { cookbooks: cookbook_names }.merge().symbolize_keys outdated = berksfile.outdated() if outdated.empty? Berkshelf.formatter.msg "All cookbooks up to date" else outdated.each do |cookbook, latest_version| Berkshelf.formatter.msg "Cookbook '#{cookbook.name} (#{cookbook.version_constraint})' is outdated (#{latest_version})" end end end |
#show(name = nil) ⇒ Object
316 317 318 319 320 321 322 323 324 |
# File 'lib/berkshelf/cli.rb', line 316 def show(name = nil) return list if name.nil? berksfile = ::Berkshelf::Berksfile.from_file([:berksfile]) cookbook = Berkshelf.ui.mute { berksfile.resolve }.find{ |cookbook| cookbook.cookbook_name == name } raise CookbookNotFound, "Cookbook '#{name}' was not installed by your Berksfile" unless cookbook Berkshelf.ui.say(cookbook.path) end |
#update(*cookbook_names) ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'lib/berkshelf/cli.rb', line 150 def update(*cookbook_names) berksfile = Berksfile.from_file([:berksfile]) = { cookbooks: cookbook_names }.merge().symbolize_keys berksfile.update() end |
#upload(*cookbook_names) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/berkshelf/cli.rb', line 192 def upload(*cookbook_names) berksfile = ::Berkshelf::Berksfile.from_file([:berksfile]) unless Berkshelf::Config.instance.chef.chef_server_url.present? msg = "Could not upload cookbooks: Missing Chef server_url." msg << " Generate or update your Berkshelf configuration that contains a valid Chef Server URL." raise UploadFailure, msg end unless Berkshelf::Config.instance.chef.node_name.present? msg = "Could not upload cookbooks: Missing Chef node_name." msg << " Generate or update your Berkshelf configuration that contains a valid Chef node_name." raise UploadFailure, msg end = { server_url: Berkshelf::Config.instance.chef.chef_server_url, client_name: Berkshelf::Config.instance.chef.node_name, client_key: Berkshelf::Config.instance.chef.client_key, ssl: { verify: ([:ssl_verify].nil? ? Berkshelf::Config.instance.ssl.verify : [:ssl_verify]) }, cookbooks: cookbook_names }.merge().symbolize_keys berksfile.upload() end |