-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
900 additions
and
29 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::{ | ||
io::{BufRead, BufReader}, | ||
process::{Command, ExitStatus, Stdio}, | ||
sync::mpsc::{self, Receiver, Sender}, | ||
thread, | ||
}; | ||
|
||
use crate::{pkgx::Pkgx, Extension}; | ||
use anyhow::Error; | ||
use fluentci_types::Output; | ||
|
||
#[derive(Default)] | ||
pub struct Git {} | ||
|
||
impl Extension for Git { | ||
fn exec( | ||
&self, | ||
url: &str, | ||
tx: Sender<String>, | ||
out: Output, | ||
last_cmd: bool, | ||
work_dir: &str, | ||
) -> Result<ExitStatus, Error> { | ||
self.setup()?; | ||
|
||
if url.is_empty() { | ||
return Ok(ExitStatus::default()); | ||
} | ||
|
||
let (stdout_tx, stdout_rx): (Sender<String>, Receiver<String>) = mpsc::channel(); | ||
let (stderr_tx, stderr_rx): (Sender<String>, Receiver<String>) = mpsc::channel(); | ||
|
||
let mut child = Command::new("bash") | ||
.arg("-c") | ||
.arg(format!("git clone {}", url)) | ||
.current_dir(work_dir) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::piped()) | ||
.spawn()?; | ||
|
||
let stdout_tx_clone = stdout_tx.clone(); | ||
let stdout = child.stdout.take().unwrap(); | ||
let stderr = child.stderr.take().unwrap(); | ||
|
||
let out_clone = out.clone(); | ||
let tx_clone = tx.clone(); | ||
|
||
thread::spawn(move || { | ||
let mut stdout = String::new(); | ||
while let Ok(line) = stdout_rx.recv() { | ||
println!("{}", line); | ||
stdout.push_str(&line); | ||
stdout.push_str("\n"); | ||
} | ||
if out_clone == Output::Stdout && last_cmd { | ||
tx_clone.send(stdout).unwrap(); | ||
} | ||
}); | ||
|
||
thread::spawn(move || { | ||
let mut stderr = String::new(); | ||
while let Ok(line) = stderr_rx.recv() { | ||
println!("{}", line); | ||
stderr.push_str(&line); | ||
stderr.push_str("\n"); | ||
} | ||
if out == Output::Stderr && last_cmd { | ||
tx.send(stderr).unwrap(); | ||
} | ||
}); | ||
|
||
thread::spawn(move || { | ||
let reader = BufReader::new(stdout); | ||
for line in reader.lines() { | ||
stdout_tx_clone.send(line.unwrap()).unwrap(); | ||
} | ||
}); | ||
|
||
thread::spawn(move || { | ||
let reader = BufReader::new(stderr); | ||
for line in reader.lines() { | ||
stderr_tx.send(line.unwrap()).unwrap(); | ||
} | ||
}); | ||
|
||
child.wait().map_err(Error::from) | ||
} | ||
|
||
fn setup(&self) -> Result<(), Error> { | ||
Pkgx::default().install(vec!["git"])?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::{ | ||
io::{BufRead, BufReader}, | ||
process::{Command, ExitStatus, Stdio}, | ||
sync::mpsc::{self, Receiver, Sender}, | ||
thread, | ||
}; | ||
|
||
use crate::{pkgx::Pkgx, Extension}; | ||
use anyhow::Error; | ||
use fluentci_types::Output; | ||
|
||
#[derive(Default)] | ||
pub struct Http {} | ||
|
||
impl Extension for Http { | ||
fn exec( | ||
&self, | ||
url: &str, | ||
tx: Sender<String>, | ||
out: Output, | ||
last_cmd: bool, | ||
work_dir: &str, | ||
) -> Result<ExitStatus, Error> { | ||
self.setup()?; | ||
|
||
if url.is_empty() { | ||
return Ok(ExitStatus::default()); | ||
} | ||
|
||
let (stdout_tx, stdout_rx): (Sender<String>, Receiver<String>) = mpsc::channel(); | ||
let (stderr_tx, stderr_rx): (Sender<String>, Receiver<String>) = mpsc::channel(); | ||
|
||
let filename = sha256::digest(url).to_string(); | ||
let mut child = Command::new("bash") | ||
.arg("-c") | ||
.arg(format!("curl -s {} -o {}", url, filename)) | ||
.current_dir(work_dir) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::piped()) | ||
.spawn()?; | ||
|
||
let stdout_tx_clone = stdout_tx.clone(); | ||
let stdout = child.stdout.take().unwrap(); | ||
let stderr = child.stderr.take().unwrap(); | ||
|
||
let out_clone = out.clone(); | ||
let tx_clone = tx.clone(); | ||
|
||
thread::spawn(move || { | ||
let mut stdout = String::new(); | ||
while let Ok(line) = stdout_rx.recv() { | ||
println!("{}", line); | ||
stdout.push_str(&line); | ||
stdout.push_str("\n"); | ||
} | ||
if out_clone == Output::Stdout && last_cmd { | ||
tx_clone.send(stdout).unwrap(); | ||
} | ||
}); | ||
|
||
thread::spawn(move || { | ||
let mut stderr = String::new(); | ||
while let Ok(line) = stderr_rx.recv() { | ||
println!("{}", line); | ||
stderr.push_str(&line); | ||
stderr.push_str("\n"); | ||
} | ||
if out == Output::Stderr && last_cmd { | ||
tx.send(stderr).unwrap(); | ||
} | ||
}); | ||
|
||
thread::spawn(move || { | ||
let reader = BufReader::new(stdout); | ||
for line in reader.lines() { | ||
stdout_tx_clone.send(line.unwrap()).unwrap(); | ||
} | ||
}); | ||
|
||
thread::spawn(move || { | ||
let reader = BufReader::new(stderr); | ||
for line in reader.lines() { | ||
stderr_tx.send(line.unwrap()).unwrap(); | ||
} | ||
}); | ||
|
||
child.wait().map_err(Error::from) | ||
} | ||
|
||
fn setup(&self) -> Result<(), Error> { | ||
Pkgx::default().install(vec!["curl"])?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.