Add exit_json and fail_json to git module
This adds exit_json() and fail_json() to git module. It also sets version to 'HEAD', if not provided.
This commit is contained in:
parent
23c691bd30
commit
720ef7404e
1 changed files with 18 additions and 34 deletions
52
git
52
git
|
@ -32,35 +32,34 @@ import sys
|
||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# Basic support methods
|
||||||
|
|
||||||
|
def exit_json(rc=0, **kwargs):
|
||||||
|
print json.dumps(kwargs)
|
||||||
|
sys.exit(rc)
|
||||||
|
|
||||||
|
def fail_json(**kwargs):
|
||||||
|
kwargs['failed'] = True
|
||||||
|
exit_json(rc=1, **kwargs)
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
# convert arguments of form a=b c=d
|
# convert arguments of form a=b c=d
|
||||||
# to a dictionary
|
# to a dictionary
|
||||||
# FIXME: make more idiomatic
|
# FIXME: make more idiomatic
|
||||||
|
|
||||||
if len(sys.argv) == 1:
|
if len(sys.argv) == 1:
|
||||||
print json.dumps({
|
fail_json(msg="the command module requires arguments (-a)")
|
||||||
"failed" : True,
|
|
||||||
"msg" : "the command module requires arguments (-a)"
|
|
||||||
})
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
argfile = sys.argv[1]
|
argfile = sys.argv[1]
|
||||||
if not os.path.exists(argfile):
|
if not os.path.exists(argfile):
|
||||||
print json.dumps({
|
fail_json(msg="Argument file not found")
|
||||||
"failed" : True,
|
|
||||||
"msg" : "Argument file not found"
|
|
||||||
})
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
args = open(argfile, 'r').read()
|
args = open(argfile, 'r').read()
|
||||||
items = shlex.split(args)
|
items = shlex.split(args)
|
||||||
|
|
||||||
if not len(items):
|
if not len(items):
|
||||||
print json.dumps({
|
fail_json(msg="the command module requires arguments (-a)")
|
||||||
"failed" : True,
|
|
||||||
"msg" : "the command module requires arguments (-a)"
|
|
||||||
})
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
params = {}
|
params = {}
|
||||||
for x in items:
|
for x in items:
|
||||||
|
@ -69,7 +68,7 @@ for x in items:
|
||||||
|
|
||||||
dest = params['dest']
|
dest = params['dest']
|
||||||
repo = params['repo']
|
repo = params['repo']
|
||||||
version = params['version']
|
version = params.get('version', 'HEAD')
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
|
|
||||||
|
@ -129,24 +128,14 @@ else:
|
||||||
# handle errors from clone or pull
|
# handle errors from clone or pull
|
||||||
|
|
||||||
if out.find('error') != -1:
|
if out.find('error') != -1:
|
||||||
print json.dumps({
|
fail_json(out=out, err=err)
|
||||||
"failed" : True,
|
|
||||||
"out" : out,
|
|
||||||
"err" : err
|
|
||||||
})
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# switch to version specified regardless of whether
|
# switch to version specified regardless of whether
|
||||||
# we cloned or pulled
|
# we cloned or pulled
|
||||||
|
|
||||||
(out, err) = switchver(version, dest)
|
(out, err) = switchver(version, dest)
|
||||||
if err.find('error') != -1:
|
if err.find('error') != -1:
|
||||||
print json.dumps({
|
fail_json(out=out, err=err)
|
||||||
"failed" : True,
|
|
||||||
"out" : out,
|
|
||||||
"err" : err
|
|
||||||
})
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# determine if we changed anything
|
# determine if we changed anything
|
||||||
|
|
||||||
|
@ -156,9 +145,4 @@ changed = False
|
||||||
if before != after:
|
if before != after:
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
print json.dumps({
|
exit_json(changed=changed, before=before, after=after)
|
||||||
"changed" : changed,
|
|
||||||
"before" : before,
|
|
||||||
"after" : after
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue