Sha256: 67db463b8b860b11b75c507b95c59dc7626e4f82d8fc30aeef407521c4c6c021

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 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
        
        args.merge!({ opts: rest, log: log, internet: Idonethis::Adapters::Internet, view: Idonethis::Adapters::Views::Cli::List.method(:apply)})
        credential = Settings.credential 

        log(args).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),
          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 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

3 entries across 3 versions & 1 rubygems

Version Path
idonethis-cli-0.4.0 lib/adapters/cli.rb
idonethis-cli-0.3.0 lib/adapters/cli.rb
idonethis-cli-0.2.2 lib/adapters/cli.rb