Module: Hackmac::Utils

Includes:
FileUtils
Defined in:
lib/hackmac/utils.rb

Overview

A module that provides utility methods for executing shell commands and interacting with users.

The Utils module includes methods for running system commands with colored output, prompting users for input, and handling common file operations in a Hackmac context.

Instance Method Summary collapse

Instance Method Details

#ask(prompt) ⇒ Boolean

The ask method prompts the user for a yes/no response.

Parameters:

  • prompt (String)

    the message to display to the user

Returns:

  • (Boolean)

    true if the user answered ‘y’ or ‘Y’, false otherwise



47
48
49
50
# File 'lib/hackmac/utils.rb', line 47

def ask(prompt)
  print prompt.bold.yellow
  gets =~ /\Ay/i
end

#x(cmd, verbose: true) ⇒ String

The x method executes a shell command and displays its output with colorized prompts.

Parameters:

  • cmd (String)

    the shell command to execute

  • verbose (TrueClass, FalseClass) (defaults to: true)

    whether to show command output in verbose mode

Returns:

  • (String)

    the captured output of the command execution



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hackmac/utils.rb', line 27

def x(cmd, verbose: true)
  prompt = cmd =~ /\A\s*sudo/ ? ?# : ?$
  cmd_output = "#{prompt} #{cmd}".color(27) + (verbose ? "" : " >/dev/null".yellow)
  output, result = nil, nil
  puts cmd_output
  system "#{cmd} 2>&1"
  result = $?
  if result.success?
    puts "✅ Command succeded!".green
  else
    puts "⚠️  Command #{cmd.inspect} failed with exit status #{result.exitstatus}".on_red.white
    exit result.exitstatus
  end
  output
end