Permalink
Browse files

Help text for 'add'

  • Loading branch information...
1 parent 7e6231e commit 1896695d402020cf0cbc530c6fb002e0d7bfd82c @bjeanes committed Apr 23, 2012
Showing with 107 additions and 43 deletions.
  1. +1 −0 Rakefile
  2. +14 −10 ghost.gemspec
  3. +10 −0 lib/ghost/cli/task.rb
  4. +15 −0 lib/ghost/cli/task/add.rb
  5. +19 −2 lib/ghost/cli/task/help.rb
  6. +48 −31 spec/ghost/cli_spec.rb
View
1 Rakefile
@@ -28,6 +28,7 @@ task :gemspec do
s.executables += %w[ghost]
s.has_rdoc = false
+ s.add_dependency 'unindent', '1.0'
s.add_development_dependency "rspec", "2.9.0"
s.add_development_dependency "rake", "0.9.2.2"
end
View
24 ghost.gemspec
@@ -1,34 +1,38 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
- s.name = "ghost"
+ s.name = %q{ghost}
s.version = "1.0.0.pre"
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["Bodaniel Jeanes"]
- s.date = "2012-04-19"
- s.description = "Allows you to create, list, and modify local hostnames on POSIX systems (e.g. Mac OS X and Linux) and Windows"
- s.email = "me@bjeanes.com"
+ s.date = %q{2012-04-22}
+ s.default_executable = %q{ghost}
+ s.description = %q{Allows you to create, list, and modify local hostnames on POSIX systems (e.g. Mac OS X and Linux) and Windows}
+ s.email = %q{me@bjeanes.com}
s.executables = ["ghost"]
- s.files = ["LICENSE", "README.md", "bin/ghost", "lib/ghost", "lib/ghost/cli", "lib/ghost/cli/add.rb", "lib/ghost/cli/task", "lib/ghost/cli/task/add.rb", "lib/ghost/cli/task/empty.rb", "lib/ghost/cli/task/export.rb", "lib/ghost/cli/task/import.rb", "lib/ghost/cli/task/list.rb", "lib/ghost/cli/task.rb", "lib/ghost/cli.rb", "lib/ghost/host.rb", "lib/ghost/store", "lib/ghost/store/dscl_store.rb", "lib/ghost/store/hosts_file_store.rb", "lib/ghost/store.rb", "lib/ghost/version.rb", "lib/ghost.rb", "spec/ghost", "spec/ghost/cli_spec.rb", "spec/ghost/host_spec.rb", "spec/ghost/store", "spec/ghost/store/dscl_store_spec.rb", "spec/ghost/store/hosts_file_store_spec.rb", "spec/ghost/store_spec.rb", "spec/spec_helper.rb"]
- s.homepage = "http://github.com/bjeanes/ghost"
+ s.files = ["LICENSE", "README.md", "bin/ghost", "lib/ghost", "lib/ghost/cli", "lib/ghost/cli/task", "lib/ghost/cli/task/add.rb", "lib/ghost/cli/task/delete.rb", "lib/ghost/cli/task/empty.rb", "lib/ghost/cli/task/export.rb", "lib/ghost/cli/task/help.rb", "lib/ghost/cli/task/import.rb", "lib/ghost/cli/task/list.rb", "lib/ghost/cli/task.rb", "lib/ghost/cli.rb", "lib/ghost/host.rb", "lib/ghost/store", "lib/ghost/store/dscl_store.rb", "lib/ghost/store/hosts_file_store.rb", "lib/ghost/store.rb", "lib/ghost/tokenized_file.rb", "lib/ghost/version.rb", "lib/ghost.rb", "spec/ghost", "spec/ghost/cli_spec.rb", "spec/ghost/host_spec.rb", "spec/ghost/store", "spec/ghost/store/dscl_store_spec.rb", "spec/ghost/store/hosts_file_store_spec.rb", "spec/ghost/store_spec.rb", "spec/ghost/tokenized_file_spec.rb", "spec/spec_helper.rb"]
+ s.homepage = %q{http://github.com/bjeanes/ghost}
s.require_paths = ["lib"]
- s.rubyforge_project = "ghost"
- s.rubygems_version = "1.8.10"
- s.summary = "Allows you to create, list, and modify local hostnames"
- s.test_files = ["spec/ghost", "spec/ghost/cli_spec.rb", "spec/ghost/host_spec.rb", "spec/ghost/store", "spec/ghost/store/dscl_store_spec.rb", "spec/ghost/store/hosts_file_store_spec.rb", "spec/ghost/store_spec.rb", "spec/spec_helper.rb"]
+ s.rubyforge_project = %q{ghost}
+ s.rubygems_version = %q{1.6.2}
+ s.summary = %q{Allows you to create, list, and modify local hostnames}
+ s.test_files = ["spec/ghost", "spec/ghost/cli_spec.rb", "spec/ghost/host_spec.rb", "spec/ghost/store", "spec/ghost/store/dscl_store_spec.rb", "spec/ghost/store/hosts_file_store_spec.rb", "spec/ghost/store_spec.rb", "spec/ghost/tokenized_file_spec.rb", "spec/spec_helper.rb"]
if s.respond_to? :specification_version then
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<unindent>, ["= 1.0"])
s.add_development_dependency(%q<rspec>, ["= 2.9.0"])
s.add_development_dependency(%q<rake>, ["= 0.9.2.2"])
else
+ s.add_dependency(%q<unindent>, ["= 1.0"])
s.add_dependency(%q<rspec>, ["= 2.9.0"])
s.add_dependency(%q<rake>, ["= 0.9.2.2"])
end
else
+ s.add_dependency(%q<unindent>, ["= 1.0"])
s.add_dependency(%q<rspec>, ["= 2.9.0"])
s.add_dependency(%q<rake>, ["= 0.9.2.2"])
end
View
10 lib/ghost/cli/task.rb
@@ -1,3 +1,5 @@
+require 'unindent'
+
module Ghost
class Cli
class Task
@@ -13,6 +15,14 @@ def desc(str = nil)
@desc
end
end
+
+ def help
+ if block_given?
+ @help = yield.unindent
+ else
+ @help
+ end
+ end
end
def initialize(out)
View
15 lib/ghost/cli/task/add.rb
@@ -7,4 +7,19 @@ def perform(host, ip = nil)
rescue Ghost::Host::NotResolvable
abort "Unable to resolve IP address for target host #{ip.inspect}."
end
+
+ help do
+ <<-EOF.unindent
+ Usage: ghost add <local host name> [<remote host name>|<IP address>]
+
+ #{desc}
+
+ If a second parameter is not provided, it defaults to 127.0.0.1
+
+ Examples:
+ ghost add my-localhost # points to 127.0.0.1
+ ghost add google.dev google.com # points to the IP of google.com
+ ghost add router 192.168.1.1 # points to 192.168.1.1
+ EOF
+ end
end
View
21 lib/ghost/cli/task/help.rb
@@ -1,5 +1,17 @@
Ghost::Cli.task :help, nil do
def perform(task=nil)
+ return overview unless task
+
+ task = tasks_by_name[task]
+ abort "No such task #{task}" unless task
+ abort "No help for task #{task}" unless task.help
+
+ puts task.help
+ end
+
+ private
+
+ def overview
puts "USAGE: ghost <task> [<args>]"
puts ""
puts "The ghost tasks are:"
@@ -12,10 +24,15 @@ def perform(task=nil)
puts "See 'ghost help <task>' for more information on a specific task."
end
- private
+ def tasks_by_name
+ Ghost::Cli.tasks
+ end
+
+ def tasks
+ tasks_by_name.values.uniq
+ end
def tasks_to_show
- tasks = Ghost::Cli.tasks.values.uniq
size = tasks.map { |t| t.name.length }.max
tasks.sort_by(&:name).each do |task|
next unless task.desc
View
79 spec/ghost/cli_spec.rb
@@ -12,10 +12,10 @@ def ghost(args)
out = StringIO.new
Ghost::Cli.new(out).parse(args.split(/\s+/))
out.rewind
- out.read.chomp
+ out.read
rescue SystemExit
out.rewind
- out.read.chomp
+ out.read
end
let(:store_path) { File.join(Dir.tmpdir, "etc_hosts.#{Process.pid}.#{rand(9999)}") }
@@ -28,19 +28,19 @@ def ghost(args)
describe "help" do
let(:overview) do
- """
- USAGE: ghost <task> [<args>]
-
- The ghost tasks are:
- add Add a host
- delete Remove a ghost-managed host
- empty Clear all ghost-managed hosts
- export Export all hosts in /etc/hosts format
- import Import hosts in /etc/hosts format
- list Show all (or a filtered) list of hosts
-
- See 'ghost help <task>' for more information on a specific task.
- """.gsub(/^ {6}/,'').strip
+ <<-EOF.unindent
+ USAGE: ghost <task> [<args>]
+
+ The ghost tasks are:
+ add Add a host
+ delete Remove a ghost-managed host
+ empty Clear all ghost-managed hosts
+ export Export all hosts in /etc/hosts format
+ import Import hosts in /etc/hosts format
+ list Show all (or a filtered) list of hosts
+
+ See 'ghost help <task>' for more information on a specific task.
+ EOF
end
it 'displays help overview when called with no args' do
@@ -50,14 +50,31 @@ def ghost(args)
it 'displays help overview when help task is called with no arguments' do
ghost("help").should == overview
end
+
+ describe "add" do
+ specify do
+ ghost("help add").should == <<-EOF.unindent
+ Usage: ghost add <local host name> [<remote host name>|<IP address>]
+
+ Add a host
+
+ If a second parameter is not provided, it defaults to 127.0.0.1
+
+ Examples:
+ ghost add my-localhost # points to 127.0.0.1
+ ghost add google.dev google.com # points to the IP of google.com
+ ghost add router 192.168.1.1 # points to 192.168.1.1
+ EOF
+ end
+ end
end
describe "environment configuration" # via GHOST_OPTS (see OptionParser#environment)
describe "--version" do
it "outputs the gem name and version" do
- ghost("--version").should == "ghost #{Ghost::VERSION}"
- ghost("-v").should == "ghost #{Ghost::VERSION}"
+ ghost("--version").should == "ghost #{Ghost::VERSION}\n"
+ ghost("-v").should == "ghost #{Ghost::VERSION}\n"
end
end
@@ -71,7 +88,7 @@ def ghost(args)
end
it "outputs a summary of the operation" do
- ghost("add my-app.local").should == "[Adding] my-app.local -> 127.0.0.1"
+ ghost("add my-app.local").should == "[Adding] my-app.local -> 127.0.0.1\n"
end
context "when an entry for that hostname already exists" do
@@ -87,7 +104,7 @@ def ghost(args)
end
it "outputs a summary of the operation" do
- ghost("add my-app.local 192.168.1.1").should == "[Adding] my-app.local -> 192.168.1.1"
+ ghost("add my-app.local 192.168.1.1").should == "[Adding] my-app.local -> 192.168.1.1\n"
end
end
@@ -103,14 +120,14 @@ def ghost(args)
it "outputs a summary of the operation" do
ghost("add my-app.local google.com").should ==
- "[Adding] my-app.local -> 74.125.225.99"
+ "[Adding] my-app.local -> 74.125.225.99\n"
end
context "when the remote hostname can not be resolved" do
before { Ghost::Host.stub(:new).and_raise(Ghost::Host::NotResolvable) }
it "outputs an error message" do
- ghost("add my-app.local google.com").should == "Unable to resolve IP address for target host \"google.com\"."
+ ghost("add my-app.local google.com").should == "Unable to resolve IP address for target host \"google.com\".\n"
end
end
end
@@ -149,11 +166,11 @@ def ghost(args)
context "with no filtering parameter" do
it "outputs all hostnames" do
- ghost("list").should == %"
+ ghost("list").should == <<-EOF.unindent
Listing 2 host(s):
gist.github.com -> 10.0.0.1
google.com -> 192.168.1.10
- ".gsub(/^\s{10}/,'').strip.chomp
+ EOF
end
end
@@ -167,21 +184,21 @@ def ghost(args)
context 'that is a regex' do
it "outputs entries whose hostname or IP match the filter" do
- ghost("list /\.com$/").should == %"
+ ghost("list /\.com$/").should == <<-EOF.unindent
Listing 2 host(s):
google.com -> 10.0.0.1
gmail.com -> 127.0.0.1
- ".gsub(/^\s{12}/,'').strip.chomp
+ EOF
end
end
context 'that is a string' do
it "outputs entries whose hostname or IP match the filter" do
- ghost("list google").should == %"
+ ghost("list google").should == <<-EOF.unindent
Listing 2 host(s):
google.com -> 10.0.0.1
google.co.uk -> 192.168.1.10
- ".gsub(/^\s{12}/,'').strip.chomp
+ EOF
end
end
end
@@ -198,7 +215,7 @@ def ghost(args)
end
it 'outputs a summary of the operation' do
- ghost("empty").should == "[Emptying] Done."
+ ghost("empty").should == "[Emptying] Done.\n"
end
end
@@ -207,7 +224,7 @@ def ghost(args)
store.add Ghost::Host.new("gist.github.com", "10.0.0.1")
store.add Ghost::Host.new("google.com", "192.168.1.10")
- ghost("export").should == <<-EOE.gsub(/^\s+/,'').chomp
+ ghost("export").should == <<-EOE.unindent
10.0.0.1 gist.github.com
192.168.1.10 google.com
EOE
@@ -217,7 +234,7 @@ def ghost(args)
describe "import" do
context "with current export format" do
let(:import) do
- <<-EOI.gsub(/^\s+/,'').chomp
+ <<-EOI.unindent
1.2.3.4 foo.com
2.3.4.5 bar.com
EOI
@@ -257,7 +274,7 @@ def ghost(args)
context 'with multiple hosts per line' do
let(:import) do
- <<-EOI.gsub(/^\s+/,'').chomp
+ <<-EOI.unindent
1.2.3.4 foo.com
2.3.4.5 bar.com subdomain.bar.com
EOI

0 comments on commit 1896695

Please sign in to comment.