Class: SimpleShell

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_shell.rb

Overview

A very simple shell operations implementation

Synopsis

shell = SimpleShell.new
res = shell.ls
puts res.out
puts res.err
puts res.S?  (as $? wont work :->)

commands = shell.in("/other/path") do |sh|
  sh.ls
  sh.rm "-r", "bar"
end
puts commands.first.out

Handling stdout and stderr

shell = SimpleShell.new
shell.stdout_handler = ->(line) {
  do_something(line)
}

shell.do("./long_running.sh")

Off course the same goes for “‘shell.stderr_handler“`

Defined Under Namespace

Classes: Command

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base = Dir.pwd, env = {}) ⇒ SimpleShell

Create a new shell instance. This is tied to a base dir, which defaults to Dir.pwd

SimpleShell.new
SimpleShell.new("/path/to/base")


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

def initialize(base=Dir.pwd, env={})
  if base.is_a?(Hash)
    env  = base
    base = Dir.pwd
  end

  @base     = base
  @env      = env
  @commands = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

funkyness for neat little shell access

shell.ls              # = shell.system("ls")
shell.rm %w(-rf foo/) # = shell.system("rm", "-rf", "foo/")


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

def method_missing(name, *args, &block)
  super
rescue NoMethodError => e
  if args[0].is_a? Array
    args = args[0]
  end

  self.do(name.to_s, *args, &block)
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



31
32
33
# File 'lib/simple_shell.rb', line 31

def base
  @base
end

#commandsObject (readonly)

Returns the value of attribute commands.



31
32
33
# File 'lib/simple_shell.rb', line 31

def commands
  @commands
end

#envObject (readonly)

Returns the value of attribute env.



31
32
33
# File 'lib/simple_shell.rb', line 31

def env
  @env
end

#stderr_handlerObject

Returns the value of attribute stderr_handler.



32
33
34
# File 'lib/simple_shell.rb', line 32

def stderr_handler
  @stderr_handler
end

#stdout_handlerObject

Returns the value of attribute stdout_handler.



32
33
34
# File 'lib/simple_shell.rb', line 32

def stdout_handler
  @stdout_handler
end

Class Method Details

.noisyObject



34
35
36
# File 'lib/simple_shell.rb', line 34

def self.noisy
  @noisy ||= false
end

.noisy=(noise) ⇒ Object



38
39
40
# File 'lib/simple_shell.rb', line 38

def self.noisy=(noise)
  @noisy = noise
end

Instance Method Details

#in(dir) {|shell| ... } ⇒ Object

open up a sub shell at another directory

shell.in("/path/to/other") do |sub_shell|
  sub_shell.system("ls")
  sub_shell.in("/path/to/third") do |sub_sub_shell|
    ...
 end

Yields:

  • (shell)


67
68
69
70
71
72
73
# File 'lib/simple_shell.rb', line 67

def in(dir, &block)
  dir = File.join(@base, dir) if !File.exists?(dir) || dir !~ /^\//

  shell = SimpleShell.new(dir, @env)
  yield shell
  return shell.commands
end

#S?Boolean

the S? of the last command

Returns:

  • (Boolean)


103
104
105
# File 'lib/simple_shell.rb', line 103

def S?
  @commands.last.S?
end

#system(*args, &block) ⇒ Object Also known as: do, command

perform a command on the shell

shell.system("ls")
shell.do("ls")
shell.command("ls")

It is expected that arguments are provided as an array for security/safety purposes:

shell.system("rm", "-r", "-f", "foo/")

Returns a Command instance



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/simple_shell.rb', line 88

def system(*args, &block)
  command = nil
  Dir.chdir(@base) do
    command = Command.new(self)
    command.execute(*args, &block)
  end

  @commands << command

  return command
end