allow forcing init in dirty directory

This commit is contained in:
Nick Sweeting 2019-05-01 02:27:50 -04:00
parent cb2dd1ee28
commit ad3898add6
2 changed files with 24 additions and 12 deletions

View file

@ -21,10 +21,18 @@ def main(args: Optional[List[str]]=None, stdin: Optional[IO]=None, pwd: Optional
add_help=True,
formatter_class=SmartFormatter,
)
parser.parse_args(args or ())
parser.add_argument(
'--force', # '-f',
action='store_true',
help='Ignore unrecognized files in current directory and initialize anyway',
)
command = parser.parse_args(args or ())
reject_stdin(__command__, stdin)
init(out_dir=pwd or OUTPUT_DIR)
init(
force=command.force,
out_dir=pwd or OUTPUT_DIR,
)
if __name__ == '__main__':

View file

@ -238,7 +238,7 @@ def run(subcommand: str,
@enforce_types
def init(out_dir: str=OUTPUT_DIR) -> None:
def init(force: bool=False, out_dir: str=OUTPUT_DIR) -> None:
"""Initialize a new ArchiveBox collection in the current directory"""
os.makedirs(out_dir, exist_ok=True)
@ -253,6 +253,10 @@ def init(out_dir: str=OUTPUT_DIR) -> None:
print('{green}[*] Updating existing ArchiveBox collection in this folder...{reset}'.format(**ANSI))
print(f' {out_dir}')
print('{green}------------------------------------------------------------------{reset}'.format(**ANSI))
else:
if force:
stderr('[!] This folder appears to already have files in it, but no index.json is present.', color='lightyellow')
stderr(' Because --force was passed, ArchiveBox will initialize anyway (which may overwrite existing files).')
else:
stderr(
("{red}[X] This folder appears to already have files in it, but no index.json is present.{reset}\n\n"
@ -262,7 +266,7 @@ def init(out_dir: str=OUTPUT_DIR) -> None:
" (Always make sure your data folder is backed up first before updating ArchiveBox)"
).format(out_dir, **ANSI)
)
raise SystemExit(1)
raise SystemExit(2)
if existing_index:
print('\n{green}[*] Verifying archive folder structure...{reset}'.format(**ANSI))