diff --git a/link-tool.py b/link-tool.py index 2905a41..f71a7b3 100644 --- a/link-tool.py +++ b/link-tool.py @@ -65,8 +65,13 @@ def load_config(conf_path: str) -> Tuple[Conf, List[Link]]: return conf, links -def make_link(links: List[Link], dry_run=False): +def make_link(links: List[Link], dry_run=False, overwrite=False): for link in links: + if os.path.exists(link.dst): + if not overwrite: + logger.info('dst exists, skipping: {}'.format(link.dst)) + return + if os.path.exists(link.dst): logger.warning('dst exists, removing: {}'.format(link.dst)) if not dry_run: @@ -86,6 +91,8 @@ def main(): parser = argparse.ArgumentParser() parser.add_argument('-c', '--config', required=True, help='configuration file') + parser.add_argument('-o', '--overwrite', action='store_true', + help='overwrite dst when exists') parser.add_argument('-n', '--dry-run', action='store_true', help='dry run without actually create link') parser.add_argument('-v', '--verbose', action='store_true', @@ -96,6 +103,7 @@ def main(): config_path = args.config verbose = args.verbose dry_run = args.dry_run + overwrite = args.overwrite logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO) @@ -115,7 +123,7 @@ def main(): logger.info('the CWD realpath: {}'.format(os.path.realpath(chdir_target))) os.chdir(chdir_target) - make_link(links, dry_run=dry_run) + make_link(links, dry_run=dry_run, overwrite=overwrite) if __name__ == '__main__': main()