Add a fmt command that uses the Fuchsia toolchain

Change-Id: I6874a5a6c6ad2f699fa24d72944a045e0a86e188
diff --git a/src/lib.rs b/src/lib.rs
index c4cb676..b9b74a9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -449,9 +449,9 @@
         fs::canonicalize(std::env::current_exe()?)?
     };
 
-    let mut runner_args = vec![
-        fargo_path.to_str().ok_or_else(|| err_msg("unable to convert path to utf8 encoding"))?,
-    ];
+    let mut runner_args = vec![fargo_path
+        .to_str()
+        .ok_or_else(|| err_msg("unable to convert path to utf8 encoding"))?];
 
     if options.verbose {
         runner_args.push("-v");
@@ -508,6 +508,21 @@
     }
 }
 
+fn format_project(manifest_path: Option<PathBuf>) -> Result<(), Error> {
+    let mut cmd = Command::new(cargo_path()?);
+    if let Some(ref manifest_path) = manifest_path {
+        let parent =
+            manifest_path.parent().expect(&format!("Can't get parent of {:#?}", manifest_path));
+        cmd.current_dir(parent);
+    }
+    cmd.arg(FORMAT);
+    let cargo_status = cmd.status()?;
+    if !cargo_status.success() {
+        bail!("cargo exited with status {:?}", cargo_status,);
+    }
+    Ok(())
+}
+
 /// Runs the cargo tool configured to target Fuchsia. When used as a library,
 /// the runner options must contain the path to fargo or some other program
 /// that implements the `run-on-target` subcommand in a way compatible with
@@ -531,6 +546,7 @@
 ///         disable_cross: false,
 ///         manifest_path: None,
 ///         cmx_path: None,
+///         app_name: None,
 ///     },
 ///     "help",
 ///     &[],
@@ -731,6 +747,8 @@
 static MAKE_PACKAGE: &str = "make-package";
 static BINARY_PATH: &str = "binary-path";
 
+static FORMAT: &str = "fmt";
+
 /// Arguments which configure the startup of an emulator.
 ///
 /// Used when `start`ing or `restart`ing.
@@ -1138,6 +1156,9 @@
                 )
                 .about("Make a Fuchsia package from an unstripped binary"),
         )
+        .subcommand(
+            SubCommand::with_name(FORMAT).about("Run cargo fmt using the Fuchsia toolchain"),
+        )
         .get_matches();
 
     let verbose = global_matches.is_present("verbose");
@@ -1503,5 +1524,11 @@
         return Ok(());
     }
 
+    if let Some(fmt_matches) = global_matches.subcommand_matches(FORMAT) {
+        let manifest_path = convert_manifest_path(&fmt_matches.value_of(MANIFEST_PATH));
+        format_project(manifest_path)?;
+        return Ok(());
+    }
+
     Ok(())
 }