Module: Scripto::MiscCommands
Constant Summary collapse
- BASE_62 =
('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
Instance Method Summary collapse
-
#md5_file(path) ⇒ Object
Return the md5 checksum for the file at
path
. -
#md5_string(str) ⇒ Object
Return the md5 checksum for
str
. -
#prompt?(question) ⇒ Boolean
Ask the user a question via stderr, then return true if they enter YES, yes, y, etc.
-
#random_string(len) ⇒ Object
Return a random alphanumeric string of length
len
. -
#root? ⇒ Boolean
Return true if the current user is “root”.
-
#whoami ⇒ Object
Who is the current user?.
Instance Method Details
#md5_file(path) ⇒ Object
Return the md5 checksum for the file at path
.
19 20 21 22 23 24 25 |
# 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) ⇒ Object
Return the md5 checksum for str
.
28 29 30 |
# File 'lib/scripto/misc_commands.rb', line 28 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.
34 35 36 37 38 |
# 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) ⇒ Object
Return a random alphanumeric string of length len
.
41 42 43 |
# File 'lib/scripto/misc_commands.rb', line 41 def random_string(len) (1..len).map { BASE_62.sample }.join end |
#root? ⇒ Boolean
Return true if the current user is “root”.
14 15 16 |
# File 'lib/scripto/misc_commands.rb', line 14 def root? whoami == 'root' end |
#whoami ⇒ Object
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 |