Class: TaskLoop::Log

Inherits:
Command
  • Object
show all
Defined in:
lib/taskloop/command/log.rb

Constant Summary

Constants inherited from Command

Command::DOLPHIN, Command::LOGO

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#create_dir_if_needed, #create_file_if_needed, #create_taskloop_file_structure_if_needed, #taskloop_cron_log_path, #taskloop_cron_tab_path, #taskloop_data_dir, #taskloop_data_proj_dirs, #taskloop_dir, #taskloop_environments_path, #taskloop_proj_list_dirs, #taskloop_proj_list_path, #taskloop_taskfile_paths, #tasklooprc_path

Constructor Details

#initialize(argv) ⇒ Log

Returns a new instance of Log.



20
21
22
23
24
# File 'lib/taskloop/command/log.rb', line 20

def initialize(argv)
  @task_name = argv.option('task-name')
  @cron = argv.flag?('cron', false)
  super
end

Class Method Details

.optionsObject



13
14
15
16
17
18
# File 'lib/taskloop/command/log.rb', line 13

def self.options
  [
    ['--task-name=TASK_NAME', "To show the log of a task specified by option."],
    ['--cron', 'To show the log of taskloop, which is based on cron.'],
  ].concat(super)
end

Instance Method Details

#check_log_of_cronObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/taskloop/command/log.rb', line 92

def check_log_of_cron
  puts "=============================".ansi.blue
  puts "Log of cron: ".ansi.blue
  puts ""
  log_file = File.open(taskloop_cron_log_path, "r")
  log_file.each_line do |line|
    puts line
  end
  puts "=============================".ansi.blue
  puts ""
end

#check_log_of_task(name) ⇒ Object



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
# File 'lib/taskloop/command/log.rb', line 50

def check_log_of_task(name)
  found = false
  data_proj_dirs = Dir.entries(taskloop_data_dir)
  data_proj_dirs.each do |dir|
    if dir == "." or dir == ".."
      next
    end
    data_proj_dir = File.join(taskloop_data_dir, dir)

    print_proj = false
    log_files = Dir.entries(data_proj_dir)
    log_files.each do |file|
      if "#{name.sha1}_log" == file
        found = true

        if !print_proj
          print_proj = true
          desc_file_path = File.join(data_proj_dir, ".description")
          puts "=============================".ansi.blue
          File.open(desc_file_path).each_line do |line|
            puts "Project of <#{line.strip}>".ansi.blue
          end
        end
        # print
        task_log_path = File.join(data_proj_dir, "#{name.sha1}_log")
        puts "Log of <Task.name: #{name}> above: ".ansi.blue
        File.open(task_log_path).each_line do |line|
          puts line
        end

        puts "=============================".ansi.blue
        puts ""
      end
    end
  end

  unless found
    puts "Warning: log of <Task.name: #{name}> not exist. Please check if the name of task is correct.".ansi.yellow
    puts ""
  end
end

#runObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/taskloop/command/log.rb', line 37

def run
  super
  if @task_name
    check_log_of_task(@task_name)
    return
  end

  if @cron
    check_log_of_cron
    return
  end
end

#validate!Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/taskloop/command/log.rb', line 26

def validate!
  super
  if @task_name && @cron
    help! "The --task-name option and the --cron option cannot be used simultaneously."
  end

  if @task_name == nil && !@cron
    help! "Use --task-name option or --cron option for 'taskloop log' command."
  end
end