add git command error handling and find_first_mut_container
This commit is contained in:
@@ -45,9 +45,7 @@ pub struct PodmanEngine {
|
||||
|
||||
impl PodmanEngine {
|
||||
pub fn new(working_folder: PathBuf) -> PodmanEngineTraitImpl {
|
||||
PodmanEngineTraitImpl {
|
||||
working_folder
|
||||
}
|
||||
PodmanEngineTraitImpl { working_folder }
|
||||
}
|
||||
|
||||
fn _new(working_folder: PathBuf) -> Self {
|
||||
@@ -73,10 +71,12 @@ impl PodmanEngine {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn reload(trait_impl : &PodmanEngineTraitImpl) -> Result<Self, std::io::Error> {
|
||||
Ok(
|
||||
Self::_new(trait_impl.working_folder.clone())
|
||||
)
|
||||
pub fn reload(trait_impl: &PodmanEngineTraitImpl) -> Result<Self, std::io::Error> {
|
||||
Ok(Self::_new(trait_impl.working_folder.clone()))
|
||||
}
|
||||
|
||||
fn find_mut_first_container(&mut self, ide_id: &str) -> Option<&mut PodmanContainer> {
|
||||
self.list.container.iter_mut().find(|i| i.ide_id.eq(ide_id))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,23 +86,23 @@ struct PodmanIdeStatus {
|
||||
}
|
||||
|
||||
pub struct PodmanEngineTraitImpl {
|
||||
working_folder: PathBuf
|
||||
working_folder: PathBuf,
|
||||
}
|
||||
|
||||
impl NixIdeManageServiceEngine for PodmanEngineTraitImpl {
|
||||
fn fetch_current_ide_state(&self, ide_id: &str) -> Result<IdeState, Status> {
|
||||
let eng = PodmanEngine::reload(self)
|
||||
let mut eng = PodmanEngine::reload(self)
|
||||
.map_err(|_| Status::new(500, "internal error : reload engine"))?;
|
||||
match eng.list.container.iter().find(|i| i.ide_id.eq(ide_id)) {
|
||||
match eng.find_mut_first_container(ide_id) {
|
||||
Some(i) => Ok(i.ide_state.clone()),
|
||||
_ => Err(Status::NotFound),
|
||||
}
|
||||
}
|
||||
|
||||
fn start_open(&self, ide_id: &str, param: &OpenGitParam) -> Result<IdeState, Status> {
|
||||
let mut eng = PodmanEngine::reload(self)
|
||||
let mut eng = PodmanEngine::reload(self)
|
||||
.map_err(|_| Status::new(500, "internal error : reload engine"))?;
|
||||
match eng.list.container.iter().find(|i| i.ide_id.eq(ide_id)) {
|
||||
match eng.find_mut_first_container(ide_id) {
|
||||
Some(i) => Ok(i.ide_state.clone()),
|
||||
_ => {
|
||||
let ide_folder = create_ide_folder_path(&self.working_folder, ide_id);
|
||||
@@ -130,13 +130,14 @@ impl NixIdeManageServiceEngine for PodmanEngineTraitImpl {
|
||||
state: PodmanState::FetchingSource,
|
||||
ide_param,
|
||||
};
|
||||
|
||||
eng.list.container.push(container);
|
||||
eng.save()
|
||||
.map_err(|_| Status::new(500, "internal error: can't save curent state"))?;
|
||||
|
||||
fetch_sources(&format!("{}/repo", ide_folder), param)
|
||||
.map_err(|_| Status::new(500, "internal error"))?;
|
||||
match eng.list.container.iter_mut().find(|i| i.ide_id.eq(ide_id)) {
|
||||
match eng.find_mut_first_container(ide_id) {
|
||||
Some(i) => {
|
||||
i.state = PodmanState::StartingContainer;
|
||||
eng.save().map_err(|_| {
|
||||
@@ -223,20 +224,26 @@ fn read_from_list_file(path: &str) -> Result<PodmanContainerList, std::io::Error
|
||||
}
|
||||
|
||||
fn fetch_sources(repo_path: &str, param: &OpenGitParam) -> Result<(), std::io::Error> {
|
||||
Command::new("git")
|
||||
let cmd_result = match Command::new("git")
|
||||
.arg("clone")
|
||||
.arg("-n")
|
||||
.arg(¶m.clone_url)
|
||||
.arg(repo_path)
|
||||
.output()?;
|
||||
.status()
|
||||
{
|
||||
Ok(s) if s.success() => Command::new("git")
|
||||
.current_dir(repo_path)
|
||||
.arg("checkout")
|
||||
.arg(¶m.ref_name)
|
||||
.status(),
|
||||
e => e,
|
||||
};
|
||||
|
||||
Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(repo_path)
|
||||
.arg("checkout")
|
||||
.arg(¶m.ref_name)
|
||||
.output()
|
||||
.map(|_| ())
|
||||
match cmd_result {
|
||||
Ok(s) if s.success() => Ok(()),
|
||||
Err(e) => Err(e),
|
||||
_ => Err(std::io::Error::from(std::io::ErrorKind::InvalidData)),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -245,7 +252,7 @@ fn test_fetch_sources() {
|
||||
.duration_since(std::time::SystemTime::UNIX_EPOCH)
|
||||
.unwrap();
|
||||
let temp_dir = std::env::temp_dir();
|
||||
let repo_path = format!("{}/{}", temp_dir.display(), now.as_secs());
|
||||
let repo_path = format!("{}/{}", temp_dir.display(), now.as_nanos());
|
||||
let git_param = OpenGitParam {
|
||||
clone_url: "https://github.com/jmesmon/rust-hello-world.git".to_owned(),
|
||||
inquirer: "stubbfel".to_owned(),
|
||||
@@ -253,3 +260,33 @@ fn test_fetch_sources() {
|
||||
};
|
||||
assert_eq!(fetch_sources(&repo_path, &git_param).unwrap(), ());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fetch_sources_wrong_ref() {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::SystemTime::UNIX_EPOCH)
|
||||
.unwrap();
|
||||
let temp_dir = std::env::temp_dir();
|
||||
let repo_path = format!("{}/{}", temp_dir.display(), now.as_nanos());
|
||||
let git_param = OpenGitParam {
|
||||
clone_url: "https://github.com/jmesmon/rust-hello-world.git".to_owned(),
|
||||
inquirer: "stubbfel".to_owned(),
|
||||
ref_name: "master2".to_owned(),
|
||||
};
|
||||
assert_eq!(fetch_sources(&repo_path, &git_param).is_err(), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fetch_sources_wrong_url() {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::SystemTime::UNIX_EPOCH)
|
||||
.unwrap();
|
||||
let temp_dir = std::env::temp_dir();
|
||||
let repo_path = format!("{}/{}", temp_dir.display(), now.as_nanos());
|
||||
let git_param = OpenGitParam {
|
||||
clone_url: "https://bar.com/foo.git".to_owned(),
|
||||
inquirer: "stubbfel".to_owned(),
|
||||
ref_name: "master".to_owned(),
|
||||
};
|
||||
assert_eq!(fetch_sources(&repo_path, &git_param).is_err(), true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user