Class: Saimaa::Command

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

Class Method Summary collapse

Class Method Details

.execute(cmd, project) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/saimaa.rb', line 44

def execute(cmd, project)
  stats, stdout, stderr = systemu(cmd)
  result = %W(
    --project--
    #{project}
    --stdout--
    #{stdout}
    --stderr--
    #{stderr}
  ).join("\n")
  [stats, result]
end

.notify(cmd, stdin) ⇒ Object



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

def notify(cmd, stdin)
  stats, stdout, stderr = systemu(cmd, :stdin => stdin)
  [stats, stdout, stderr]
end

.run(argv) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/saimaa.rb', line 10

def run(argv)
  options = {}
  stats, result = []
  OptionParser.new do |opt|
    opt.on('-c COMMAND', 'execute command') do |cmd|
      options[:execute] = cmd
    end
    opt.on('-n COMMAND', 'notifier command') do |cmd|
      options[:notify] = cmd
    end
    opt.parse!(argv)
  end

  project = argv.first

  if options.key?(:execute)
    stats, result = execute(options[:execute], project)
  end

  unless stats.to_i == 0
    if options.key?(:notify)
      stats, stdout, stderr = notify(options[:notify], result)
      puts stdout
    else
      puts result
    end
  end

  save(project,
    :stats => stats.to_i,
    :result => result,
    :time => Time.now)
end

.save(project, record) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/saimaa.rb', line 62

def save(project, record)
  path = "#{ENV["HOME"]}/.saimaa"
  FileUtils.mkdir(path) unless File.exists?(path)
  db = YAML::Store.new("#{path}/saimaa.yml")
  db.transaction do
    db[project] ||= []
    db[project] << record
  end
end