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



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

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



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/berkshelf/cli.rb', line 330

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



426
427
428
429
430
431
432
# File 'lib/berkshelf/cli.rb', line 426

def cookbook(name, path = nil)
  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



304
305
306
307
308
# File 'lib/berkshelf/cli.rb', line 304

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

#init(path = '.') ⇒ Object



267
268
269
270
271
272
273
274
# File 'lib/berkshelf/cli.rb', line 267

def init(path = '.')
  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



292
293
294
295
# File 'lib/berkshelf/cli.rb', line 292

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

#outdated(*names) ⇒ Object



248
249
250
251
252
# File 'lib/berkshelf/cli.rb', line 248

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

#package(path = nil) ⇒ Object



359
360
361
362
363
364
365
366
367
368
# File 'lib/berkshelf/cli.rb', line 359

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



260
261
262
263
264
# File 'lib/berkshelf/cli.rb', line 260

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

#show(name) ⇒ Object



317
318
319
320
321
# File 'lib/berkshelf/cli.rb', line 317

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



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

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

#verifyObject



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

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

#versionObject



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

def version
  Berkshelf.formatter.version
end

#vizObject



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

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

  Berkshelf.ui.info(path)
end