lib/adapters/cli.rb in idonethis-cli-0.12.0 vs lib/adapters/cli.rb in idonethis-cli-0.13.0

- old
+ new

@@ -1,87 +1,92 @@ -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 - - command = "help" unless command - - 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_case = choose command.to_sym, rest - - unless use_case - puts "No command <#{command.to_sym}> found" - return - end - - use_case.call credential, args - end - - private - - def choose(command, opts) - 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" } - } - - if command == :list && opts.include?("teams") - return ->(credential, args) { - Idonethis::UseCases::Teams.apply(credential, args.merge(view: Idonethis::Adapters::Views::Cli::Teams.method(:apply))) - } - end - - use_case = use_cases[command] - end - - 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 - - opts.on("-t TEAM", "--team TEAM" "Run against this team") do |team_name| - args[:team] = team_name - end - end.parse! - - args - end - end - end -end +require 'internet' +require 'views/cli' +require 'use_cases/new' +require 'use_cases/list' +require 'settings_file' +require 'io/directory_info' + +module Idonethis::Adapters + class Cli + class << self + def run(argv={}) + args = parse(argv) + + command,*rest = argv + + command = "help" unless command + + 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_case = choose command.to_sym, rest + + unless use_case + log.call "No command <#{command.to_sym}> found" + return + end + + use_case.call credential, args + end + + private + + def choose(command, opts) + use_cases = { + list: Idonethis::UseCases::List.method(:apply), + new: Idonethis::UseCases::New.method(:apply), + config: Idonethis::UseCases::Config.method(:apply), + git: ->(_, args) { Idonethis::UseCases::Git.apply(_, args.merge(git: Idonethis::Adapters::Git, view: ->(msg) { puts msg }, fs: Idonethis::Adapters::IO::DirectoryInfo))}, + help: ->(credential, args) { puts "TODO: implement help" } + } + + if command == :list && opts.include?("teams") + return ->(credential, args) { + Idonethis::UseCases::Teams.apply(credential, args.merge(view: Idonethis::Adapters::Views::Cli::Teams.method(:apply))) + } + end + + use_case = use_cases[command] + end + + 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 + + opts.on("-t TEAM", "--team TEAM" "Run against this team") do |team_name| + args[:team] = team_name + end + + opts.on("-s WHEN", "--since WHEN" "Show git commits since when") do |value| + args[:since] = value + end + end.parse! + + args + end + end + end +end