Class: Berkshelf::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/berkshelf/cli.rb

Overview

Author:

Class Method Summary collapse

Instance Method Summary collapse

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 = 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.expand_path(path)

  if File.exist?(path) && !options[: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)

    message = "Enter value for #{attribute}"
    message << " (default: '#{default}')" if default
    message << ": "

    input = Berkshelf.ui.ask(message)

    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(options[: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 options[:git]
  Berkshelf.formatter.deprecation "--vagrant is now the default" if options[:vagrant]

  unless Config.instance.valid?
    raise InvalidConfiguration.new(Config.instance.errors)
  end

  ::Berkshelf::CookbookGenerator.new([File.join(Dir.pwd, name), name], options).invoke_all
end

#info(name) ⇒ Object

Raises:



348
349
350
351
352
353
354
355
356
357
# File 'lib/berkshelf/cli.rb', line 348

def info(name)
  if options[:version]
    cookbook = Berkshelf.cookbook_store.cookbook(name, options[: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 options[:git]
  Berkshelf.formatter.deprecation "--vagrant is now the default" if options[:vagrant]

  if File.chef_cookbook?(path)
    options[:chefignore] = true
    options[:metadata_entry] = true
  end

  ::Berkshelf::InitGenerator.new([path], options).invoke_all

  ::Berkshelf.formatter.msg "Successfully initialized"
end

#installObject



158
159
160
161
# File 'lib/berkshelf/cli.rb', line 158

def install
  berksfile = ::Berkshelf::Berksfile.from_file(options[:berksfile])
  berksfile.install(options)
end

#listObject



319
320
321
322
323
324
325
326
# File 'lib/berkshelf/cli.rb', line 319

def list
  berksfile = ::Berkshelf::Berksfile.from_file(options[: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

Raises:



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(options[: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 ""

  outdated_options = {
    cookbooks: cookbook_names
  }.merge(options).symbolize_keys

  outdated = berksfile.outdated(outdated_options)

  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

Raises:



335
336
337
338
339
340
341
# File 'lib/berkshelf/cli.rb', line 335

def show(name)
  berksfile = ::Berkshelf::Berksfile.from_file(options[: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(options[:berksfile])

  update_options = {
    cookbooks: cookbook_names
  }.merge(options).symbolize_keys

  berksfile.update(update_options)
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(options[:berksfile])

  upload_options             = Hash[options.except(:no_freeze, :berksfile)].symbolize_keys
  upload_options[:cookbooks] = cookbook_names
  upload_options[:freeze]    = false if options[:no_freeze]

  berksfile.upload(upload_options)
end

#versionObject



378
379
380
381
382
# File 'lib/berkshelf/cli.rb', line 378

def version
  Berkshelf.formatter.msg version_header
  Berkshelf.formatter.msg "\n"
  Berkshelf.formatter.msg license
end