Module: Sys
Instance Method Summary collapse
-
#any_key? ⇒ Boolean
Wait for any key to be pressed.
-
#caller_filename ⇒ Object
Get the caller’s filename for the caller of the function this call is nested in not the function this call is called in.
-
#capture ⇒ Object
Capture STDOUT to a string.
-
#env(var, required: true) ⇒ Object
Get the given environment variable by nam.
-
#exec(cmd, env: {}, die: true) ⇒ Object
Run the system command in an exception wrapper.
-
#getpass ⇒ Object
Read a password from stdin without echoing.
-
#rm_rf(path) ⇒ Object
- Remove given dir or file Params:
path
- path to delete
returns
-
path that was deleted.
- path to delete
- Remove given dir or file Params:
-
#umount(mount, retries: 1) ⇒ Object
- Unmount the given mount point Params:
mount
- mount point to unmount
retries
-
number of times to retry.
- mount point to unmount
- Unmount the given mount point Params:
Instance Method Details
#any_key? ⇒ Boolean
Wait for any key to be pressed
31 32 33 34 35 36 37 38 39 |
# File 'lib/nub/sys.rb', line 31 def any_key? begin state = `stty -g` `stty raw -echo -icanon isig` STDIN.getc.chr ensure `stty #{state}` end end |
#caller_filename ⇒ Object
Get the caller’s filename for the caller of the function this call is nested in not the function this call is called in
44 45 46 47 |
# File 'lib/nub/sys.rb', line 44 def caller_filename path = caller_locations(2, 1).first.path return File.basename(path) end |
#capture ⇒ Object
Capture STDOUT to a string
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/nub/sys.rb', line 51 def capture stdout, stderr = StringIO.new, StringIO.new $stdout, $stderr = stdout, stderr result = Proc.new.call $stdout, $stderr = STDOUT, STDERR $stdout.flush $stderr.flush return OpenStruct.new(result: result, stdout: stdout.string, stderr: stderr.string) end |
#env(var, required: true) ⇒ Object
Get the given environment variable by nam
67 68 69 70 71 |
# File 'lib/nub/sys.rb', line 67 def env(var, required:true) value = ENV[var] Log.die("#{var} env variable is required!") if required && !value return value end |
#exec(cmd, env: {}, die: true) ⇒ Object
Run the system command in an exception wrapper
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/nub/sys.rb', line 78 def exec(cmd, env:{}, die:true) result = false puts("exec: #{cmd.is_a?(String) ? cmd : cmd * ' '}") begin if cmd.is_a?(String) result = system(env, cmd, out: $stdout, err: :out) else result = system(env, *cmd, out: $stdout, err: :out) end rescue Exception => e result = false puts(e..colorize(:red)) puts(e.backtrace.inspect.colorize(:red)) exit if die end !puts("Error: failed to execute command properly".colorize(:red)) and exit unless !die or result return result end |
#getpass ⇒ Object
Read a password from stdin without echoing
103 104 105 106 107 108 |
# File 'lib/nub/sys.rb', line 103 def getpass print("Enter Password: ") pass = STDIN.noecho(&:gets).strip puts return pass end |
#rm_rf(path) ⇒ Object
Remove given dir or file Params:
path
-
path to delete
returns
-
path that was deleted
114 115 116 117 |
# File 'lib/nub/sys.rb', line 114 def rm_rf(path) Sys.exec("rm -rf #{path}") return path end |
#umount(mount, retries: 1) ⇒ Object
Unmount the given mount point Params:
mount
-
mount point to unmount
retries
-
number of times to retry
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/nub/sys.rb', line 123 def umount(mount, retries:1) check = ->(mnt){ match = `mount`.split("\n").find{|x| x.include?(mnt)} match = match.split('overlay').first if match return (match and match.include?(mnt)) } success = false while not success and retries > 0 if check[mount] success = system('umount', '-fv', mount) else success = true end # Sleep for a second if failed sleep(1) and retries -= 1 unless success end # Die if still mounted !puts("Error: Failed to umount #{mount}".colorize(:red)) and exit if check[mount] end |