Class: SiteDiff::Cli

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

Overview

SiteDiff CLI.

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)


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

def self.check_unknown_options?(_config)
  true
end

.exit_on_failure?Boolean

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

Returns:

  • (Boolean)


31
32
33
# File 'lib/sitediff/cli.rb', line 31

def self.exit_on_failure?
  true
end

Instance Method Details

#crawl(config_file = nil) ⇒ Object

Crawls the “before” site to determine “paths”.



241
242
243
244
# File 'lib/sitediff/cli.rb', line 241

def crawl(config_file = nil)
  api = Api.new(options['directory'], config_file)
  api.crawl
end

#diff(config_file = nil) ⇒ Object

Computes diffs.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sitediff/cli.rb', line 112

def diff(config_file = nil)
  # Determine "paths" override based on options.
  if options['paths'] && options['paths-file']
    SiteDiff.log "Can't specify both --paths-file and --paths.", :error
    exit(-1)
  end

  api = Api.new(options['directory'], config_file)
  api_options =
    clean_keys(
      options,
      :paths,
      :paths_file,
      :ignore_whitespace,
      :export,
      :before,
      :after,
      :cached,
      :verbose,
      :debug,
      :report_format,
      :before_report,
      :after_report,
      :remove_html_comments
    )
  api_options[:cli_mode] = true
  puts api_options
  api.diff(api_options)
end

#init(*urls) ⇒ Object

Initializes a sitediff (yaml) configuration file.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/sitediff/cli.rb', line 195

def init(*urls)
  unless (1..2).cover? urls.size
    SiteDiff.log 'sitediff init requires one or two URLs', :error
    exit(2)
  end
  api_options =
    clean_keys(
      options,
      :depth,
      :concurrency,
      :interval,
      :include,
      :exclude,
      :preset,
      :crawl
    )
    .merge(
      {
        after_url: urls.pop,
        before_url: urls.pop,
        directory: get_dir(options['directory']),
        curl_opts: get_curl_opts(options)
      }
    )

  Api.init(api_options)
end

#serve(config_file = nil) ⇒ Object

Serves SiteDiff report for accessing in the browser.



154
155
156
157
158
# File 'lib/sitediff/cli.rb', line 154

def serve(config_file = nil)
  api = Api.new(options['directory'], config_file)
  api_options = clean_keys(options, :browse, :port)
  api.serve(api_options)
end

#store(config_file = nil) ⇒ Object

Caches the current version of the site.



230
231
232
233
234
# File 'lib/sitediff/cli.rb', line 230

def store(config_file = nil)
  api = Api.new(options['directory'], config_file)
  api_options = clean_keys(options, :url, :debug)
  api.store(api_options)
end

#versionObject

Show version information.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sitediff/cli.rb', line 43

def version
  filename = '../../sitediff.gemspec'
  filename = '/sitediff/sitediff.gemspec' if !File.exist?(File.expand_path(filename, __dir__)) && File.exist?(File.expand_path('/sitediff/sitediff.gemspec', __dir__))

  gemspec = Bundler.load_gemspec(File.expand_path(filename, __dir__))

  output = []
  output.push("Sitediff CLI #{gemspec.version}")
  if options[:verbose]
    output.push("Website: #{gemspec.homepage}")
    output.push("GitHub: #{gemspec.['source_code_uri']}")
  end
  puts output.join("\n")
end