Refactor dryrun safe commands
This commit is contained in:
parent
d592dc49b8
commit
78302888dd
40
run.py
40
run.py
@ -86,7 +86,7 @@ def convert_template(env, template, key_value_store, out):
|
|||||||
|
|
||||||
out_dir_name = os.path.dirname(out)
|
out_dir_name = os.path.dirname(out)
|
||||||
if not os.path.isdir(out_dir_name):
|
if not os.path.isdir(out_dir_name):
|
||||||
os.makedirs(out_dir_name)
|
dryrun_safe_mkdir(out_dir_name)
|
||||||
|
|
||||||
if os.path.isfile(out) and not args.force:
|
if os.path.isfile(out) and not args.force:
|
||||||
if args.diff:
|
if args.diff:
|
||||||
@ -100,11 +100,7 @@ def convert_template(env, template, key_value_store, out):
|
|||||||
print(f"File `{out}` already exists, will not overwrite. Rerun with `-f` to force overwriting existing files.", file=sys.stderr)
|
print(f"File `{out}` already exists, will not overwrite. Rerun with `-f` to force overwriting existing files.", file=sys.stderr)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not args.dryrun:
|
dryrun_safe_write(out, rendered)
|
||||||
with open(out, 'w') as file:
|
|
||||||
file.write(rendered)
|
|
||||||
else:
|
|
||||||
print(f'Would write rendered template to `{out}`.')
|
|
||||||
|
|
||||||
if args.link:
|
if args.link:
|
||||||
first_line = source.split('\n')[0]
|
first_line = source.split('\n')[0]
|
||||||
@ -123,11 +119,34 @@ def create_symlink(source, destination):
|
|||||||
print('`{destination}` already exists, will not overwrite. Rerun with `-f` to force overwiring existing files.', file=sys.stderr)
|
print('`{destination}` already exists, will not overwrite. Rerun with `-f` to force overwiring existing files.', file=sys.stderr)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not args.dryrun:
|
dryrun_safe_remove(destination)
|
||||||
os.remove(destination)
|
|
||||||
else:
|
|
||||||
print(f'Would remove `{destination}`.')
|
|
||||||
|
|
||||||
|
dest_dir_name = os.path.dirname(destination)
|
||||||
|
if not os.path.isdir(dest_dir_name):
|
||||||
|
dryrun_safe_mkdir(dest_dir_name)
|
||||||
|
|
||||||
|
dryrun_safe_create_symlink(source, destination)
|
||||||
|
|
||||||
|
def dryrun_safe_write(location, content):
|
||||||
|
if not args.dryrun:
|
||||||
|
with open(location, 'w') as file:
|
||||||
|
file.write(content)
|
||||||
|
else:
|
||||||
|
print(f'Would write to `{location}`.')
|
||||||
|
|
||||||
|
def dryrun_safe_remove(location):
|
||||||
|
if not args.dryrun:
|
||||||
|
os.remove(location)
|
||||||
|
else:
|
||||||
|
print(f'Would remove `{location}`.')
|
||||||
|
|
||||||
|
def dryrun_safe_mkdir(dir):
|
||||||
|
if not args.dryrun:
|
||||||
|
os.makedirs(dir)
|
||||||
|
else:
|
||||||
|
print(f'Would create directory `{dir}`.')
|
||||||
|
|
||||||
|
def dryrun_safe_create_symlink(source, destination):
|
||||||
if not args.dryrun:
|
if not args.dryrun:
|
||||||
os.symlink(source, destination)
|
os.symlink(source, destination)
|
||||||
else:
|
else:
|
||||||
@ -135,4 +154,3 @@ def create_symlink(source, destination):
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user