Class: Remind::Commands::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/remind/commands/cli.rb

Constant Summary collapse

SCHEDULE_FILE =
File.expand_path(File.join(Dir.pwd, 'config', 'schedule.rb'))

Instance Method Summary collapse

Instance Method Details

#add_task(interval, command) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/remind/commands/cli.rb', line 34

def add_task(interval, command)
  unless File.exist?(SCHEDULE_FILE)
    create_schedule_file
  end

  new_task = "every #{interval} do\n  rake '#{command}'\nend\n\n"
  File.open(SCHEDULE_FILE, 'a') { |f| f.puts new_task }
  puts "Task added to #{SCHEDULE_FILE}"
  update_cron
end

#create_schedule_fileObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/remind/commands/cli.rb', line 46

def create_schedule_file  
  FileUtils.mkdir_p(File.dirname(SCHEDULE_FILE))
  File.open(SCHEDULE_FILE, 'w') do |file|
    file.puts "# frozen_string_literal: true"
    file.puts "# This file is auto-generated by Thor CLI"
    file.puts "set :output, \"#{Dir.pwd}/log/cron_log.log\""
    file.puts "set :environment, :development\n\n"
  end
  puts "Schedule file created at #{SCHEDULE_FILE}"
end

#list_tasksObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/remind/commands/cli.rb', line 65

def list_tasks
  unless File.exist?(SCHEDULE_FILE)
    create_schedule_file
  end

  tasks = File.readlines(SCHEDULE_FILE).select { |line| line.match?(/^every/) }

  if tasks.empty?
    puts "No tasks found."
  else
    puts "Scheduled tasks:"
    tasks.each { |task| puts task }
  end
end

#remove_task(command) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/remind/commands/cli.rb', line 82

def remove_task(command)
  content = File.read(SCHEDULE_FILE)
  new_content = content.gsub(/every .* do\n\s+rake '#{command}'\nend\n/, '')
  File.write(SCHEDULE_FILE, new_content)
  puts "Task removed from #{command}"
  update_cron
end

#set_envObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/remind/commands/cli.rb', line 12

def set_env
  webhook_url = ask("Enter your Slack Webhook URL:")

  if webhook_url.empty?
    puts 'No URL provided. Aborting...'
    return
  end

  FileUtils.touch('.env') unless File.exist?('.env')

  env_content = File.read('.env')
  if env_content.match?(/^SLACK_WEBHOOK_URL=/)
    env_content.gsub!(/^SLACK_WEBHOOK_URL=.*/, "SLACK_WEBHOOK_URL=#{webhook_url}")
  else
    env_content << "\nSLACK_WEBHOOK_URL=#{webhook_url}\n"
  end

  File.write('.env', env_content)
  puts 'SLACK_WEBHOOK_URL has been set in .env file.'
end

#update_cronObject



59
60
61
62
# File 'lib/remind/commands/cli.rb', line 59

def update_cron
  system("whenever --update-crontab")
  puts "Crontab updated"
end