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.



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 = options.dup # unfreeze frozen options Hash from Thor
end

Class Method Details

.dispatch(meth, given_args, given_opts, config) ⇒ Object



9
10
11
12
# File 'lib/berkshelf/cli.rb', line 9

def dispatch(meth, given_args, given_opts, config)
  super
  Berkshelf.formatter.cleanup_hook unless config[:current_task].name == "help"
end

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.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

#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 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([name, File.join(Dir.pwd, name)], options).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 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



130
131
132
133
# File 'lib/berkshelf/cli.rb', line 130

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

#listObject



300
301
302
303
304
305
306
307
# File 'lib/berkshelf/cli.rb', line 300

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:



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(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 = nil) ⇒ Object

Raises:



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(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



150
151
152
153
154
155
156
157
158
# File 'lib/berkshelf/cli.rb', line 150

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



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

  upload_options = {
    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: (options[:ssl_verify].nil? ? Berkshelf::Config.instance.ssl.verify : options[:ssl_verify])
    },
    cookbooks: cookbook_names
  }.merge(options).symbolize_keys

  berksfile.upload(upload_options)
end

#versionObject



327
328
329
330
331
# File 'lib/berkshelf/cli.rb', line 327

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