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.



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

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      => '',
    :daemonize   => false,
    :environment => 'deployment'
  }
end

Instance Method Details

#editoroptsObject

returns dup of @editoropts merged with rcopts



123
124
125
# File 'lib/editserver/command.rb', line 123

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

#optionsObject



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/editserver/command.rb', line 33

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

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

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

    opt.on '-H', '--host HOST', "IP/Hostname to bind to; #{rackopts[:Host]} by default" do |arg|
      @rackopts[:Host] = arg
    end

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

    opt.on '-d', '--default EDITOR', 'Editor to launch at root path; May be one of:',
           (Editserver.new(editoropts).editors.keys - ['default']).join(', ') do |arg|
      @editoropts['default'] = arg
    end

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

    opt.on '-f', '--fork', 'Fork and daemonize; returns pid of daemon' do
      @rackopts[:daemonize] = true
      @rackopts[:pid]       = "/tmp/#{File.basename $0}/#{File.basename $0}.pid"
    end

    opt.on '-q', '--quiet', 'Produce no output' do
      @opts[:quiet]           = true
      @rackopts[:Logger]      = WEBrick::Log.new nil, WEBrick::BasicLog::FATAL - 1 # zero, essentially
      @rackopts[:environment] = 'none'
    end

    opt.on '--rc PATH', "Path to rc file; #{@opts[:rcfile]} by default" 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
      @rcopts          = nil
      @opts[:norcfile] = true
    end

    # normally implicit, but must be explicit when having an option beginning with `h'
    opt.on_tail '-h', '--help' do
      puts opt; exit
    end
  end
end

#rackoptsObject

returns dup of @rackopts masked by rcopts



112
113
114
115
116
117
118
119
120
# File 'lib/editserver/command.rb', line 112

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/editserver/command.rb', line 92

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

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

#runObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/editserver/command.rb', line 135

def run
  options.parse @args
  $0 = 'editserver'

  # Rack::Server issues shutdown on SIGINT only
  trap :TERM do
    trap :TERM, 'DEFAULT'
    Process.kill :INT, $$
  end

  if rackopts[:daemonize]
    FileUtils.mkdir_p File.dirname(rackopts[:pid])
    Process.wait fork { server.start }
    sleep 0.1 until File.exists? rackopts[:pid] and File.read(rackopts[:pid]).to_i > 0

    say host_and_port
    say "Editserver PID: #{fx File.read(rackopts[:pid]), [36,1]}"
  else
    begin
      say banner
      server.start
      say fx("\nGoodbye!", [32,1])
    rescue StandardError => e
      say fx(e.to_s, [31,1])
      exit e.respond_to?(:errno) ? e.errno : 1
    end
  end
end

#say(str) ⇒ Object



88
89
90
# File 'lib/editserver/command.rb', line 88

def say str
  puts str unless @opts[:quiet]
end

#serverObject



127
128
129
130
131
132
133
# File 'lib/editserver/command.rb', line 127

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