Module: Scripto::MiscCommands

Included in:
Scripto, Main
Defined in:
lib/scripto/misc_commands.rb

Constant Summary collapse

BASE_62 =
("0".."9").to_a + ("A".."Z").to_a + ("a".."z").to_a

Instance Method Summary collapse

Instance Method Details

#md5_file(path) ⇒ Object

Return the md5 checksum for the file at path.



19
20
21
22
23
24
25
26
27
# File 'lib/scripto/misc_commands.rb', line 19

def md5_file(path)
  File.open(path) do |f|
    digest, buf = Digest::MD5.new, ""
    while f.read(4096, buf)
      digest.update(buf)
    end
    digest.hexdigest
  end
end

#md5_string(str) ⇒ Object

Return the md5 checksum for str.



30
31
32
# File 'lib/scripto/misc_commands.rb', line 30

def md5_string(str)
  Digest::MD5.hexdigest(str.to_s)
end

#prompt?(question) ⇒ Boolean

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

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/scripto/misc_commands.rb', line 36

def prompt?(question)
  $stderr.write("#{question} (y/n) ")
  $stderr.flush
  $stdin.gets =~ /^y/i
end

#random_string(len) ⇒ Object

Return a random alphanumeric string of length len.



43
44
45
# File 'lib/scripto/misc_commands.rb', line 43

def random_string(len)
  (1..len).map { BASE_62.sample }.join
end

#root?Boolean

Return true if the current user is “root”.

Returns:

  • (Boolean)


14
15
16
# File 'lib/scripto/misc_commands.rb', line 14

def root?
  whoami == "root"
end

#whoamiObject

Who is the current user?



9
10
11
# File 'lib/scripto/misc_commands.rb', line 9

def whoami
  @scripto_whoami ||= Etc.getpwuid(Process.uid).name
end