Class: Daily::Txt::CLI

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

Class Method Summary collapse

Class Method Details

.bootstrap(argv) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/daily/txt/cli.rb', line 33

def bootstrap(argv)
  begin
    option = parse_option(argv)
  rescue OptionParser::InvalidOption => e
    abort(e.to_s)
  end
  begin
    config_path = option[:config_path]
    config = Daily::Txt::Config.load(config_path)
  rescue Daily::Txt::Config::NotFound => e
    if config_path
      abort(e.to_s)
    end
    config = Daily::Txt::Config.create_default
  end

  Daily::Txt::PathBuilder.prepare_basedir(config["home"])
  
  dispatch_action(option, config)
end

.dispatch_action(option, config) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/daily/txt/cli.rb', line 54

def dispatch_action(option, config)
  case option[:mode]
  when :list
    list(config, option)
  else
    open_today(config)
  end
end

.list(config, option) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/daily/txt/cli.rb', line 63

def list(config, option)
  visitor = File::Visitor.new
  visitor.add_filter(:ext, Daily::Txt::PathBuilder::DEFAULT_EXT)

  if option[:asc]
    visitor.set_direction(:asc)
  else
    visitor.set_direction(:desc)
  end

  visitor.visit(config["home"]) do |path|
    puts path
  end
end

.open_today(config) ⇒ Object



78
79
80
81
82
83
# File 'lib/daily/txt/cli.rb', line 78

def open_today(config)
  path = Daily::Txt::PathBuilder.by_date(
    config["home"], Date.today)
  Daily::Txt::PathBuilder.prepare_basedir(path)
  Daily::Txt::System.exec(config["editor"], path)
end

.parse_option(argv) ⇒ Object



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

def parse_option(argv)
  option = {}
  parser = OptionParser.new

  parser.on('-f PATH', 'config file path') do |path|
    option[:config_path] = path
  end
  parser.on('-l', '--list', 'list text files') do |path|
    option[:mode] = :list
  end
  parser.on('--ascending', 'make list order ascending') do
    option[:asc] = true
  end

  begin
    parser.parse!(argv)
  rescue OptionParser::MissingArgument => e
    abort(e.to_s)
  end
  option
end