[shac] Fixing buildifier format check.

The json output of the buildifier format command looks slightly
different fro the output of the buildifier lint command, so I needed
to split the executions into two.

Also ran the formatter on these files to fix some empty-spacing
issues.

Change-Id: If93e5eeaf91f13ff6006a32074edbd8238a6e88f
Reviewed-on: https://fuchsia-review.googlesource.com/c/shac-project/checks-starlark/+/1021755
Reviewed-by: Darren Chan <chandarren@google.com>
diff --git a/checks/buildifier/common.star b/checks/buildifier/common.star
index 9798cac..bb3e2d6 100644
--- a/checks/buildifier/common.star
+++ b/checks/buildifier/common.star
@@ -15,11 +15,24 @@
            file_filter(f)
     ]
 
-def get_files_with_warnings(ctx, command, files):
+def _run_command(ctx, command, files):
     res = ctx.os.exec(command + files, ok_retcodes = (0, 4)).wait()
     if res.retcode != 0:
         fail(res)
-    data = json.decode(res.stdout)
+    return json.decode(res.stdout)
+
+def get_unformatted_files(ctx, command, files):
+    data = _run_command(ctx, command, files)
+    if data["success"]:
+        return None
+    data["files"] = [i for i in data["files"] if not i["formatted"]]
+    return data
+
+def get_files_with_warnings(ctx, command, files):
+    data = _run_command(ctx, command, files)
+    if data["success"]:
+        return None
+    data["files"] = [i for i in data["files"] if i["warnings"]]
     return data
 
 def apply_fixes_to_files(ctx, command, data, emit_level = "error", emit_message = "File is not linted."):
@@ -28,18 +41,10 @@
     for file in data["files"]:
         filename = file["filename"]
         msg = "\n -> ".join([s["message"] for s in file["warnings"]])
-	if len(msg) > 0:
-	    msg = "\n -> " + msg
-
-        # Buildifier doesn't have a dry-run mode that prints the formatted file
-        # to stdout, so copy each file to a temporary file and format the
-        # temporary file in-place to obtain the formatted result.
-        tempfiles[filename] = ctx.io.tempfile(
-            ctx.io.read_file(filename),
-            name = filename,
-        )
+        if len(msg) > 0:
+            msg = "\n -> " + msg
         warning_messages[filename] = msg
-
+        tempfiles[filename] = ctx.io.tempfile(ctx.io.read_file(filename), name = filename)
     ctx.os.exec(command + tempfiles.values()).wait()
 
     for filepath, temp in tempfiles.items():
diff --git a/checks/buildifier/format.star b/checks/buildifier/format.star
index 3d0520d..ce22e89 100644
--- a/checks/buildifier/format.star
+++ b/checks/buildifier/format.star
@@ -8,7 +8,7 @@
     "./common.star",
     "apply_fixes_to_files",
     "get_affected_files",
-    "get_files_with_warnings",
+    "get_unformatted_files",
 )
 
 def _buildifier_format(
@@ -29,7 +29,7 @@
 
     # Run buildifier to identify the files that need to be formatted.
     command = [tool] + extra_args + ["--format=json", "--mode=check"]
-    files_to_format = get_files_with_warnings(ctx, command, files)
+    files_to_format = get_unformatted_files(ctx, command, files)
     if not files_to_format:
         return
 
@@ -43,4 +43,4 @@
         emit_message = emit_message,
     )
 
-buildifier_format = shac.check(_buildifier_format)
+buildifier_format = shac.check(_buildifier_format, formatter = True)