Skip to content

Commit

Permalink
Merge pull request #11 from robmiller/fix/code-cleanliness
Browse files Browse the repository at this point in the history
Code style and refactoring

Fixes #9, #10
  • Loading branch information
robmiller committed Aug 16, 2013
2 parents 9a7ed80 + 260dde1 commit 6975bd2
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 311 deletions.
51 changes: 26 additions & 25 deletions bin/varnisher
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ require 'yaml'
require 'varnisher'

Main {
examples "varnisher purge http://example.com", "varnisher spider example.com", "varnisher purge --reindex example.com"
examples 'varnisher purge http://example.com', 'varnisher spider example.com', 'varnisher purge --reindex example.com'

description "Varnisher is a set of tools for working with the Varnish HTTP cache."
description 'Varnisher is a set of tools for working with the Varnish HTTP cache.'

argument 'target'

Expand All @@ -20,82 +20,83 @@ Main {
}

option('q', 'quiet') {
description "If given, Varnisher will be silent apart from errors."
description 'If given, Varnisher will be silent apart from errors.'
}

option('H', 'hostname') {
argument :required
description "The hostname/IP address of your Varnish server."
description 'The hostname/IP address of your Varnish server.'
}

option('p', 'port') {
argument :required
cast :int
description "The port Varnish is listening on."
description 'The port Varnish is listening on.'
}

option('o', 'output-file') {
argument :required
description "A file to output log information to. If not given, output will be printed to STDOUT"
description 'A file to output log information to. If not given, output will be printed to STDOUT'
}

def before_run
load_config
end

mode "purge" do
argument('target') { description "The URL or hostname to purge" }
mode 'purge' do
argument('target') { description 'The URL or hostname to purge' }

option('reindex') {
description "If you specify a hostname to purge, this option will respider that hostname after the purging is complete. This will keep your cache as warm as possible."
description 'If you specify a hostname to purge, this option will respider that hostname after the purging is complete. This will keep your cache as warm as possible.'
}

def run
target = params['target'].value

# If target is a valid URL, then assume we're purging a page and its contents.
if target =~ /^[a-z]+:\/\//
Varnisher::PagePurger.new target
end

if target =~ %r(^[a-z]+://)
purger = Varnisher::PagePurger.new target
purger.purge
# If target is a hostname, assume we want to purge an entire domain.
if target =~ /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/
elsif target =~ /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/
Varnisher::DomainPurger.new target

if params['reindex'].given?
Varnisher::Spider.new "http://#{target}/"
spider = Varnisher::Spider.new "http://#{target}/"
spider.run
end
end
end
end

mode "spider" do
argument('target') { description "The URL to begin spidering from." }
mode 'spider' do
argument('target') { description 'The URL to begin spidering from.' }

option('n', 'num-pages') {
argument :required
cast :int
description "Maximum number of pages to crawl. Setting this to -1 (the default) will impose no limit."
description 'Maximum number of pages to crawl. Setting this to -1 (the default) will impose no limit.'
}

option('t', 'threads') {
argument :required
cast :int
description "Spidering is done in parallel; this variable controls how many threads will be used."
description 'Spidering is done in parallel; this variable controls how many threads will be used.'
}

option('#', 'ignore-hashes') {
description "When given, /foo#foo and /foo#bar will be treated as separate URLs; the default is to treat them as the same resource."
description 'When given, /foo#foo and /foo#bar will be treated as separate URLs; the default is to treat them as the same resource.'
}

option('q', 'ignore-query-strings') {
description "When given, /foo?foo=bar and /foo?foo=baz will be treated as the same resource."
description 'When given, /foo?foo=bar and /foo?foo=baz will be treated as the same resource.'
}

def run
target = params['target'].value

Varnisher::Spider.new target
spider = Varnisher::Spider.new target
spider.run
end
end

Expand All @@ -105,16 +106,16 @@ Main {

# Check the user's RC file -- if it exists -- to see if they've
# specified any defaults of their own.
rcfile = File.expand_path("~/.varnishrc")
rcfile = File.expand_path('~/.varnishrc')
if FileTest.readable? rcfile
rc = YAML::load(File.open(rcfile))
rc = YAML.load(File.open(rcfile))
options.merge!(rc)
end

# The highest priority is given to command line arguments, so that
# the user can override things that are in their RC file if they
# choose to.
options.merge!(params.to_options.reject { |k,v| v.nil? })
options.merge!(params.to_options.reject { |k, v| v.nil? })

Varnisher.options = options
end
Expand Down
11 changes: 4 additions & 7 deletions lib/varnisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# * {Varnisher::Spider}
# * {Varnisher::DomainPurger}
# * {Varnisher::PagePurger}
module Varnisher
module Varnisher
# Our default options are set here; they can be overriden either by
# command-line arguments or by settings in a user's ~/.varnishrc file.
@options = {
Expand All @@ -32,12 +32,9 @@ def self.options
def self.options=(options)
@options = options

if options['hostname'].nil? and options['target']
begin
uri = URI.parse(options['target'])
options['hostname'] = uri.host
rescue
end
if options['hostname'].nil? && options['target']
uri = URI.parse(options['target'])
options['hostname'] = uri.host
end

start_logging
Expand Down
2 changes: 1 addition & 1 deletion lib/varnisher/domainpurger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DomainPurger
#
# @param domain [String] The hostname to purge
def initialize(domain)
purged = Varnisher::purge(domain, :domain)
purged = Varnisher.purge(domain, :domain)
if purged
Varnisher.log.info "Purged #{domain}"
else
Expand Down
Loading

0 comments on commit 6975bd2

Please sign in to comment.