Sha256: 1c456df1fe931f9e142d9972bed97a1883ad5e46cec742557cdb2ba9ab22aa6c

Contents?: true

Size: 1.14 KB

Versions: 28

Compression:

Stored size: 1.14 KB

Contents

# encoding: UTF-8
require 'ostruct'
require 'optparse'
require 'term/ansicolor'
require 'git-whistles/logger'


module Git::Whistles
  class App

    def initialize
      @options = OpenStruct.new(defaults)
      @log = Git::Whistles::Logger.new($stderr)
    end


    def main(args)
      parse_args!(args)
    end


    def self.run!
      new.main(ARGV)
    end


    protected

    # default options hash
    def defaults
      {}
    end

    attr :options
    attr :log


    def option_parser
      @option_parser ||= OptionParser.new do |op|
        op.banner = "Usage: #{$0}"
      end
    end



    def parse_args!(args)
      begin
        option_parser.parse!(args)
      rescue OptionParser::InvalidOption => error
        die error.message, :usage => true
      end
    end


    def run(command)
      %x(#{command})
    end


    def run!(command)
      result = %x(#{command})
      return result if $? == 0
      die "command '#{command}' failed"
    end


    def die(message, options = {})
      puts Term::ANSIColor.red(message)
      if options[:usage]
        puts 
        puts option_parser.help
      end
      exit 1
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
git-whistles-1.4.2 lib/git-whistles/app.rb
git-whistles-1.4 lib/git-whistles/app.rb
git-whistles-1.3 lib/git-whistles/app.rb
git-whistles-1.2.1 lib/git-whistles/app.rb
git-whistles-1.2.0 lib/git-whistles/app.rb
git-whistles-1.1.3 lib/git-whistles/app.rb
git-whistles-1.1.2 lib/git-whistles/app.rb
git-whistles-1.1.1 lib/git-whistles/app.rb
git-whistles-1.1.0 lib/git-whistles/app.rb
git-whistles-1.0.2 lib/git-whistles/app.rb
git-whistles-1.0.1 lib/git-whistles/app.rb
git-whistles-0.12.0 lib/git-whistles/app.rb
git-whistles-0.10.0 lib/git-whistles/app.rb
git-whistles-0.9.1 lib/git-whistles/app.rb
git-whistles-0.9.0 lib/git-whistles/app.rb
git-whistles-0.8.2 lib/git-whistles/app.rb
git-whistles-0.8.1 lib/git-whistles/app.rb
git-whistles-0.8.0 lib/git-whistles/app.rb
git-whistles-0.7.7 lib/git-whistles/app.rb
git-whistles-0.7.6 lib/git-whistles/app.rb