Class: Kimurai::CLI

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

Defined Under Namespace

Classes: AnsibleCommandBuilder, Generator

Instance Method Summary collapse

Instance Method Details

#__print_versionObject



154
155
156
# File 'lib/kimurai/cli.rb', line 154

def __print_version
  puts VERSION
end

#console(spider_name = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/kimurai/cli.rb', line 103

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

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

    unless klass = Kimurai.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 kimurai list`"
    end
  else
    klass = inside_project? ? ApplicationSpider : ::Kimurai::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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kimurai/cli.rb', line 69

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

  unless klass = Kimurai.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 kimurai list`"
  end

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

  klass.crawl!
end

#dashboardObject



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/kimurai/cli.rb', line 159

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

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

#deploy(user_host) ⇒ Object



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

def deploy(user_host)
  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

  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/kimurai/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



127
128
129
130
131
132
# File 'lib/kimurai/cli.rb', line 127

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

  Kimurai.list.keys.each { |name| puts name }
end

#parse(spider_name, method_name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kimurai/cli.rb', line 88

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

  unless klass = Kimurai.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 kimurai list`"
  end

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

#runnerObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/kimurai/cli.rb', line 138

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

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

  require './config/boot'
  require 'kimurai/runner'

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

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

#setup(user_host) ⇒ Object



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

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

  pid = spawn *command
  Process.wait pid
end