class SiteDiff::Cli

SiteDiff CLI.

Public Class Methods

check_unknown_options?(_config) click to toggle source

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

# File lib/sitediff/cli.rb, line 35
def self.check_unknown_options?(_config)
  true
end
exit_on_failure?() click to toggle source

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

# File lib/sitediff/cli.rb, line 30
def self.exit_on_failure?
  true
end

Public Instance Methods

clean_keys(hash, *keys) click to toggle source

Clean keys - return a subset of a hash with keys as symbols.

# File lib/sitediff/cli.rb, line 259
def clean_keys(hash, *keys)
  new_hash = hash.transform_keys { |k| k.tr('-', '_').to_sym }
  new_hash.slice(*keys)
end
crawl(config_file = nil) click to toggle source

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

# File lib/sitediff/cli.rb, line 228
def crawl(config_file = nil)
  api = Api.new(options['directory'], config_file)
  api.crawl
end
diff(config_file = nil) click to toggle source

Computes diffs.

# File lib/sitediff/cli.rb, line 102
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
    )
  api_options[:cli_mode] = true
  api.diff(api_options)
end
get_curl_opts(options) click to toggle source

Generates CURL options.

TODO: Possibly move to API class.

# File lib/sitediff/cli.rb, line 237
def get_curl_opts(options)
  # We do want string keys here
  bool_hash = { 'true' => true, 'false' => false }
  curl_opts = UriWrapper::DEFAULT_CURL_OPTS
              .clone
              .merge(options['curl_options'] || {})
              .merge(options['curl_opts'] || {})
  curl_opts.each { |k, v| curl_opts[k] = bool_hash.fetch(v, v) }
  curl_opts
end
get_dir(directory) click to toggle source

Ensures that the given directory exists.

# File lib/sitediff/cli.rb, line 250
def get_dir(directory)
  # Create the dir. Must go before cache initialization!
  @dir = Pathname.new(directory || '.')
  @dir.mkpath unless @dir.directory?
  @dir.to_s
end
init(*urls) click to toggle source

Initializes a sitediff (yaml) configuration file.

# File lib/sitediff/cli.rb, line 183
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, # may be nil
        directory: get_dir(options['directory']),
        curl_opts: get_curl_opts(options)
      }
    )
  Api.init(api_options)
end
serve(config_file = nil) click to toggle source

Serves SiteDiff report for accessing in the browser.

# File lib/sitediff/cli.rb, line 142
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) click to toggle source

Caches the current version of the site.

# File lib/sitediff/cli.rb, line 217
def store(config_file = nil)
  api = Api.new(options['directory'], config_file)
  api_options = clean_keys(options, :url, :debug)
  api.store(api_options)
end
version() click to toggle source

Show version information.

# File lib/sitediff/cli.rb, line 42
def version
  gemspec = SiteDiff.gemspec
  output = []
  output.push("Sitediff CLI #{gemspec.version}")
  if options[:verbose]
    output.push('Website: ' + gemspec.homepage)
    output.push('GitHub: ' + gemspec.metadata['source_code_uri'])
  end
  puts output.join("\n")
end