module Scripto::MiscCommands

Constants

BASE_62

Public Instance Methods

md5_file(path) click to toggle source

Return the md5 checksum for the file at path.

# File lib/scripto/misc_commands.rb, line 19
def md5_file(path)
  File.open(path) do |f|
    digest, buf = Digest::MD5.new, ''
    digest.update(buf) while f.read(4096, buf)
    digest.hexdigest
  end
end
md5_string(str) click to toggle source

Return the md5 checksum for str.

# File lib/scripto/misc_commands.rb, line 28
def md5_string(str)
  Digest::MD5.hexdigest(str.to_s)
end
prompt?(question) click to toggle source

Ask the user a question via stderr, then return true if they enter YES, yes, y, etc.

# File lib/scripto/misc_commands.rb, line 34
def prompt?(question)
  $stderr.write("#{question} (y/n) ")
  $stderr.flush
  $stdin.gets =~ /^y/i
end
random_string(len) click to toggle source

Return a random alphanumeric string of length len.

# File lib/scripto/misc_commands.rb, line 41
def random_string(len)
  (1..len).map { BASE_62.sample }.join
end
root?() click to toggle source

Return true if the current user is “root”.

# File lib/scripto/misc_commands.rb, line 14
def root?
  whoami == 'root'
end
whoami() click to toggle source

Who is the current user?

# File lib/scripto/misc_commands.rb, line 9
def whoami
  @scripto_whoami ||= Etc.getpwuid(Process.uid).name
end