Class: Dnotify::Setup

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

Constant Summary collapse

ConfigPath =
File.expand_path "~/.dnotifyrc"

Class Method Summary collapse

Class Method Details

.checkObject



30
31
32
33
34
35
36
37
38
# File 'lib/dnotify.rb', line 30

def self.check
  config_present = File.exists?(ConfigPath)
  if config_present
    puts 'dnotify is setup'
  else
    puts "no config file(#{ Setup::ConfigPath }) exists"
  end
  config_present
end

.runObject



8
9
10
11
# File 'lib/dnotify.rb', line 8

def self.run
  setup unless check
  write_crontab
end

.setupObject



13
14
15
16
# File 'lib/dnotify.rb', line 13

def self.setup
  FileUtils.cp(File.join(File.dirname(__FILE__), '../resources/dnotifyrc.sample.yaml'), ConfigPath)
  puts "copied default template to #{ConfigPath}"
end

.write_crontabObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dnotify.rb', line 18

def self.write_crontab
  existing_crontab = `crontab -l`
  return if existing_crontab =~ /dnotify/i
  #copied from https://github.com/javan/whenever/blob/master/lib/whenever/command_line.rb
  puts 'setting up cron'
  tmp_cron_file = Tempfile.new('dnotify_tmp_cron').path
  File.open(tmp_cron_file, File::WRONLY | File::APPEND) do |file|
    file << existing_crontab << "* * * * * /bin/bash -l -c 'dnotify' > /tmp/dnotify.log 2>&1\n"
  end
  puts `crontab #{tmp_cron_file}`
end