import os
import subprocess
import sys

import blah.repositories
import blah.fetcher

class WhatIsThisCommand(object):
    def create_parser(self, subparser):
        subparser.add_argument("directory", nargs="?")
    
    def execute(self, args):
        directory = args.directory if args.directory is not None else os.getcwd()
        repository = blah.repositories.find_repository(directory)
        if repository is None:
            print "Could not find source control repository"
        else:
            print "{0}+file://{1}".format(repository.type, repository.working_directory)

what_is_this_command = WhatIsThisCommand()

class FetchCommand(object):
    def create_parser(self, subparser):
        subparser.add_argument("repository_uri", metavar="repository-uri")
        subparser.add_argument("local_path", metavar="local-path")
    
    def execute(self, args):
        blah.fetcher.fetch(args.repository_uri, args.local_path)

commands = {
    "whatisthis": what_is_this_command,
    "what-is-this": what_is_this_command,
    "fetch": FetchCommand()
}
