Class: AlsaBackup::CLI

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

Class Method Summary collapse

Class Method Details

.execute(stdout, *arguments) ⇒ Object



6
7
8
9
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
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/alsa_backup/cli.rb', line 6

def self.execute(stdout, *arguments)
  options = {}
  mandatory_options = %w()

  OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      AlsaBackup : continuous recording with alsa

      Usage: #{File.basename($0)} [options]

      Options are:
    BANNER
    opts.separator ""
    opts.on("-f", "--file=FILE", String,
            "Recording file") { |arg| options[:file] = arg }
    opts.on("-l", "--length=LENGTH", String,
            "Length in seconds") { |arg| options[:length] = arg }
    opts.on("-d", "--directory=DIRECTORY", String,
            "Base directory") { |arg| options[:directory] = arg }
    opts.on("-c", "--config=CONFIG", String,
            "Configuration file") { |arg| options[:config] = arg }
    opts.on("-p", "--pid=PID_FILE", String,
            "File to write the process pid") { |arg| options[:pid] = arg }
    opts.on("-b", "--background", nil,
            "Daemonize the process") { |arg| options[:daemonize] = true }
    opts.on("-h", "--help",
            "Show this help message.") { stdout.puts opts; exit }
    opts.parse!(arguments)

    if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? }
      stdout.puts opts; exit
    end
  end

  load File.expand_path(options[:config]) if options[:config]

  AlsaBackup.recorder.file = options[:file] if options[:file]
  AlsaBackup.recorder.directory = options[:directory] if options[:directory]

  pid_file = File.expand_path(options[:pid]) if options[:pid]

  Daemonize.daemonize(nil, "alsa-backup") if options[:daemonize]
  File.write(pid_file, $$) if pid_file
  
  length = options[:length].to_i if options[:length]
  AlsaBackup.recorder.start(length)
rescue Exception => e
  AlsaBackup.logger.fatal(e)
end