Use fx shell instead of ssh directly

Using ssh directly no longer appears to work, most likely due
to http://fxrev.dev/431854/

Change-Id: I9a0f8e8f9252da8c4faf19553bcd44455d5c6bdf
Reviewed-on: https://fuchsia-review.googlesource.com/c/fargo/+/446395
Reviewed-by: Dan Johnson <computerdruid@google.com>
diff --git a/src/command_line.rs b/src/command_line.rs
index 8e54229..a61b9a7 100644
--- a/src/command_line.rs
+++ b/src/command_line.rs
@@ -3,7 +3,7 @@
     build_rustc::build_rustc,
     check_binary,
     cross::run_pkg_config,
-    device::{netls, ssh, start_emulator, stop_emulator, StartEmulatorOptions},
+    device::{netls, shell, start_emulator, stop_emulator, StartEmulatorOptions},
     enable_networking, format_project,
     linking::extract_linkage_information,
     manifest::load_manifest,
@@ -50,7 +50,7 @@
     /// Run binary on Fuchsia device or emulator
     Run(Run),
     /// Open a shell on Fuchsia device or emulator
-    Ssh,
+    Shell,
     /// Start a Fuchsia emulator
     Start(Start),
     /// Stop all Fuchsia emulators
@@ -566,8 +566,7 @@
             let mut params = vec![];
 
             if run.kill_all {
-                ssh(opt.verbose, &fuchsia_config, &target_options, "killall *fargo.cmx")
-                    .unwrap_or_default();
+                shell(opt.verbose, &target_options, "killall *fargo.cmx").unwrap_or_default();
             }
 
             if let Some(package) = run.package.as_ref() {
@@ -622,7 +621,6 @@
                 &binary_to_run,
                 verbose,
                 run_on_target.nocapture,
-                &fuchsia_config,
                 &target_options,
                 run_mode,
                 &run_cargo_options,
@@ -635,8 +633,8 @@
             );
         }
 
-        FargoCommand::Ssh => {
-            return ssh(opt.verbose, &fuchsia_config, &target_options, "");
+        FargoCommand::Shell => {
+            return shell(opt.verbose, &target_options, "");
         }
 
         FargoCommand::Start(start_opts) => {
diff --git a/src/device.rs b/src/device.rs
index 8a16b31..4ce7f4a 100644
--- a/src/device.rs
+++ b/src/device.rs
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-use crate::sdk::{fuchsia_dir, fx_path, target_out_dir, FuchsiaConfig, TargetOptions};
+use crate::sdk::{fuchsia_dir, fx_path, TargetOptions};
 use crate::utils::is_mac;
 use failure::{bail, err_msg, Error, ResultExt};
 use std::env;
@@ -10,28 +10,6 @@
 use std::process::{Command, Stdio};
 use std::{str, thread, time};
 
-pub fn netaddr(verbose: bool, target_options: &TargetOptions<'_, '_>) -> Result<String, Error> {
-    let fx_script = fx_path()?;
-    if !fx_script.exists() {
-        bail!("fx script not found at {:?}", fx_script);
-    }
-
-    let mut args = vec!["netaddr", "--fuchsia", "--timeout=500", "--nowait"];
-    if let Some(device_name) = target_options.device_name {
-        args.push(device_name);
-    }
-    let netaddr_result = Command::new(fx_script).args(args).output()?;
-    let result = str::from_utf8(&netaddr_result.stdout)?.trim().to_string();
-    if verbose {
-        println!("netaddr status = {}, result = {}", netaddr_result.status, result);
-    }
-    if !netaddr_result.status.success() {
-        let err_str = str::from_utf8(&netaddr_result.stderr)?.trim().to_string();
-        bail!("netaddr failed with status {:?}: {}", netaddr_result.status, err_str);
-    }
-    Ok(result)
-}
-
 pub fn netls(verbose: bool) -> Result<(), Error> {
     let fx_script = fx_path()?;
     if !fx_script.exists() {
@@ -50,36 +28,29 @@
     Ok(())
 }
 
-static SSH_OPTIONS: &'static [&str] = &[
-    "-o",
-    "UserKnownHostsFile=/dev/null",
-    "-o",
-    "StrictHostKeyChecking=no",
-    "-o",
-    "ConnectTimeout=20",
-];
-
-pub fn ssh(
+pub fn shell(
     verbose: bool,
-    config: &FuchsiaConfig,
     target_options: &TargetOptions<'_, '_>,
     command: &str,
 ) -> Result<(), Error> {
-    let netaddr = netaddr(verbose, target_options).context("netaddr failed")?;
-    let ssh_config = target_out_dir(config)?.join("ssh-keys/ssh_config");
-    if !ssh_config.exists() {
-        bail!("ssh config not found at {:?}", ssh_config);
+    let fx_script = fx_path()?;
+    if !fx_script.exists() {
+        bail!("fx script not found at {:?}", fx_script);
     }
-    let ssh_result = Command::new("ssh")
-        .env_remove("SSH_AUTH_SOCK")
-        .arg("-q")
-        .arg("-F")
-        .arg(ssh_config)
-        .args(SSH_OPTIONS)
-        .arg(netaddr)
-        .arg(command)
-        .status()
-        .context("unable to run ssh")?;
+
+    let mut ssh_cmd = Command::new(fx_script);
+
+    if let Some(device_name) = target_options.device_name {
+        ssh_cmd.arg(format!("-d={}", device_name));
+    }
+
+    ssh_cmd.arg("shell").arg(command);
+
+    if verbose {
+        println!("ssh_cmd = {:#?}", ssh_cmd);
+    }
+
+    let ssh_result = ssh_cmd.status().context("unable to run fx shell")?;
 
     if !ssh_result.success() {
         bail!("ssh failed: {}", ssh_result);
diff --git a/src/lib.rs b/src/lib.rs
index f704c3d..6e4f473 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,7 +6,6 @@
 //! exposes one function, `run_cargo`, that could be integrated directly into
 //! Rust programs that want to cross compile cargo crates on Fuchsia.
 
-#![recursion_limit = "1024"]
 #![deny(warnings)]
 
 mod build_rustc;
@@ -22,7 +21,7 @@
 pub use crate::sdk::{FuchsiaConfig, TargetOptions};
 
 use crate::cross::{pkg_config_path, run_configure};
-use crate::device::{enable_networking, ssh};
+use crate::device::{enable_networking, shell};
 use crate::package::make_package;
 use crate::sdk::{
     cargo_path, clang_archiver_path, clang_c_compiler_path, clang_cpp_compiler_path,
@@ -43,7 +42,6 @@
     filename: &str,
     verbose: bool,
     nocapture: bool,
-    config: &FuchsiaConfig,
     target_options: &TargetOptions<'_, '_>,
     run_mode: RunMode,
     run_cargo_options: &RunCargoOptions,
@@ -94,7 +92,7 @@
         println!("running {}", command_string);
     }
 
-    ssh(verbose, config, target_options, &command_string).context("ssh failed")?;
+    shell(verbose, target_options, &command_string).context("ssh failed")?;
     Ok(())
 }