Module: Mongrel2::CLI::CommandUtilities

Defined in:
lib/mongrel2/cli.rb

Overview

Functions for common command tasks

Instance Method Summary collapse

Instance Method Details

#edit(filename) ⇒ Object

Invoke the user’s editor on the given filename and return the exit code from doing so.



445
446
447
448
449
450
451
# File 'lib/mongrel2/cli.rb', line 445

def edit( filename )
  editor = ENV['EDITOR'] || ENV['VISUAL'] || DEFAULT_EDITOR
  system editor, filename.to_s
  unless $?.success? || editor =~ /vim/i
    raise "Editor exited with an error status (%d)" % [ $?.exitstatus ]
  end
end

#find_mongrel2Object

Search the PATH for a mongrel2 binary, returning the absolute Pathname to it if found, and outputting a warning and describing how to set ENV if not.



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/mongrel2/cli.rb', line 456

def find_mongrel2
  if ENV['MONGREL2']
    m2 = Pathname( ENV['MONGREL2'] )
    error = nil
    if !m2.file?
      error = "but it isn't a plain file."
    elsif !m2.executable?
      error = "but it isn't executable."
    end

    raise "MONGREL2 was set to #{m2}, #{error}" if error

    return m2
  else
    m2 = ENV['PATH'].split( File::PATH_SEPARATOR ).
      map {|dir| Pathname(dir) + 'mongrel2' }.
      find {|path| path.executable? }

    return m2 if m2

    raise "The 'mongrel2' binary doesn't seem to be in your PATH. Either " +
      "add the appropriate directory to your PATH or set the MONGREL2 " +
      "environment variable to the full path."
  end

end

#find_server(serverspec = nil) ⇒ Object

Search the current mongrel2 config for a server matching serverspec and return it as a Mongrel2::Config::Server object.



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/mongrel2/cli.rb', line 385

def find_server( serverspec=nil )
  server = nil
  servers = Mongrel2::Config.servers

  raise "No servers are configured." if servers.empty?

  # If there's only one configured server, just make sure if a serverspec was given
  # that it would have matched.
  if servers.length == 1
    server = servers.first if !serverspec ||
      servers.first.values.values_at( :uuid, :default_host, :name ).include?( serverspec )

  # Otherwise, require an argument and search for the desired server if there is one
  else
    raise "You must specify a server uuid/hostname/name when more " +
          "than one server is configured." if servers.length > 1 && !serverspec

    server = servers.find {|s| s.uuid == serverspec } ||
             servers.find {|s| s.default_host == serverspec } ||
             servers.find {|s| s.name == serverspec }
  end

  raise "No servers match '#{serverspec}'" unless server

  return server
end

#read_historyObject

Read command line history from HISTORY_FILE



414
415
416
417
418
419
420
421
422
423
424
# File 'lib/mongrel2/cli.rb', line 414

def read_history
  histfile = HISTORY_FILE.expand_path

  if histfile.exist?
    lines = histfile.readlines.collect {|line| line.chomp }
    self.log.debug "Read %d saved history commands from %s." % [ lines.length, histfile ]
    Readline::HISTORY.push( *lines )
  else
    self.log.debug "History file '%s' was empty or non-existant." % [ histfile ]
  end
end

#save_historyObject

Save command line history to HISTORY_FILE



428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/mongrel2/cli.rb', line 428

def save_history
  histfile = HISTORY_FILE.expand_path

  lines = Readline::HISTORY.to_a.reverse.uniq.reverse
  lines = lines[ -DEFAULT_HISTORY_SIZE, DEFAULT_HISTORY_SIZE ] if
    lines.length > DEFAULT_HISTORY_SIZE

  self.log.debug "Saving %d history lines to %s." % [ lines.length, histfile ]

  histfile.open( File::WRONLY|File::CREAT|File::TRUNC ) do |ofh|
    ofh.puts( *lines )
  end
end