Module: Kanrisuru::Core::Stream

Extended by:
OsPackage::Define
Defined in:
lib/kanrisuru/core/stream.rb,
lib/kanrisuru/core/stream/commands.rb,
lib/kanrisuru/core/stream/parsers/sed.rb,
lib/kanrisuru/core/stream/commands/cat.rb,
lib/kanrisuru/core/stream/commands/sed.rb,
lib/kanrisuru/core/stream/parsers/echo.rb,
lib/kanrisuru/core/stream/commands/echo.rb,
lib/kanrisuru/core/stream/commands/head.rb,
lib/kanrisuru/core/stream/commands/tail.rb,
lib/kanrisuru/core/stream/commands/read_file_chunk.rb

Defined Under Namespace

Modules: Parser

Instance Method Summary collapse

Methods included from OsPackage::Define

extended, os_define

Instance Method Details

#cat(files, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kanrisuru/core/stream/commands/cat.rb', line 6

def cat(files, opts = {})
  command = Kanrisuru::Command.new('cat')
  command.append_flag('-T', opts[:show_tabs])
  command.append_flag('-n', opts[:number])
  command.append_flag('-s', opts[:squeeze_blank])
  command.append_flag('-v', opts[:show_nonprinting])
  command.append_flag('-E', opts[:show_ends])
  command.append_flag('-b', opts[:number_nonblank])
  command.append_flag('-A', opts[:show_all])

  command.append_array(files)

  append_file(command, opts)
  execute_shell(command)

  Kanrisuru::Result.new(command, &:to_a)
end

#echo(text, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/kanrisuru/core/stream/commands/echo.rb', line 6

def echo(text, opts = {})
  command = Kanrisuru::Command.new('echo')
  command.append_flag('-e', opts[:backslash])
  command << "'#{text}'"

  append_file(command, opts)
  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Echo.parse(cmd, opts)
  end
end

#head(path, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/kanrisuru/core/stream/commands/head.rb', line 6

def head(path, opts = {})
  command = Kanrisuru::Command.new('head')
  command.append_arg('-c', opts[:bytes])
  command.append_arg('-n', opts[:lines])
  command << path

  execute_shell(command)

  Kanrisuru::Result.new(command, &:to_a)
end

#read_file_chunk(path, start_line, end_line) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kanrisuru/core/stream/commands/read_file_chunk.rb', line 6

def read_file_chunk(path, start_line, end_line)
  if !start_line.instance_of?(Integer) || !end_line.instance_of?(Integer) ||
     start_line > end_line || start_line.negative? || end_line.negative?
    raise ArgumentError, 'Invalid start or end'
  end

  end_line = end_line - start_line + 1
  command = Kanrisuru::Command.new("tail -n +#{start_line} #{path} | head -n #{end_line}")

  execute_shell(command)

  Kanrisuru::Result.new(command, &:to_a)
end

#sed(path, expression, replacement, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kanrisuru/core/stream/commands/sed.rb', line 6

def sed(path, expression, replacement, opts = {})
  command = Kanrisuru::Command.new('sed')
  command.append_flag('-i', opts[:in_place])
  command.append_flag('-r', opts[:regexp_extended])

  command << "'s/#{expression}/#{replacement}/g'"
  command << "'#{path}'"

  append_file(command, opts)
  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Sed.parse(cmd, opts)
  end
end

#tail(path, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/kanrisuru/core/stream/commands/tail.rb', line 6

def tail(path, opts = {})
  command = Kanrisuru::Command.new('tail')
  command.append_arg('-c', opts[:bytes])
  command.append_arg('-n', opts[:lines])
  command << path

  execute_shell(command)

  Kanrisuru::Result.new(command, &:to_a)
end