I would like to use a Fabric command to set up a local development environment, and as part of that I want to be able to set up a git remote. This works fine:
from fabric.api import local
def set_remote():
""" Set up git remote for pushing to dev."""
local('git remote add myremote [email protected]:myrepo.git')
The problem comes with running this a second time - when the local command bombs because the remote already exists. I'd like to prevent this by checking if the remote exists first:
In pseudocode, I'd like to do the following:
if 'myremote' in local('git remote'):
print 'Remote \'myremote\' already exists.'
else:
local('git remote add myremote [email protected]:myrepo.git')
How can I do this?
You can use the settings
context manager to warn_only
:
from fabric.context_managers import settings
with settings(warn_only=True):
# some command we are all right with having fail
Alternately, you can set the capture
keyword arg on the local
command to True
:
if 'myremote' in local('git remote', capture=True):
print 'Remote \'myremote\' already exists.'
else:
local('git remote add myremote [email protected]:myrepo.git')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With