Create cli.common, begin record command

This commit is contained in:
Jacob Hinkle 2022-09-25 12:43:11 -04:00
parent 07f6347d49
commit 6c62d0568d
4 changed files with 59 additions and 25 deletions

15
src/nancy/cli/common.py Normal file
View File

@ -0,0 +1,15 @@
def confirm(question, default_no=False):
"""Ask a question and wait for a Y/N response."""
choices = ' [y/N]: ' if default_no else ' [Y/n]: '
default_answer = 'n' if default_no else 'y'
while True:
raw_reply = str(input(question + choices))
reply = raw_reply.lower().strip() or default_answer
if reply[0] == 'y':
return True
elif reply[0] == 'n':
return False
else:
print("Unrecognized input:", reply)

View File

@ -7,8 +7,8 @@ import sys
import warnings
def print_diff(ABdiff: fs.FSDiff, version_info='count', indent=2,
indent_level=0, use_color=True, show_hashes=False):
def print_diff(ABdiff: fs.FSDiff, indent=2, indent_level=0, use_color=True,
show_hashes=False):
"""Pretty print an FSDiff object"""
if use_color:
try:
@ -67,7 +67,7 @@ def print_diff(ABdiff: fs.FSDiff, version_info='count', indent=2,
_print_row('MOD', d.B, l)
def diff(store, filedir_path, version_info='count', show_hashes=False, use_color=True):
def diff(store, filedir_path, show_hashes=False, use_color=True):
"""Unwrapped diff command that prints a diff"""
if not os.path.exists(filedir_path):
raise FileNotFoundError(f"Cannot diff non-existent file or directory {filedir_path}")
@ -75,17 +75,11 @@ def diff(store, filedir_path, version_info='count', show_hashes=False, use_color
# get the diff object
fsdiff = store.diff(filedir_path)
print_diff(fsdiff, version_info=version_info, show_hashes=show_hashes, use_color=use_color)
print_diff(fsdiff, show_hashes=show_hashes, use_color=use_color)
@click.command()
@click.argument("path", default='.')
@click.option(
"--version-info",
type=click.Choice(['count', 'details', 'none'], case_sensitive=False),
default='count',
help='How much info to display about file versions.',
)
@click.option(
'-H', "--show-hashes",
is_flag=True,
@ -96,7 +90,7 @@ def diff(store, filedir_path, version_info='count', show_hashes=False, use_color
is_flag=True,
help='If given, do not print any color output.',
)
def diff_cli(path, version_info, show_hashes, no_color):
def diff_cli(path, show_hashes, no_color):
"""Detect and describe changes to PATH
PATH is a path to a file or directory inside an existing nancy store

View File

@ -2,10 +2,15 @@ import click
from .. import store
from .common import confirm
from .diff import print_diff
import os
import sys
def record(directory):
def record(directory, message, show_diff=True, show_hashes=False, use_color=True,
skip_confirm=False):
"""Unwrapped record command"""
if not os.path.isdir(directory):
raise ValueError(f"Cannot record non-existent directory {directory}")
@ -16,12 +21,38 @@ def record(directory):
else: # this is an existing store
s = store.Store(directory)
s.record()
fsdiff = s.diff(directory)
if show_diff:
print_diff(fsdiff, show_hashes=show_hashes, use_color=use_color)
print('Message:', message)
if skip_confirm or confirm("Record the values above into the database?"):
s.record(fsdiff, message=message)
else:
print("Cancelled!")
sys.exit(1)
@click.command()
@click.argument("directory", default='.')
def record_cli(directory):
@click.option(
'-H', "--show-hashes",
is_flag=True,
help='If given, prepend each line in the diff with the new file hash (SHA256).',
)
@click.option(
"--no-color",
is_flag=True,
help='If given, do not print any color output.',
)
@click.option(
"-m", "--message",
type=str,
required=True,
help='A user-defined descriptive message for this recording operation.',
def record_cli(directory, show_hashes, no_color, message):
"""
Initialize tracking or record changes to a tracked directory.
@ -30,5 +61,6 @@ def record_cli(directory):
directory is part of an existing store, it will be updated and versions
of any files changes since the last recording will be incremented.
"""
record(directory)
record(directory, message=message, show_hashes=show_hashes, use_color=not
no_color)

View File

@ -202,14 +202,10 @@ class Store:
return fs.FSDiff.compute(recorded, current)
def record(self, message=None):
def record(self, diff, message=None):
with self.program('RECORD', message) as p:
d = self.diff(self.path)
# select all dirs
# detect new and deleted files, and those that have changed type
for _, d in diff.flatten_tree():
pass
# create entries for all directories if they do not yet exist (top
# down)
@ -219,9 +215,6 @@ class Store:
# update versions in nancy.db as appropriate
#p.unlink_file()
#p.record_file()
# all effected files
#@contextmanager
def run(