Class: Tanakai::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/tanakai/cli.rb,
lib/tanakai/cli/generator.rb,
lib/tanakai/cli/ansible_command_builder.rb

Defined Under Namespace

Classes: AnsibleCommandBuilder, Generator

Instance Method Summary collapse

Instance Method Details

#__print_versionObject



157
158
159
# File 'lib/tanakai/cli.rb', line 157

def __print_version
  puts VERSION
end

#console(spider_name = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/tanakai/cli.rb', line 106

def console(spider_name = nil)
  require 'pry'
  require './config/boot' if inside_project?

  if spider_name
    raise "Can't find Tanakai project" unless inside_project?

    unless klass = Tanakai.find_by_name(spider_name)
      raise "Can't find spider with name `#{spider_name}` in the project. " \
        "To list all available spiders, run: `$ bundle exec tanakai list`"
    end
  else
    klass = inside_project? ? ApplicationSpider : ::Tanakai::Base
  end

  engine = options["engine"]&.delete(":")&.to_sym
  if url = options["url"]
    klass.new(engine).request_to(:console, url: options["url"])
  else
    klass.new(engine).public_send(:console)
  end
end

#crawl(spider_name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tanakai/cli.rb', line 72

def crawl(spider_name)
  raise "Can't find Tanakai project" unless inside_project?
  require './config/boot'

  unless klass = Tanakai.find_by_name(spider_name)
    raise "Can't find spider with name `#{spider_name}` in the project. " \
      "To list all available spiders, run: `$ bundle exec tanakai list`"
  end

  # Set time_zone if exists
  if time_zone = Tanakai.configuration.time_zone
    Tanakai.time_zone = time_zone
  end

  klass.crawl!
end

#dashboardObject



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/tanakai/cli.rb', line 162

def dashboard
  raise "Can't find Tanakai project" unless inside_project?

  require './config/boot'
  if Object.const_defined?("Tanakai::Dashboard")
    require 'tanakai/dashboard/app'
    Tanakai::Dashboard::App.run!
  else
    raise "Tanakai::Dashboard is not defined"
  end
end

#deploy(user_host) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tanakai/cli.rb', line 47

def deploy(user_host)
  unless options["skip-check"]
    if !`git status --short`.empty?
      raise "Deploy: Please commit your changes first"
    elsif `git remote`.empty?
      raise "Deploy: Please add remote origin repository to your repo first"
    elsif !`git rev-list master...origin/master`.empty?
      raise "Deploy: Please push your commits to the remote origin repo first"
    end
  end

  repo_url = options["repo-url"] ? options["repo-url"] : `git remote get-url origin`.strip
  repo_name = repo_url[/\/([^\/]*)\.git/i, 1]

  command = AnsibleCommandBuilder.new(user_host, options, playbook: "deploy",
    vars: { repo_url: repo_url, repo_name: repo_name, repo_key_path: options["repo-key-path"] }
  ).get

  pid = spawn *command
  Process.wait pid
end

#generate(generator_type, *args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tanakai/cli.rb', line 8

def generate(generator_type, *args)
  case generator_type
  when "project"
    project_name = args.shift
    raise "Provide project name to generate a new project" unless project_name.present?
    Generator.new.generate_project(project_name)
  when "spider"
    spider_name = args.shift
    raise "Provide spider name to generate a spider" unless spider_name.present?
    Generator.new.generate_spider(spider_name, in_project: inside_project?)
  when "schedule"
    Generator.new.generate_schedule
  else
    raise "Don't know this generator type: #{generator_type}"
  end
end

#listObject



130
131
132
133
134
135
# File 'lib/tanakai/cli.rb', line 130

def list
  raise "Can't find Tanakai project" unless inside_project?
  require './config/boot'

  Tanakai.list.keys.sort.each { |name| puts name }
end

#parse(spider_name, method_name) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tanakai/cli.rb', line 91

def parse(spider_name, method_name)
  raise "Can't find Tanakai project" unless inside_project?
  require './config/boot'

  unless klass = Tanakai.find_by_name(spider_name)
    raise "Can't find spider with name `#{spider_name}` in the project. " \
      "To list all available spiders, run: `$ bundle exec tanakai list`"
  end

  klass.parse!(method_name, url: options["url"])
end

#runnerObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/tanakai/cli.rb', line 141

def runner
  raise "Can't find Tanakai project" unless inside_project?

  jobs = options["jobs"]
  raise "Jobs count can't be 0" if jobs == 0

  require './config/boot'
  require 'tanakai/runner'

  spiders = options["include"].presence || Tanakai.list.keys
  spiders -= options["exclude"]

  Runner.new(spiders, jobs).run!
end

#setup(user_host) ⇒ Object



33
34
35
36
37
38
# File 'lib/tanakai/cli.rb', line 33

def setup(user_host)
  command = AnsibleCommandBuilder.new(user_host, options, playbook: "setup").get

  pid = spawn *command
  Process.wait pid
end