This repository was archived by the owner on Jul 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlib.rs
56 lines (44 loc) · 1.53 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use anyhow::Error;
use std::{io::BufRead, process::Command, thread};
use tokio::sync::mpsc::UnboundedSender;
pub fn start_code_tunnel(sender: UnboundedSender<String>, dir: &str) -> Result<u32, Error> {
verify_if_code_is_installed()?;
let mut child = Command::new("code")
.arg("tunnel")
.arg("--accept-server-license-terms")
.current_dir(dir)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.expect("failed to execute process");
let pid = child.id();
thread::spawn(move || {
let stdout = child.stdout.take().expect("Failed to get stdout handle");
let stderr = child.stderr.take().expect("Failed to get stderr handle");
let stdout_reader = std::io::BufReader::new(stdout);
let stderr_reader = std::io::BufReader::new(stderr);
for line in stdout_reader.lines() {
if let Ok(line) = line {
sender.send(line).unwrap();
}
}
for line in stderr_reader.lines() {
if let Ok(line) = line {
sender.send(line).unwrap();
}
}
drop(sender);
child.wait().expect("Failed to wait on child");
});
Ok(pid)
}
fn verify_if_code_is_installed() -> Result<(), Error> {
let output = Command::new("code")
.arg("--version")
.output()
.expect("failed to execute process");
if !output.status.success() {
return Err(Error::msg("Code is not installed"));
}
Ok(())
}