Module: NiTo

Overview

uNix Tools like mkdir, mount in Ruby code

Class Method Summary collapse

Class Method Details

.cp(src, dest) ⇒ Object



66
67
68
# File 'lib/nito.rb', line 66

def cp(src, dest)
  FileUtils.cp src, dest
end

.echo(file, content) ⇒ Object

Like echo ‘content’ > to_file



57
58
59
# File 'lib/nito.rb', line 57

def echo(file, content)
  File.write(file, "#{content}\n")
end

.echo_a(file, content) ⇒ Object

Like echo ‘content’ >> to_file



62
63
64
# File 'lib/nito.rb', line 62

def echo_a(file, content)
  File.write(file, "#{content}\n", mode: 'a') unless grep? file, content
end

.grep?(file, search) ⇒ Boolean

Returns:

  • (Boolean)


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

def grep?(file, search)
  is_found = false
  return is_found unless File.exist? file

  File.open(file).each do |l|
    is_found = true if l =~ /#{search}/
  end
  is_found
end

.mkdir(path, perm = 0755) ⇒ Object



13
14
15
16
17
# File 'lib/nito.rb', line 13

def mkdir(path, perm = 0755)
  return if Dir.exist? path

  FileUtils.mkdir_p path, mode: perm
end

.mount(*args) ⇒ Object

Mount, accept *args, the last argument should be the destination e.g: mount ‘–types proc’, ‘/proc’, ‘/mnt/getch/proc’



41
42
43
44
45
46
# File 'lib/nito.rb', line 41

def mount(*args)
  return if mount? args.last

  mkdir args.last
  Getch::Command.new('mount', args.join(' '))
end

.mount?(dir) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/nito.rb', line 48

def mount?(dir)
  res = false
  File.open('/proc/mounts').each do |l|
    res = true if l =~ /#{dir}/
  end
  res
end

.mv(src, dest) ⇒ Object



70
71
72
# File 'lib/nito.rb', line 70

def mv(src, dest)
  FileUtils.mv src, dest
end

.rm(file) ⇒ Object



29
30
31
# File 'lib/nito.rb', line 29

def rm(file)
  File.exist?(file) && File.delete(file)
end

.search_proc_swaps(path) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/nito.rb', line 92

def search_proc_swaps(path)
  found = nil
  File.open('/proc/swaps').each do |l|
    if l =~ /#{path}/
      found = l.split(' ')
    end
  end
  found
end

.sed(file, regex, change) ⇒ Object

Like sed -i /old:new/ file



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nito.rb', line 80

def sed(file, regex, change)
  tmp_file = Tempfile.new
  File.open(file).each do |l|
    if l.match(regex)
      echo_a tmp_file, change
    else
      File.write tmp_file, l, mode: 'a'
    end
  end
  cp tmp_file, file
end

.sh(*args) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/nito.rb', line 121

def sh(*args)
  log = Log.new
  Open3.popen3 args.join(' ') do |_, stdout, stderr, wait_thr|
    if wait_thr.value.success?
      log.info_res 'Ok'
      return stdout.read.chomp
    end
    puts
    log.dbg args.join(' ') + "\nEXIT:#{wait_thr.value}"
    log.dbg "STDERR:#{stderr.read}"
    log.dbg "STDOUT:#{stdout.read}"
    log.fatal 'Die'
  end
end

.swapoff(dev) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/nito.rb', line 102

def swapoff(dev)
  return unless grep? '/proc/swaps', dev

  found = search_proc_swaps(dev)
  found ?
    Getch::Command.new('swapoff', found[0]) :
    return
end

.swapoff_dm(name) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/nito.rb', line 111

def swapoff_dm(name)
  dm = Getch::Helpers.get_dm name
  dm || return

  found = search_proc_swaps(dm)
  found ?
    Getch::Command.new('swapoff', found[0]) :
    return
end

.touch(file) ⇒ Object

create a void file



75
76
77
# File 'lib/nito.rb', line 75

def touch(file)
  File.write(file, '') unless File.exist? file
end

.umount(dir) ⇒ Object



33
34
35
36
37
# File 'lib/nito.rb', line 33

def umount(dir)
  return unless mount? dir

  Getch::Command.new('umount', dir)
end