Sha256: af5727378d8fd705ea67c4e04b648f41b20443ed2ab73413e8d5a23ef2666421

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

require 'pullr/local_repository'
require 'pullr/remote_repository'

require 'optparse'

module Pullr
  class CLI

    #
    # Initializes the Command Line Interface (CLI).
    #
    def initialize
      @scm = nil
      @uri = nil
      @path = nil
      @mode = :clone
      @args = []
    end

    #
    # Runs the Command Line Interface (CLI).
    #
    def CLI.run
      self.new.run(*ARGV)
    end

    #
    # Runs the Command Line Interface (CLI) with the given arguments.
    #
    # @param [Array<String>] args
    #   Arguments to run the CLI with.
    #
    def run(*args)
      optparse(*args)

      case @mode
      when :clone
        @uri ||= @args[0]

        unless @uri
          STDERR.puts "pullr: missing the URI argument"
          exit -1
        end

        repo = RemoteRepository.new(
          :uri => @uri,
          :scm => @scm
        )

        repo.pull(@path || @args[1])
      when :update
        repo = LocalRepository.new(
          :uri => @uri,
          :path => @path,
          :scm => @scm
        )

        repo.update
      end
    end

    protected

    #
    # Parses the given arguments.
    #
    # @param [Array<String>] args
    #   The command-line arguments.
    #
    def optparse(*args)
      opts = OptionParser.new

      opts.banner = 'usage: pullr URI [PATH]'

      opts.on('-S','--scm NAME','Source Code Management to use') do |scm|
        @scm = scm
      end

      opts.on('-U','--uri URI','The URI of the repository') do |uri|
        @uri = uri
      end

      opts.on('-u','--update [PATH]','Update the repository') do |path|
        @mode = :update
        @path = (path || Dir.pwd)
      end

      begin
        @args = opts.parse!(args)
      rescue OptionParser::InvalidOption => e
        STDERR.puts e.message
        STDERR.puts opts
        exit -1
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pullr-0.1.3 lib/pullr/cli.rb
pullr-0.1.2 lib/pullr/cli.rb
pullr-0.1.1 lib/pullr/cli.rb