Repository: deis/deis

Issue:
Proposal: deis cli discrete git remote command
Currently `$ deis apps:create foo` (optionally) adds a git remote.

If an app is accessed by multiple people then the others have to create their remote manually.

Heroku toolbelt has a command for this:

```
$ heroku help git
Additional commands, type "heroku help COMMAND" for more details:

  git:clone APP [DIRECTORY]  #  clones a heroku app to your local machine at DIRECTORY (defaults to app name)
  git:remote [OPTIONS]       #  adds a git remote to an app repo
```

I propose adding a `deis git:remote --app foo --remote deis` command to the deis CLI.



Discussion:

Resolving patch:
diff --git a/client/deis.py b/client/deis.py
--- a/client/deis.py
+++ b/client/deis.py
@@ -23,6 +23,7 @@
 
   keys          manage ssh keys used for `git push` deployments
   perms         manage permissions for applications
+  git           manage git for applications
 
 Shortcut commands, use ``deis shortcuts`` to see all::
 
@@ -436,6 +437,9 @@ def apps_create(self, args):
 
           -b --buildpack BUILDPACK
             a buildpack url to use for this app
+
+          -r --remote REMOTE
+            name of remote to create. [default: deis]
         """
         body = {}
         app_name = None
@@ -467,24 +471,12 @@ def apps_create(self, args):
         if buildpack:
             self._config_set(app_id, {'BUILDPACK_URL': buildpack})
 
-        # set a git remote if necessary
-        try:
-            self._session.git_root()
-        except EnvironmentError:
-            return
-        hostname = urlparse.urlparse(self._settings['controller']).netloc.split(':')[0]
-        git_remote = "ssh://git@{hostname}:2222/{app_id}.git".format(**locals())
         if args.get('--no-remote'):
+            hostname = urlparse.urlparse(self._settings['controller']).netloc.split(':')[0]
+            git_remote = "ssh://git@{hostname}:2222/{app_id}.git".format(**locals())
             self._logger.info('remote available at {}'.format(git_remote))
         else:
-            try:
-                subprocess.check_call(
-                    ['git', 'remote', 'add', 'deis', git_remote],
-                    stdout=subprocess.PIPE)
-                self._logger.info('Git remote deis added')
-            except subprocess.CalledProcessError:
-                self._logger.error('Could not create Deis remote')
-                sys.exit(1)
+            self._git_remote_create(app_id, args.get('--remote'))
 
     def apps_destroy(self, args):
         """
@@ -1273,6 +1265,58 @@ def domains_list(self, args):
         else:
             raise ResponseError(response)
 
+    def git(self, args):
+        """
+        Valid commands for git:
+
+        git:remote          Adds git remote of application to repository
+
+        Use `deis help [command]` to learn more.
+        """
+        raise DocoptExit('`deis git` is not a valid command, try `deis help git`')
+
+    def git_remote(self, args):
+        """
+        Adds git remote of application to repository
+
+        Usage: deis git:remote [options]
+
+        Options:
+          -a --app=<app>
+            the uniquely identifiable name for the application.
+
+          -r --remote REMOTE
+            name of remote to create. [default: deis]
+        """
+        app = args.get('--app')
+        if not app:
+            app = self._session.app
+        response = self._dispatch(
+            'get', "/v1/apps/{app}/domains".format(app=app))
+        if response.status_code == requests.codes.ok:
+            self._git_remote_create(app, args.get('--remote'))
+        else:
+            raise ResponseError(response)
+
+    def _git_remote_create(self, app, remote_name):
+        """
+        Adds a git_remote to the current repository. Sets up a git root if necessary.
+        """
+        hostname = urlparse.urlparse(self._settings['controller']).netloc.split(':')[0]
+        git_remote = "ssh://git@{hostname}:2222/{app}.git".format(**locals())
+        try:
+            self._session.git_root()
+        except EnvironmentError:
+            return
+        try:
+            subprocess.check_call(
+                ['git', 'remote', 'add', remote_name, git_remote],
+                stdout=subprocess.PIPE)
+            self._logger.info('Git remote {} added'.format(remote_name))
+        except subprocess.CalledProcessError:
+            self._logger.error('Could not create Deis remote')
+            sys.exit(1)
+
     def limits(self, args):
         """
         Valid commands for limits:


Test patch: