Sha256: 0683ddb66a471e69c9f52c8a4d19980e860b7af9c409d7f44b016e6ba431d6cc

Contents?: true

Size: 1.76 KB

Versions: 15

Compression:

Stored size: 1.76 KB

Contents

# encoding: utf-8

require 'liquid/boot'
require 'mixlib/cli'

class CLI
  include Mixlib::CLI

  option :configfile,
    short: '-c FILE',
    long: '--config FILE',
    description: 'Configuration File to load'

  option :name,
    :short => '-n NAME',
    :long => '--name NAME',
    :description => 'Process name',
    :default => File.basename($0),
    :proc => ->(value) { $0 = value }

  option :environment,
    :short => '-e ENVIRONMENT',
    :long => '--environment ENVIRONMENT',
    :description => "Set the daemon environment",
    :default => "development",
    :proc => ->(value) { Env.set(value) }

  option :debug,
    :short => '-D',
    :long => '--debug',
    :description => "Enable debug output",
    :boolean => true,
    :default => false,
    :proc => ->(value) { $conf.mixin(log_level: value ? :debug : :info); value }

  option :help,
    :short => '-h',
    :long => '--help',
    :description => "Show this message",
    :on => :tail,
    :boolean => true,
    :show_options => true,
    :exit => 0

  def option(name, args)
    args[:on] ||= :on
    args[:boolean] ||= false
    args[:required] ||= false
    args[:proc] ||= nil
    args[:show_options] ||= false
    args[:exit] ||= nil

    if args.has_key?(:default)
      config[name.to_sym] = args[:default]
    end

    options[name.to_sym] = args
  end

  def self.for(cls, &block)
    cli = new
    cli.instance_eval(&block) if block_given?
    cli.parse_options

    $log.info("cli:initialize", cli.config)
    $conf.reload!

    # infer some variables
    opts = cli.config.merge({
      fqdn: Socket.gethostbyname(Socket.gethostname).first
    })

    # sneak in opts without subclassing
    cls.allocate.tap do |obj|
      obj.define_singleton_method(:opts) { opts }
      obj.send(:initialize)
    end
  end

end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
liquid-ext-2.0.3 lib/liquid/cli.rb
liquid-ext-2.0.2 lib/liquid/cli.rb
liquid-ext-2.0.1 lib/liquid/cli.rb
liquid-ext-2.0.0 lib/liquid/cli.rb
liquid-ext-1.2.6 lib/liquid/cli.rb
liquid-ext-1.2.5 lib/liquid/cli.rb
liquid-ext-1.2.4 lib/liquid/cli.rb
liquid-ext-1.2.2 lib/liquid/cli.rb
liquid-ext-1.2.1 lib/liquid/cli.rb
liquid-ext-1.2.0 lib/liquid/cli.rb
liquid-ext-1.1.1 lib/liquid/cli.rb
liquid-ext-1.1.0 lib/liquid/cli.rb
liquid-ext-1.0.2 lib/liquid/cli.rb
liquid-ext-1.0.1 lib/liquid/cli.rb
liquid-ext-1.0.0 lib/liquid/cli.rb