Class: Berkshelf::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/berkshelf/cli.rb,
lib/berkshelf/commands/shelf.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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/berkshelf/cli.rb', line 53

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/berkshelf/cli.rb', line 37

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

    if 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



213
214
215
216
217
218
219
220
221
222
# File 'lib/berkshelf/cli.rb', line 213

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].each_with_object({}) { |(k, v), m| m[k.to_sym] = v }

  lockfile.apply(environment_name, lock_options)
end

#contingent(name) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/berkshelf/cli.rb', line 315

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

#info(name) ⇒ Object



289
290
291
292
293
# File 'lib/berkshelf/cli.rb', line 289

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

#installObject



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

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

#listObject



277
278
279
280
# File 'lib/berkshelf/cli.rb', line 277

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

#outdated(*names) ⇒ Object



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

def outdated(*names)
  berksfile = Berksfile.from_options(options)
  outdated  = berksfile.outdated(*names, include_non_satisfying: options[:all])
  Berkshelf.formatter.outdated(outdated)
end

#package(path = nil) ⇒ Object



344
345
346
347
348
349
350
351
352
353
# File 'lib/berkshelf/cli.rb', line 344

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



256
257
258
259
260
# File 'lib/berkshelf/cli.rb', line 256

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

#show(name) ⇒ Object



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

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

#update(*cookbook_names) ⇒ Object



149
150
151
152
# File 'lib/berkshelf/cli.rb', line 149

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

#upload(*names) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/berkshelf/cli.rb', line 190

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.each_with_object({}) { |(k, v), m| m[k.to_sym] = v })
end

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



374
375
376
377
# File 'lib/berkshelf/cli.rb', line 374

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

#verifyObject



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

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

#versionObject



416
417
418
# File 'lib/berkshelf/cli.rb', line 416

def version
  Berkshelf.formatter.version
end

#vizObject



408
409
410
411
412
413
# File 'lib/berkshelf/cli.rb', line 408

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

  Berkshelf.ui.info(path)
end