Module: Libexec::Exec

Included in:
Libexec
Defined in:
lib/libexec/exec.rb

Instance Method Summary collapse

Instance Method Details

#by_each_chomp(*argv, env: {}, mode: "r", **opts) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/libexec/exec.rb', line 60

def by_each_chomp(*argv, env: {}, mode: "r", **opts)
  arr = []
  cmd = _ruby_style_cmd(argv)

  IO.popen(env, cmd, mode, opts) do |pipe|
    pipe.each_line do |line|
      arr.push(line.chomp)
    end
  end
  arr
end

#by_each_line(*argv, env: {}, mode: "r", **opts) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/libexec/exec.rb', line 48

def by_each_line(*argv, env: {}, mode: "r", **opts)
  arr = []
  cmd = _ruby_style_cmd(argv)

  IO.popen(env, cmd, mode, opts) do |pipe|
    pipe.each_line do |line|
      arr.push(line)
    end
  end
  arr
end

#by_ls_1(*argv, env: {}, mode: "r", **opts) ⇒ Array

wrapper for `ls -1` which will be often used

Parameters:

  • argv (Array)

Returns:

  • (Array)

    line.chomp



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/libexec/exec.rb', line 35

def by_ls_1(*argv, env: {}, mode: "r", **opts)
  arr = []
  argv.unshift("ls", "-1")
  cmd = _js_style_cmd(argv)

  IO.popen(env, cmd, mode, opts) do |pipe|
    pipe.each_line do |line|
      arr.push(line.chomp)
    end
  end
  arr
end

#code(*argv, **opts) ⇒ Integer

wrapper for Process.spawn link

Returns:

  • (Integer)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/libexec/exec.rb', line 88

def code(*argv, **opts)
  opt_catch_error = opts[:catch_error]
  opt_code = opts[:code]

  *args, last_arg = argv

  is_integer = last_arg.instance_of?(Integer)
  arg_catch_error = is_integer
  arg_code = is_integer ? last_arg : false

  catch_error = arg_catch_error || opt_catch_error || false
  code = arg_code || opt_code || 1

  cmd = is_integer ? _js_style_cmd(args) : _js_style_cmd(argv)
  Process.spawn(cmd)
  Process.wait

  original_code = $?.exitstatus
  result = original_code.zero?

  if catch_error && !result
    exit code
  else
    original_code
  end
rescue Errno::ENOENT => _e
  127
end

#each_line(*argv, env: {}, mode: "r", **opts, &block) ⇒ Object

Run code block in IO.popen
inspired from Ruby IO.each_line link



23
24
25
26
27
28
29
# File 'lib/libexec/exec.rb', line 23

def each_line(*argv, env: {}, mode: "r", **opts, &block)
  cmd = _ruby_style_cmd(argv)

  IO.popen(env, cmd, mode, opts) do |pipe|
    pipe.each_line(&block)
  end
end

#each_ls_1(*argv, env: {}, mode: "r", **opts, &block) ⇒ Object

Operate each line result by yourself in `ls -1`

Parameters:

  • argv (Array)


75
76
77
78
79
80
81
82
# File 'lib/libexec/exec.rb', line 75

def each_ls_1(*argv, env: {}, mode: "r", **opts, &block)
  argv.unshift("ls", "-1")
  cmd = _js_style_cmd(argv)

  IO.popen(env, cmd, mode, opts) do |pipe|
    pipe.each_line(&block)
  end
end

#output(*argv) ⇒ String

Note:

Only recommend to use when get single line output

wrapper for Kernel.`command` link

Returns:

  • (String)

    output.chomp



122
123
124
125
126
127
# File 'lib/libexec/exec.rb', line 122

def output(*argv)
  cmd = _js_style_cmd(argv)

  output = `#{cmd}`
  output.chomp
end

#run(*argv, env: {}, mode: "r", **opts) ⇒ nil

wrapper for Ruby IO.popen link
cmd first grammar
conv argv by _ruby_style_cmd

Returns:

  • (nil)


11
12
13
14
15
16
17
18
# File 'lib/libexec/exec.rb', line 11

def run(*argv, env: {}, mode: "r", **opts)
  cmd = _ruby_style_cmd(argv)

  IO.popen(env, cmd, mode, opts) do |pipe|
    print pipe.gets until pipe.eof?
  end
  nil
end