Class: PostRunner::Main

Inherits:
Object
  • Object
show all
Includes:
DirUtils
Defined in:
lib/postrunner/Main.rb

Instance Method Summary collapse

Methods included from DirUtils

#create_directory

Constructor Details

#initializeMain

Returns a new instance of Main.



34
35
36
37
38
39
40
41
# File 'lib/postrunner/Main.rb', line 34

def initialize
  @filter = nil
  @name = nil
  @force = false
  @attribute = nil
  @value = nil
  @db_dir = File.join(ENV['HOME'], '.postrunner')
end

Instance Method Details

#main(args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/postrunner/Main.rb', line 43

def main(args)
  return 0 if (args = parse_options(args)).nil?

  unless $DEBUG
    Kernel.trap('INT') do
      begin
        Log.fatal('Aborting on user request!')
      rescue RuntimeError
        exit 1
      end
    end
  end

  begin
    create_directory(@db_dir, 'PostRunner data')
    ensure_flat_file_db
    @db = PEROBS::Store.new(File.join(@db_dir, 'database'),
                            { :engine => PEROBS::FlatFileDB,
                              :progressmeter =>
                              PEROBS::ConsoleProgressMeter.new })
    # Create a hash to store configuration data in the store unless it
    # exists already.
    cfg = (@db['config'] ||= @db.new(PEROBS::Hash))
    cfg['unit_system'] ||= 'metric'
    cfg['version'] ||= VERSION
    # First day of the week. 0 means Sunday, 1 Monday and so on.
    cfg['week_start_day'] ||= 1
    # We always override the data_dir as the user might have moved the data
    # directory. The only reason we store it in the DB is to have it
    # available throught the application.
    cfg['data_dir'] = @db_dir
    # Always update html_dir setting so that the DB directory can be moved
    # around by the user.
    cfg['html_dir'] = File.join(@db_dir, 'html')

    setup_directories
    retval = execute_command(args)
    @db.exit

    return retval

  rescue Exception => e
    if e.is_a?(SystemExit) || e.is_a?(Interrupt)
      $stderr.puts e.backtrace.join("\n") if $DEBUG
    elsif e.is_a?(Fit4Ruby::Abort)
      # Programm execution error that does not warrant a backtrace to be
      # printed.
      return -1
    else
      Log.error("#{e}\n#{e.backtrace.join("\n")}\n\n" +
                "#{'*' * 79}\nYou have triggered a bug in PostRunner " +
                "#{VERSION}!")
    end
    return -1
  ensure
    @db.exit if @db
  end
end