Class: Pinoccio::FunctionCommands

Inherits:
CommandCollection show all
Defined in:
lib/commands.rb

Instance Method Summary collapse

Methods inherited from CommandCollection

#execute, #get, #initialize, #run

Constructor Details

This class inherits a constructor from Pinoccio::CommandCollection

Instance Method Details

#add(name, body = nil) ⇒ Object



327
328
329
330
331
# File 'lib/commands.rb', line 327

def add(name, body=nil)
  body = name if body.nil?
  body = "function #{name} { #{body} }" unless body =~ /^function /
  execute(body)
end

#listObject



301
302
303
304
305
306
307
308
# File 'lib/commands.rb', line 301

def list
  ls = run('ls')
  ls.split(/\r?\n/).reduce({}) do |hsh, fn|
    matches = fn.match(/function ([a-z0-9]+) \{/)
    hsh[matches[1]] = fn
    hsh
  end
end

#pid_for(fn) ⇒ Object



337
338
339
340
341
# File 'lib/commands.rb', line 337

def pid_for(fn)
  process = running.find {|p| p[:function].to_s == fn.to_s }
  return process[:pid] if process
  nil
end

#remove(fn) ⇒ Object



310
311
312
# File 'lib/commands.rb', line 310

def remove(fn)
  execute("rm #{fn}")
end

#remove_allObject



314
315
316
# File 'lib/commands.rb', line 314

def remove_all
  execute("rm *")
end

#repeat(fn, delay) ⇒ Object



318
319
320
# File 'lib/commands.rb', line 318

def repeat(fn, delay)
  execute("run #{fn}, #{delay}")
end

#runningObject



343
344
345
346
347
348
349
# File 'lib/commands.rb', line 343

def running
  ps = run("ps")
  ps.split(/\r?\n/).map do |line|
    pid, fn = *line.split(": ")
    {pid: pid.to_i, function: fn}
  end
end

#startup(body) ⇒ Object



333
334
335
# File 'lib/commands.rb', line 333

def startup(body)
  add(:startup, body)
end

#stop(pid) ⇒ Object



322
323
324
325
# File 'lib/commands.rb', line 322

def stop(pid)
  pid = pid_for(pid) unless pid.is_a?(Integer)
  execute("stop #{pid}") if pid
end