creat ide file and read from them

This commit is contained in:
stubbfelnewpc
2020-06-29 20:55:53 +02:00
parent ee1a48a28e
commit 21353c171a

View File

@@ -10,8 +10,11 @@ use nixideserver_lib::*;
use rocket::http::Status;
use std::fs;
use std::hash::Hash;
use std::io::Read;
use std::path::PathBuf;
use std::io::prelude::*;
use std::path::{
Path,
PathBuf
};
// podman create --name test -p 3000:3000 -v $PWD:/nixide -w /nixide docker.io/nixos/nix nix-shell --pure start-ide.nix --run run_ide.sh
@@ -22,7 +25,7 @@ pub struct PodmanEngine {
impl PodmanEngine {
pub fn new(working_folder: PathBuf) -> Self {
Self {
working_folder: working_folder,
working_folder,
}
}
}
@@ -32,21 +35,29 @@ struct PodmanIdeStatus {
}
impl NixIdeManageServiceEngine for PodmanEngine {
fn fetch_current_ide_state(&self, _ide_id: &str) -> Result<IdeState, Status> {
Ok(IdeState::OPENING)
fn fetch_current_ide_state(&self, ide_id: &str) -> Result<IdeState, Status> {
let state_file = create_state_file_path( &self.working_folder, ide_id);
let mut file = fs::File::open(state_file).map_err(|_|Status::new(500, "could not open state file"))?;
let mut contents = String::new();
file.read_to_string(&mut contents).map_err(|_| Status::NotFound)?;
let ide_state = read_ide_state(&contents).map_err(|_|Status::new(500, "unexpected json string"))?;
Ok(ide_state)
}
fn start_open(&self, ide_id: &str, _param: &OpenGitParam) -> Result<IdeState, Status> {
let ide_folder = format!("{}/{}", &self.working_folder.display(), ide_id);
let state_file = format!("{}/.ide_state", ide_folder);
let ide_folder = create_ide_folder_path( &self.working_folder, ide_id);
let state_file = create_state_file_path_from_ide_folder(&ide_folder);
fs::create_dir_all(&ide_folder)
.and_then(|_| fs::File::create(state_file))
.and_then(|_| fs::OpenOptions::new().create(true).write(true).read(true).open(state_file))
.and_then(|mut file| {
let mut contents = String::new();
match file.read_to_string(&mut contents)? {
0 => Ok(IdeState::OPENING),
let mut buffer = String::new();
match file.read_to_string(&mut buffer) {
Ok(s) if s > 0 => {
read_ide_state(&buffer).map_err(|_| std::io::Error::from(std::io::ErrorKind::InvalidData))
},
_ => {
let status: PodmanIdeStatus = serde_json::from_str(&contents)?;
let status =PodmanIdeStatus{ide_state: IdeState::OPENING};
file.write(serde_json::to_string_pretty(&status)?.as_bytes())?;
Ok(status.ide_state)
}
}
@@ -100,3 +111,23 @@ fn default_project_folder() -> String {
fn default_working_folder() -> String {
"$PWD".to_owned()
}
fn create_state_file_path(working_folder_path : &Path, ide_id: &str) -> String {
let ide_folder = format!("{}/{}", working_folder_path.display(), ide_id);
create_state_file_path_from_ide_folder(&ide_folder)
}
fn create_state_file_path_from_ide_folder(ide_folder : &str) -> String {
format!("{}/.ide_state", ide_folder)
}
fn create_ide_folder_path(working_folder_path : &Path, ide_id: &str) -> String {
format!("{}/{}", working_folder_path.display(), ide_id)
}
fn read_ide_state(json_string :&str) -> Result<IdeState,serde_json::Error>
{
let status: PodmanIdeStatus = serde_json::from_str(json_string)?;
Ok(status.ide_state)
}