Sha256: 54150c0a87590c959e51325b0f23ee3ea1972d11bb307f028befb098dd22cba7

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

require 'internet'
require 'views/cli'
require 'use_cases/new'
require 'use_cases/list'
require 'settings_file'

module Idonethis::Adapters
  class Cli
    class << self
      def run(argv={})
        args = parse(argv)
      
        command,*rest = argv

        log = choose_log(args) 
        
        args.merge!({ opts: rest, log: log, internet: Idonethis::Adapters::Internet, view: Idonethis::Adapters::Views::Cli::List.method(:apply)})
        credential = Settings.credential 

        log.call "args: #{args}, command: #{command}, rest: #{rest}"
        
        use_cases = {
          list:   Idonethis::UseCases::List.method(:apply),
          new:    Idonethis::UseCases::New.method(:apply),
          config: Idonethis::UseCases::Config.method(:apply),
          git:    Idonethis::UseCases::Git.method(:apply),
          help:   ->(credential, args) { puts "TODO: implement help" }
        }

        use_case = use_cases[command.to_sym]

        unless use_case
          puts "No command <#{command.to_sym}> found"
          return
        end

        use_case.call credential, args
      end
      
      private 
      
      def choose_log(args={})
        args[:verbose] == true ? ->(msg){puts "[LOG] #{msg}"} : ->(_){}
      end 
      
      def parse(argv={})
        args = {}

        require 'optparse'
        
        OptionParser.new do |opts|
          opts.banner = "Usage: command [options]"

          opts.on("-v", "--verbose", "Run verbosely") do |v|
            args[:verbose] = v
          end

          opts.on("-m MESSAGE", "Message") do |m|
            args[:message] = m
          end

          opts.on("-d", "Dry run") do |_|
            args[:dry_run] = true
          end
        end.parse!
        
        args
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
idonethis-cli-0.6.0 lib/adapters/cli.rb