Module: Bovem::ShellMethods::Execute

Included in:
Bovem::Shell
Defined in:
lib/bovem/shell.rb

Overview

Methods to run commands or delete entries.

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) delete(files, run = true, show_errors = false, fatal = true)

Deletes a list of files.

Parameters:

  • files (Array)

    The list of files to remove

  • run (Boolean) (defaults to: true)

    If false, it will just print a list of message that would be deleted.

  • show_errors (Boolean) (defaults to: false)

    If show errors.

  • fatal (Boolean) (defaults to: true)

    If quit in case of fatal errors.

Returns:

  • (Boolean)

    true if operation succeeded, false otherwise.



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/bovem/shell.rb', line 375

def delete(files, run = true, show_errors = false, fatal = true)
  rv = true
  locale = self.i18n.shell
  files = files.ensure_array.compact.collect {|f| File.expand_path(f.ensure_string) }

  if !run then
    @console.warn(locale.remove_dry)
    @console.with_indentation(11) do
      files.each do |file| @console.write(file) end
    end
  else
    rv = catch(:rv) do
      begin
        FileUtils.rm_r(files, {noop: false, verbose: false, secure: true})
        throw(:rv, true)
      rescue => e
        handle_failure(e, :remove_unwritable, :remove_not_found, :remove_error, files, fatal, show_errors)
      end

      false
    end
  end

  rv
end

- (Hash) run(command, message = nil, run = true, show_exit = true, show_output = false, show_command = false, fatal = true)

Runs a command into the shell.

Parameters:

  • command (String)

    The string to run.

  • message (String) (defaults to: nil)

    A message to show before running.

  • run (Boolean) (defaults to: true)

    If false, it will just print a message with the full command that will be run.

  • show_exit (Boolean) (defaults to: true)

    If show the exit status.

  • show_output (Boolean) (defaults to: false)

    If show command output.

  • show_command (Boolean) (defaults to: false)

    If show the command that will be run.

  • fatal (Boolean) (defaults to: true)

    If quit in case of fatal errors.

Returns:

  • (Hash)

    An hash with status and output keys.



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/bovem/shell.rb', line 347

def run(command, message = nil, run = true, show_exit = true, show_output = false, show_command = false, fatal = true)
  rv = {status: 0, output: ""}
  command = command.ensure_string
  locale = self.i18n.shell

  # Show the command
  @console.begin(message) if message.present?

  if !run then # Print a message
    @console.warn(locale.run_dry(command))
    @console.status(:ok) if show_exit
  else # Run
    rv = execute_command(command, show_command, show_output)
  end

  # Return
  @console.status(rv[:status] == 0 ? :ok : :fail) if show_exit
  exit(rv[:status]) if fatal && rv[:status] != 0
  rv
end