Sha256: 044d557fd38da3338556c3bffd1149fd1bc9b14ea39ededb5037487866c2aeb3

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

require 'optparse'
require_relative '../alerty'

class Alerty
  class CLI
    def parse_options(argv = ARGV)
      op = OptionParser.new
      op.banner += ' -- command'

      (class<<self;self;end).module_eval do
        define_method(:usage) do |msg|
          puts op.to_s
          puts "error: #{msg}" if msg
          exit 1
        end
      end

      opts = {}
      op.on('-c', '--config CONFIG_FILE', "config file path (default: /etc/alerty/alerty.yml)") {|v|
        opts[:config_path] = v
      }
      op.on('--log LOG_FILE', "log file path (default: STDOUT)") {|v|
        opts[:log_path] = v
      }
      op.on('--log-level LOG_LEVEL', "log level (default: warn)") {|v|
        opts[:log_level] = v
      }
      op.on('-t', '--timeout SECONDS', "timeout the command (default: no timeout)") {|v|
        opts[:timeout] = v.to_i
      }
      op.on('-l', '--lock LOCK_FILE', "exclusive lock file to prevent running a command duplicatedly (default: no lock)") {|v|
        opts[:lock_path] = v
      }

      op.parse!(argv)
      opts[:command] = argv.join(' ')

      if opts[:command].empty?
        raise OptionParser::InvalidOption.new("No command is given")
      end

      opts
    end

    def run
      begin
        opts = parse_options
      rescue OptionParser::InvalidOption => e
        usage e.message
      end
  
      Config.configure(opts)
      Config.plugins # load plugins in early stage
      command = Command.new(command: opts[:command])
      command.run!
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alerty-0.0.6 lib/alerty/cli.rb
alerty-0.0.5 lib/alerty/cli.rb