Class: Berkshelf::Cli
Overview
Class Method Summary collapse
Instance Method Summary collapse
- #configure(path = Berkshelf::Config.path) ⇒ Object
- #contingent(name) ⇒ Object
- #cookbook(name) ⇒ Object
- #info(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) ⇒ Object
- #update(*cookbook_names) ⇒ Object
- #upload(*cookbook_names) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(*args) ⇒ Cli
Returns a new instance of Cli.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/berkshelf/cli.rb', line 23 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 if @options[:debug] Berkshelf.logger.level = ::Logger::DEBUG end if @options[:quiet] Berkshelf.ui.mute! end Berkshelf.set_format @options[:format] @options = .dup # unfreeze frozen options Hash from Thor end |
Class Method Details
.dispatch(meth, given_args, given_opts, config) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/berkshelf/cli.rb', line 7 def dispatch(meth, given_args, given_opts, config) unless (given_args & ['-h', '--help']).empty? if given_args.length == 1 # berks --help super else command = given_args.first super(meth, ['help', command].compact, nil, config) end else super Berkshelf.formatter.cleanup_hook unless config[:current_command].name == "help" end end |
Instance Method Details
#configure(path = Berkshelf::Config.path) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/berkshelf/cli.rb', line 83 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 |
#contingent(name) ⇒ Object
366 367 368 369 370 371 372 373 374 375 |
# File 'lib/berkshelf/cli.rb', line 366 def contingent(name) berksfile = ::Berkshelf::Berksfile.from_file([:berksfile]) Berkshelf.ui.say "Cookbooks contingent upon #{name}:" sources = Berkshelf.ui.mute { berksfile.resolve }.sort.each do |cookbook| if cookbook.dependencies.include?(name) Berkshelf.ui.say " * #{cookbook.cookbook_name} (#{cookbook.version})" end end end |
#cookbook(name) ⇒ Object
422 423 424 425 426 427 428 429 430 431 |
# File 'lib/berkshelf/cli.rb', line 422 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([File.join(Dir.pwd, name), name], ).invoke_all end |
#info(name) ⇒ Object
348 349 350 351 352 353 354 355 356 357 |
# File 'lib/berkshelf/cli.rb', line 348 def info(name) if [:version] cookbook = Berkshelf.cookbook_store.cookbook(name, [:version]) else cookbook = Berkshelf.cookbook_store.cookbooks(name).sort_by(&:version).last end raise CookbookNotFound, "Cookbook '#{name}' was not installed by your Berksfile" if cookbook.nil? Berkshelf.ui.say(cookbook.pretty_print) end |
#init(path = Dir.pwd) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/berkshelf/cli.rb', line 298 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
158 159 160 161 |
# File 'lib/berkshelf/cli.rb', line 158 def install berksfile = ::Berkshelf::Berksfile.from_file([:berksfile]) berksfile.install() end |
#list ⇒ Object
319 320 321 322 323 324 325 326 |
# File 'lib/berkshelf/cli.rb', line 319 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
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/berkshelf/cli.rb', line 124 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
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/berkshelf/cli.rb', line 254 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) ⇒ Object
335 336 337 338 339 340 341 |
# File 'lib/berkshelf/cli.rb', line 335 def show(name) 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
178 179 180 181 182 183 184 185 186 |
# File 'lib/berkshelf/cli.rb', line 178 def update(*cookbook_names) berksfile = Berksfile.from_file([:berksfile]) = { cookbooks: cookbook_names }.merge().symbolize_keys berksfile.update() end |
#upload(*cookbook_names) ⇒ Object
229 230 231 232 233 234 235 236 237 |
# File 'lib/berkshelf/cli.rb', line 229 def upload(*cookbook_names) berksfile = ::Berkshelf::Berksfile.from_file([:berksfile]) = Hash[.except(:no_freeze, :berksfile)].symbolize_keys [:cookbooks] = cookbook_names [:freeze] = false if [:no_freeze] berksfile.upload() end |