Sha256: 989ec8fc7d93e19b704ec1a734f4b7ecf54a05d4e8ecddcff9ff0d059d3e94ea

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'markdo/commands'

module Markdo
  class CLI
    def initialize(command_support = CommandSupport.new)
      @stdin = command_support.stdin
      @stdout = command_support.stdout
      @stderr = command_support.stderr
      @full_env = command_support.env
    end

    def run(command_name = 'help', *args)
      command = case command_name
                when 'q'
                  QueryCommand
                when 'starred'
                  StarCommand
                when '--version'
                  VersionCommand
                else
                  choose_command_class(command_name)
                end

      command_support = CommandSupport.new(stdin: @stdin,
                                           stdout: @stdout,
                                           stderr: @stderr,
                                           env: merged_env)
      command.new(command_support).run(*args)
    end

    private

    def choose_command_class(command_name)
      command_class_name = "#{command_name.capitalize}Command"
      ::Markdo.const_get(command_class_name)
    rescue NameError
      HelpCommand
    end

    def default_env
      {
        'MARKDO_ROOT' => '.',
        'MARKDO_INBOX' => 'Inbox.md',
      }
    end

    def merged_env
      default_env.merge(@full_env)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
markdo-0.2.0 lib/markdo/cli.rb