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

Defined Under Namespace

Classes: Runner

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cli

Returns a new instance of Cli.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/berkshelf/cli.rb', line 58

def initialize(*args)
  super(*args)

  if @options[:config]
    unless File.exist?(@options[:config])
      raise ConfigNotFound.new(:berkshelf, @options[:config])
    end

    Berkshelf.config = Berkshelf::Config.from_file(@options[:config])
  end

  if @options[:debug]
    ENV["BERKSHELF_DEBUG"] = "true"
    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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/berkshelf/cli.rb', line 42

def dispatch(meth, given_args, given_opts, config)
  if given_args.length > 1 && !(given_args & Thor::HELP_MAPPINGS).empty?
    command = given_args.first

    if self.subcommands.include?(command)
      super(meth, [command, 'help'].compact, nil, config)
    else
      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



226
227
228
229
230
231
232
233
234
235
# File 'lib/berkshelf/cli.rb', line 226

def apply(environment_name)
  unless File.exist?(options[:lockfile])
    raise LockfileNotFound, "No lockfile found at #{options[:lockfile]}"
  end

  lockfile     = Lockfile.from_file(options[:lockfile])
  lock_options = Hash[options].symbolize_keys

  lockfile.apply(environment_name, lock_options)
end

#contingent(name) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/berkshelf/cli.rb', line 338

def contingent(name)
  berksfile    = Berksfile.from_options(options)
  dependencies = berksfile.cookbooks.select do |cookbook|
    cookbook.dependencies.include?(name)
  end

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

#cookbook(name, path = nil) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
# File 'lib/berkshelf/cli.rb', line 434

def cookbook(name, path = nil)
  Berkshelf.formatter.deprecation <<EOF
This command is being deprecated in favor of `chef generate cookbook` and will soon return an error.
Please use `chef generate cookbook` instead of this command.
EOF
  path = File.join(Dir.pwd, name) if path.nil?
  Berkshelf.formatter.deprecation '--git is now the default' if options[:git]
  Berkshelf.formatter.deprecation '--vagrant is now the default' if options[:vagrant]

  Berkshelf::CookbookGenerator.new([path, name], options).invoke_all
end

#info(name) ⇒ Object



312
313
314
315
316
# File 'lib/berkshelf/cli.rb', line 312

def info(name)
  berksfile = Berksfile.from_options(options)
  cookbook  = berksfile.retrieve_locked(name)
  Berkshelf.formatter.info(cookbook)
end

#init(path = '.') ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/berkshelf/cli.rb', line 271

def init(path = '.')
  Berkshelf.formatter.deprecation <<EOF
This command is being deprecated in favor of `chef generate cookbook` and will soon return an error.
Please use `chef generate cookbook` instead of this command.
EOF
  Berkshelf.formatter.deprecation '--git is now the default' if options[:git]
  Berkshelf.formatter.deprecation '--vagrant is now the default' if options[:vagrant]

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

  Berkshelf.formatter.msg 'Successfully initialized'
end

#installObject



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/berkshelf/cli.rb', line 134

def install
  if options[:path]
    # TODO: Remove in Berkshelf 4.0
    Berkshelf.formatter.deprecation "`berks install --path [PATH]` has been replaced by `berks vendor`."
    Berkshelf.formatter.deprecation "Re-run your command as `berks vendor [PATH]` or see `berks help vendor`."
    exit(1)
  end

  berksfile = Berksfile.from_options(options)
  berksfile.install
end

#listObject



300
301
302
303
# File 'lib/berkshelf/cli.rb', line 300

def list
  berksfile = Berksfile.from_options(options)
  Berkshelf.formatter.list(berksfile.list)
end

#outdated(*names) ⇒ Object



252
253
254
255
256
# File 'lib/berkshelf/cli.rb', line 252

def outdated(*names)
  berksfile = Berksfile.from_options(options)
  outdated  = berksfile.outdated(*names)
  Berkshelf.formatter.outdated(outdated)
end

#package(path = nil) ⇒ Object



367
368
369
370
371
372
373
374
375
376
# File 'lib/berkshelf/cli.rb', line 367

def package(path = nil)
  if path.nil?
    path ||= File.join(Dir.pwd, "cookbooks-#{Time.now.to_i}.tar.gz")
  else
    path = File.expand_path(path)
  end

  berksfile = Berksfile.from_options(options)
  berksfile.package(path)
end

#search(name) ⇒ Object



264
265
266
267
268
# File 'lib/berkshelf/cli.rb', line 264

def search(name)
  source = Source.new(options[:source])
  cookbooks = source.search(name)
  Berkshelf.formatter.search(cookbooks)
end

#show(name) ⇒ Object



325
326
327
328
329
# File 'lib/berkshelf/cli.rb', line 325

def show(name)
  berksfile = Berksfile.from_options(options)
  cookbook  = berksfile.retrieve_locked(name)
  Berkshelf.formatter.show(cookbook)
end

#update(*cookbook_names) ⇒ Object



161
162
163
164
# File 'lib/berkshelf/cli.rb', line 161

def update(*cookbook_names)
  berksfile = Berksfile.from_options(options)
  berksfile.update(*cookbook_names)
end

#upload(*names) ⇒ Object



202
203
204
205
206
207
208
209
# File 'lib/berkshelf/cli.rb', line 202

def upload(*names)
  berksfile = Berksfile.from_options(options)

  options[:freeze]    = !options[:no_freeze]
  options[:validate]  = false if options[:skip_syntax_check]

  berksfile.upload(names, options.symbolize_keys)
end

#vendor(path = File.join(Dir.pwd, "berks-cookbooks")) ⇒ Object



393
394
395
396
# File 'lib/berkshelf/cli.rb', line 393

def vendor(path = File.join(Dir.pwd, "berks-cookbooks"))
  berksfile = Berkshelf::Berksfile.from_options(options)
  berksfile.vendor(path)
end

#verifyObject



402
403
404
405
406
# File 'lib/berkshelf/cli.rb', line 402

def verify
  berksfile = Berksfile.from_options(options)
  berksfile.verify
  Berkshelf.formatter.msg "Verified."
end

#versionObject



429
430
431
# File 'lib/berkshelf/cli.rb', line 429

def version
  Berkshelf.formatter.version
end

#vizObject



421
422
423
424
425
426
# File 'lib/berkshelf/cli.rb', line 421

def viz
  berksfile = Berksfile.from_options(options)
  path = berksfile.viz(options[:outfile])

  Berkshelf.ui.info(path)
end