Class: Editserver::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/editserver/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ Command

Returns a new instance of Command.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/editserver/command.rb', line 9

def initialize args = []
  @args = args
  @opts = { :rcfile => "~/.editserverrc" }

  # keys are strings because YAML.load returns string keys,
  # and we are not restricting the keys like @rackopts
  @editoropts = {
    'default'  => nil,
    'terminal' => nil
  }

  @rackopts = {
    :Host        => '127.0.0.1',
    :Port        => 9999,
    :Logger      => WEBrick::Log.new(nil, WEBrick::BasicLog::WARN), # be less chatty
    :AccessLog   => [], # rack does its own access logging, so keep this blank
    :pid         => nil,
    :config      => '',
    :environment => 'deployment'
  }
end

Instance Method Details

#editoroptsObject

returns dup of @editoropts merged with rcopts



92
93
94
# File 'lib/editserver/command.rb', line 92

def editoropts
  @editoropts.dup.merge rcopts['editor']
end

#optionsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/editserver/command.rb', line 31

def options
  OptionParser.new do |opt|
    opt.summary_width = 20

    opt.banner = %Q(\
      Usage: #{File.basename $0} [options]

      Options:
    ).gsub /^ +/, ''

    opt.on '-p', '--port NUMBER', Integer, "default: #{rackopts[:Port]}" do |arg|
      @rackopts[:Port] = arg
    end

    opt.on '-t', '--terminal CMD', 'Terminal to launch for console editors' do |arg|
      @editoropts['terminal'] = arg
    end

    opt.on '--rc PATH', "Path to rc file; #{@opts[:rcfile]} by default",
           '(Also can be set by exporting EDITSERVERRC to environment)' do |arg|
      @rcopts = nil # reset cached user opts
      @opts[:rcfile] = File.expand_path arg
    end

    opt.on '--no-rc', 'Suppress reading of rc file' do
      @opts[:norcfile] = true
    end
  end
end

#rackoptsObject

returns dup of @rackopts masked by rcopts



81
82
83
84
85
86
87
88
89
# File 'lib/editserver/command.rb', line 81

def rackopts
  (opts = @rackopts.dup).keys.each do |k|
    v = rcopts['rack'][k.to_s]
    v = rcopts['rack'][k.to_s.downcase] if v.nil? # be tolerant of lowercase keys
    opts[k] = v unless v.nil?
  end

  opts
end

#rcoptsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/editserver/command.rb', line 61

def rcopts
  @rcopts ||= begin
    empty  = { 'rack' => {}, 'editor' => {} }
    rcfile = File.expand_path ENV['EDITSERVERRC'] || @opts[:rcfile]

    if @opts[:norcfile]
      empty
    elsif File.exists? rcfile
      opts = YAML.load_file File.expand_path(rcfile)
      opts           ||= {}
      opts['rack']   ||= {}
      opts['editor'] ||= {}
      opts
    else
      empty
    end
  end
end

#runObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/editserver/command.rb', line 104

def run
  options.parse @args

  begin
    $0 = 'editserver'
    puts banner
    server.start
  ensure
    puts fx("\nGoodbye!", [32,1])
  end
end

#serverObject



96
97
98
99
100
101
102
# File 'lib/editserver/command.rb', line 96

def server
  # HACK: Fixed in master -- remove when upgrading min rack dependency
  # http://groups.google.com/group/rack-devel/browse_thread/thread/8f6c3b79c99809ee
  srv = Rack::Server.new rackopts
  srv.instance_variable_set :@app, Editserver.new(editoropts)
  srv
end