Class: Berkshelf::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/berkshelf/cli.rb,
lib/berkshelf/commands/shelf.rb,
lib/berkshelf/commands/test_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cli

Returns a new instance of Cli.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/berkshelf/cli.rb', line 27

def initialize(*args)
  super(*args)

  if @options[:config]
    unless File.exist?(@options[:config])
      raise ConfigNotFound.new(:berkshelf, @options[:config])
    end
    Berkshelf::Config.set_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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/berkshelf/cli.rb', line 11

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

#apply(environment_name) ⇒ Object



243
244
245
246
247
248
# File 'lib/berkshelf/cli.rb', line 243

def apply(environment_name)
  berksfile    = ::Berkshelf::Berksfile.from_file(options[:berksfile])
  lock_options = Hash[options].symbolize_keys

  berksfile.apply(environment_name, lock_options)
end

#configureObject



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
122
123
124
125
126
127
128
129
# File 'lib/berkshelf/cli.rb', line 91

def configure
  path = File.expand_path(options[: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



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

def contingent(name)
  berksfile = Berksfile.from_file(options[:berksfile])

  sources = Berkshelf.ui.mute { berksfile.resolve(berksfile.sources)[:solution] }.sort
  dependencies = sources.select { |cookbook| cookbook.dependencies.include?(name) }

  if dependencies.empty?
    Berkshelf.formatter.msg "There are no cookbooks contingent upon '#{name}' defined in this Berksfile"
  else
    Berkshelf.formatter.msg "Cookbooks in this Berksfile contingent upon #{name}:"
    print_list(dependencies)
  end
end

#cookbook(name) ⇒ Object



395
396
397
398
399
400
401
402
403
404
# File 'lib/berkshelf/cli.rb', line 395

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

#init(path = Dir.pwd) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/berkshelf/cli.rb', line 288

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



151
152
153
154
# File 'lib/berkshelf/cli.rb', line 151

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

#listObject



309
310
311
312
313
314
315
316
317
318
319
# File 'lib/berkshelf/cli.rb', line 309

def list
  berksfile = Berksfile.from_file(options[:berksfile])
  sources = Berkshelf.ui.mute { berksfile.resolve(berksfile.sources)[:solution] }.sort

  if sources.empty?
    Berkshelf.formatter.msg 'There are no cookbooks installed by your Berksfile'
  else
    Berkshelf.formatter.msg 'Cookbooks installed by your Berksfile:'
    print_list(sources)
  end
end

#outdated(*cookbook_names) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/berkshelf/cli.rb', line 265

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

#package(name = nil) ⇒ Object



382
383
384
385
# File 'lib/berkshelf/cli.rb', line 382

def package(name = nil)
  berksfile = Berkshelf::Berksfile.from_file(options[:berksfile])
  berksfile.package(name, options)
end

#show(name) ⇒ Object

Raises:



328
329
330
331
332
333
334
335
336
337
338
# File 'lib/berkshelf/cli.rb', line 328

def show(name)
  berksfile = Berksfile.from_file(options[:berksfile])

  cookbook = Berkshelf.ui.mute {
    berksfile.resolve(berksfile.find(name))[:solution].first
  }

  raise CookbookNotFound, "Cookbook '#{name}' is not installed by your Berksfile" unless cookbook

  Berkshelf.formatter.show(cookbook)
end

#update(*cookbook_names) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/berkshelf/cli.rb', line 171

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



222
223
224
225
226
227
228
229
230
# File 'lib/berkshelf/cli.rb', line 222

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



388
389
390
391
392
# File 'lib/berkshelf/cli.rb', line 388

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