diff --git a/cfbs/__main__.py b/cfbs/__main__.py index e359cf86..9a17f8a7 100755 --- a/cfbs/__main__.py +++ b/cfbs/__main__.py @@ -9,4 +9,5 @@ from cfbs.main import main - main() + rc = main() + sys.exit(rc) diff --git a/cfbs/commands.py b/cfbs/commands.py index cfb92e94..c8b86cf9 100644 --- a/cfbs/commands.py +++ b/cfbs/commands.py @@ -21,7 +21,7 @@ sh, ) -from cfbs.pretty import pretty_file, pretty +from cfbs.pretty import pretty_check_file, pretty_file, pretty from cfbs.index import Index @@ -42,18 +42,28 @@ def put_definition(data: dict): f.write(pretty(data)) -def pretty_command(filenames: list) -> int: +def pretty_command(filenames: list, check) -> int: if not filenames: user_error("Filenames missing for cfbs pretty command") + + num_files = 0 for f in filenames: if not f or not f.endswith(".json"): user_error( f"cfbs pretty command can only be used with .json files, not '{os.path.basename(f)}'" ) try: - pretty_file(f) + if check: + if not pretty_check_file(f): + num_files += 1 + print("Would reformat %s" % f) + else: + pretty_file(f) except FileNotFoundError: user_error(f"File '{f}' not found") + if check: + print("Would reformat %d file(s)" % num_files) + return 1 if num_files > 0 else 0 return 0 diff --git a/cfbs/main.py b/cfbs/main.py index 774be93d..65038da4 100644 --- a/cfbs/main.py +++ b/cfbs/main.py @@ -36,6 +36,9 @@ def get_args(): "--force", help="Force rebuild / redownload", action="store_true" ) parser.add_argument("--index", help="Specify alternate index", type=str) + parser.add_argument( + "--check", help="Check if file(s) would be reformatted", action="store_true" + ) args = parser.parse_args() return args @@ -74,7 +77,7 @@ def main() -> int: if args.command == "search": return commands.search_command(args.args, index=args.index) if args.command == "pretty": - return commands.pretty_command(args.args) + return commands.pretty_command(args.args, args.check) if not is_cfbs_repo(): user_error("This is not a cfbs repo, to get started, type: cfbs init") diff --git a/cfbs/pretty.py b/cfbs/pretty.py index 8d418303..7dbea4df 100644 --- a/cfbs/pretty.py +++ b/cfbs/pretty.py @@ -2,6 +2,18 @@ from collections import OrderedDict +def pretty_check_file(filename): + with open(filename) as f: + s = f.read() + o = json.loads(s, object_pairs_hook=OrderedDict) + return s == pretty(o) + "\n" + + +def pretty_check_string(s): + o = json.loads(s, object_pairs_hook=OrderedDict) + return s == pretty(o) + + def pretty_file(filename): with open(filename) as f: data = f.read() diff --git a/test/test_pretty.py b/test/test_pretty.py index 361dd28f..0cacaf7f 100644 --- a/test/test_pretty.py +++ b/test/test_pretty.py @@ -1,5 +1,5 @@ from collections import OrderedDict -from cfbs.pretty import pretty, pretty_string +from cfbs.pretty import pretty, pretty_check_string, pretty_string def test_pretty(): @@ -181,3 +181,18 @@ def test_pretty_string(): } ]""" assert pretty_string(test) == expected + + +def test_pretty_check_string(): + assert pretty_check_string(' "Hello" ') == False + assert pretty_check_string('"Hello"') == True + assert ( + pretty_check_string( + """{ + "name": "lars", + "age": 27 +}""" + ) + == False + ) + assert pretty_check_string('{ "name": "lars", "age": 27 }') == True