koraktor/gallerist

View on GitHub
bin/gallerist

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
#
# This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
# Copyright (c) 2015, Sebastian Staudt

require 'optparse'
require 'rack'

require 'gallerist'

ENV['RACK_ENV'] ||= 'production'

opts = OptionParser.new do |opts|
  opts.banner = 'Usage: gallerist [options] <library>'

  opts.separator ''
  opts.separator 'Options:'
  opts.on('-p', '--port PORT', 'Specify on which port to listen (Default is 9292)') do |port|
    Gallerist.options[:port] = port
  end

  opts.separator ''
  opts.separator 'Arguments:'
  opts.separator opts.summary_indent + '<library> The path to the photo library to open.'

  opts.separator ''
  opts.separator 'Advanced options:'
  opts.on('-d', '--debug', 'Enable debug mode') do
    ENV['RACK_ENV'] = 'development'
  end
  opts.on('-l', '--log-dir DIR') do |log_dir|
    Gallerist.options[:log_dir] = log_dir
  end
  opts.on('-n', '--no-copy', 'Disable temporary copies of the library databases') do
    Gallerist.options[:nocopy] = 'true'
  end
  opts.on('-v', '--verbose', 'Enable verbose output') do
    ENV['VERBOSE'] = 'true'
  end
end
opts.parse!

config_path = File.expand_path File.join(__dir__, '..', 'config.ru')

Gallerist.options[:library] = File.expand_path ARGV[0]

if Gallerist.options[:library].nil?
  puts opts.help
  exit 1
end

begin
  Gallerist::Logging.prepare

  Rack::Server.start config: config_path,
                     logger: Gallerist::Logging.server_logger,
                     server: :puma,
                     Port: Gallerist.options[:port],
                     Silent: true
rescue Gallerist::LibraryInUseError
  $stderr.puts 'The library is currently in use.' <<
               ' Is it currently opened in Photos?'
rescue Gallerist::LibraryNonExistant
  $stderr.puts 'There’s no library at "%s".' % [ Gallerist.options[:library] ] <<
               ' Is the path correct?'
rescue Gallerist::LoggingInitializationError
  $stderr.puts 'Failed to initialize logging: %s' % [ $!.short_message ]
rescue Errno::EADDRINUSE
  $stderr.puts ('Port %d is already in use.' % [ Gallerist.options[:port] ]) <<
                ' Maybe another instance of Gallerist is already running.' <<
                ' (You may use a different port using the --port option.)'
end