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
# File 'lib/scripto/misc_commands.rb', line 19

def md5_file(path)
  Digest::MD5.file(path).hexdigest
end

#md5_string(str) ⇒ Object

Return the md5 checksum for str.



24
25
26
# File 'lib/scripto/misc_commands.rb', line 24

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)


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

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.



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

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