Class: SiteDiff::Cli
- Inherits:
-
Thor
- Object
- Thor
- SiteDiff::Cli
- Defined in:
- lib/sitediff/cli.rb
Class Method Summary collapse
-
.check_unknown_options?(_config) ⇒ Boolean
Thor, by default, does not raise an error for use of unknown options.
-
.exit_on_failure? ⇒ Boolean
Thor, by default, exits with 0 no matter what!.
Instance Method Summary collapse
- #diff(*config_files) ⇒ Object
- #init(*urls) ⇒ Object
- #serve(*config_files) ⇒ Object
- #store(*config_files) ⇒ Object
Class Method Details
.check_unknown_options?(_config) ⇒ Boolean
Thor, by default, does not raise an error for use of unknown options.
41 42 43 |
# File 'lib/sitediff/cli.rb', line 41 def self.(_config) true end |
.exit_on_failure? ⇒ Boolean
Thor, by default, exits with 0 no matter what!
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 = ['interval'] check_interval(@interval) @dir = get_dir(['directory']) config = SiteDiff::Config.new(config_files, @dir) # override config based on options paths = ['paths'] if (paths_file = ['paths-file']) if paths SiteDiff.log "Can't have both --paths-file and --paths", :error exit(-1) end paths_file = Pathname.new(paths_file). 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'] = ['before'] if ['before'] config.after['url'] = ['after'] if ['after'] # Setup cache cache = SiteDiff::Cache.new(create: ['cached'] != 'none', directory: @dir) cache. << :before if %w[before all].include?(['cached']) cache. << :after if %w[after all].include?(['cached']) cache. << :before << :after sitediff = SiteDiff.new(config, cache, [:concurrency], @interval, ['verbose'], [:debug]) num_failing = sitediff.run(get_curl_opts(), [:debug]) exit_code = num_failing > 0 ? 2 : 0 sitediff.dump(@dir, ['before-report'], ['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 = ['interval'] check_interval(@interval) @dir = get_dir(['directory']) curl_opts = get_curl_opts() @whitelist = create_regexp(['whitelist']) @blacklist = create_regexp(['blacklist']) creator = SiteDiff::Config::Creator.new([:concurrency], ['interval'], @whitelist, @blacklist, curl_opts, [:debug], *urls) creator.create( depth: [:depth], directory: @dir, rules: [:rules] != 'no', rules_disabled: ([: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, ['directory']) # Could check non-empty config here but currently errors are already raised. @dir = get_dir(['directory']) cache = Cache.new(directory: @dir) cache. << :before << :after SiteDiff::Webserver::ResultServer.new( [:port], ['directory'], browse: [:browse], cache: cache, config: config ).wait rescue SiteDiffException => e SiteDiff.log e., :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(['directory']) config = SiteDiff::Config.new(config_files, @dir) config.validate(need_before: false) cache = SiteDiff::Cache.new(directory: @dir, create: true) cache. << :before base = [:url] || config.after['url'] fetcher = SiteDiff::Fetch.new(cache, config.paths, [:interval], [:concurrency], get_curl_opts(), [:debug], before: base) fetcher.run do |path, _res| SiteDiff.log "Visited #{path}, cached" end end |