Skip to content

Commit

Permalink
Merge pull request #21 from DannyBen/patch
Browse files Browse the repository at this point in the history
Add ability to --index --and-quit
  • Loading branch information
DannyBen authored Jul 27, 2016
2 parents a566169 + 0219d86 commit ba41689
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ bind: '0.0.0.0'
autoh1: true
highlighter: true
line_numbers: true
index: false
```
Expand Down
23 changes: 9 additions & 14 deletions lib/madness/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ class CommandLine
include Singleton
include Colsole

# Without any argument, we run the server in the current directory.
# Otherwise, we will set some options before executing.
# Launch the server
def execute(argv=[])
if argv.empty?
launch_server
else
launch_server_with_options argv
end
launch_server_with_options argv
end

private
Expand All @@ -24,11 +19,10 @@ def launch_server_with_options(argv)
begin
args = Docopt::docopt(doc, argv: argv, version: VERSION)
set_config args
if args['--index']
build_index
else
launch_server
end

build_index if config.index
launch_server unless args['--and-quit']

rescue Docopt::Exit => e
puts e.message
end
Expand All @@ -42,7 +36,7 @@ def launch_server
STDERR.puts "Invalid path (#{config.path})"
return
end

show_status
Server.prepare
Server.run!
Expand All @@ -57,14 +51,15 @@ def set_config(args)
config.autoh1 = false if args['--no-auto-h1']
config.highlighter = false if args['--no-syntax']
config.line_numbers = false if args['--no-line-numbers']
config.index = true if args['--index']
end

# Say hello to everybody when the server starts, showing the known
# config.
def show_status
say_status :start, 'the madness'
say_status :listen, "#{config.bind}:#{config.port}", :txtblu
say_status :path, config.path, :txtblu
say_status :path, File.realpath(config.path), :txtblu
say_status :use, config.filename if config.file_exist?
say "-" * 40
end
Expand Down
4 changes: 4 additions & 0 deletions lib/madness/docopt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ Options:
--index
Build or rebuild the index for the search page.

--and-quit
Quit after building the index (applicable only with --index).

Examples:
madness
madness docs
madness docs --no-auto-h1 -p 4567
madness --index --and-quit

3 changes: 2 additions & 1 deletion lib/madness/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Settings
include Singleton

attr_accessor :port, :bind, :path, :autoh1,
:highlighter, :line_numbers
:highlighter, :line_numbers, :index

def initialize
reset
Expand Down Expand Up @@ -41,6 +41,7 @@ def set_defaults
self.autoh1 = true
self.highlighter = true
self.line_numbers = true
self.index = false
end

def load_from_file
Expand Down
3 changes: 2 additions & 1 deletion spec/fixtures/.madness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ port: '1337'
bind: '4.3.2.1'
autoh1: false
highlighter: false
line_numbers: false
line_numbers: false
index: true
11 changes: 10 additions & 1 deletion spec/madness/command_line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@
end

context "with --index" do
it "calls the index builder" do
it "calls the index builder and starts the server" do
expect(Server).to receive :run!
expect_any_instance_of(Search).to receive :build_index
command = %w[--index]
expect {cli.execute command}.to output(/done.*indexing.*start.*the madness/m).to_stdout
end
end

context "with --index --and-quit" do
it "calls the index builder" do
expect_any_instance_of(Search).to receive :build_index
command = %w[--index --and-quit]
expect {cli.execute command}.to output(/done.*indexing/).to_stdout
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/madness/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
expect(config.autoh1).to be false
expect(config.highlighter).to be false
expect(config.line_numbers).to be false
expect(config.index).to be true
end
end

Expand Down

0 comments on commit ba41689

Please sign in to comment.