Class: SiteDiff::Cli

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_unknown_options?(_config) ⇒ Boolean

Thor, by default, does not raise an error for use of unknown options.

Returns:

  • (Boolean)


41
42
43
# File 'lib/sitediff/cli.rb', line 41

def self.check_unknown_options?(_config)
  true
end

.exit_on_failure?Boolean

Thor, by default, exits with 0 no matter what!

Returns:

  • (Boolean)


36
37
38
# File 'lib/sitediff/cli.rb', line 36

def self.exit_on_failure?
  true
end

Instance Method Details

#diff(*config_files) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sitediff/cli.rb', line 85

def diff(*config_files)
  @interval = options['interval']
  check_interval(@interval)
  @dir = get_dir(options['directory'])
  config = SiteDiff::Config.new(config_files, @dir)

  # override config based on options
  paths = options['paths']
  if (paths_file = options['paths-file'])
    if paths
      SiteDiff.log "Can't have both --paths-file and --paths", :error
      exit(-1)
    end

    paths_file = Pathname.new(paths_file).expand_path
    unless File.exist? paths_file
      raise Config::InvalidConfig,
            "Paths file '#{paths_file}' not found!"
    end
    SiteDiff.log "Reading paths from: #{paths_file}"
    config.paths = File.readlines(paths_file)
  end
  config.paths = paths if paths

  config.before['url'] = options['before'] if options['before']
  config.after['url'] = options['after'] if options['after']

  # Setup cache
  cache = SiteDiff::Cache.new(create: options['cached'] != 'none',
                              directory: @dir)
  cache.read_tags << :before if %w[before all].include?(options['cached'])
  cache.read_tags << :after if %w[after all].include?(options['cached'])
  cache.write_tags << :before << :after

  sitediff = SiteDiff.new(config, cache, options[:concurrency], @interval,
                          options['verbose'], options[:debug])
  num_failing = sitediff.run(get_curl_opts(options), options[:debug])
  exit_code = num_failing > 0 ? 2 : 0

  sitediff.dump(@dir, options['before-report'],
                options['after-report'])
rescue Config::InvalidConfig => e
  SiteDiff.log "Invalid configuration: #{e.message}", :error
  SiteDiff.log "at #{e.backtrace}", :error
else # no exception was raised
  # Thor::Error  --> exit(1), guaranteed by exit_on_failure?
  # Failing diff --> exit(2), populated above
  exit(exit_code)
end

#init(*urls) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/sitediff/cli.rb', line 185

def init(*urls)
  unless (1..2).cover? urls.size
    SiteDiff.log 'sitediff init requires one or two URLs', :error
    exit(2)
  end

  @interval = options['interval']
  check_interval(@interval)
  @dir = get_dir(options['directory'])
  curl_opts = get_curl_opts(options)
  @whitelist = create_regexp(options['whitelist'])
  @blacklist = create_regexp(options['blacklist'])
  creator = SiteDiff::Config::Creator.new(options[:concurrency],
                                          options['interval'],
                                          @whitelist,
                                          @blacklist,
                                          curl_opts,
                                          options[:debug],
                                          *urls)
  creator.create(
    depth: options[:depth],
    directory: @dir,
    rules: options[:rules] != 'no',
    rules_disabled: (options[:rules] == 'disabled')
  ) do |_tag, info|
    SiteDiff.log "Visited #{info.uri}, cached"
  end

  SiteDiff.log "Created #{creator.config_file.expand_path}", :success
  SiteDiff.log "You can now run 'sitediff diff'", :success
end

#serve(*config_files) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/sitediff/cli.rb', line 144

def serve(*config_files)
  config = SiteDiff::Config.new(config_files, options['directory'])
  # Could check non-empty config here but currently errors are already raised.
  @dir = get_dir(options['directory'])
  cache = Cache.new(directory: @dir)
  cache.read_tags << :before << :after

  SiteDiff::Webserver::ResultServer.new(
    options[:port],
    options['directory'],
    browse: options[:browse],
    cache: cache,
    config: config
  ).wait
rescue SiteDiffException => e
  SiteDiff.log e.message, :error
  SiteDiff.log e.backtrace, :error
end

#store(*config_files) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/sitediff/cli.rb', line 226

def store(*config_files)
  @dir = get_dir(options['directory'])
  config = SiteDiff::Config.new(config_files, @dir)
  config.validate(need_before: false)
  cache = SiteDiff::Cache.new(directory: @dir, create: true)
  cache.write_tags << :before

  base = options[:url] || config.after['url']
  fetcher = SiteDiff::Fetch.new(cache,
                                config.paths,
                                options[:interval],
                                options[:concurrency],
                                get_curl_opts(options),
                                options[:debug],
                                before: base)
  fetcher.run do |path, _res|
    SiteDiff.log "Visited #{path}, cached"
  end
end