creat ide file and read from them
This commit is contained in:
@@ -10,8 +10,11 @@ use nixideserver_lib::*;
|
|||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::io::Read;
|
use std::io::prelude::*;
|
||||||
use std::path::PathBuf;
|
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
|
// 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 {
|
impl PodmanEngine {
|
||||||
pub fn new(working_folder: PathBuf) -> Self {
|
pub fn new(working_folder: PathBuf) -> Self {
|
||||||
Self {
|
Self {
|
||||||
working_folder: working_folder,
|
working_folder,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,21 +35,29 @@ struct PodmanIdeStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl NixIdeManageServiceEngine for PodmanEngine {
|
impl NixIdeManageServiceEngine for PodmanEngine {
|
||||||
fn fetch_current_ide_state(&self, _ide_id: &str) -> Result<IdeState, Status> {
|
fn fetch_current_ide_state(&self, ide_id: &str) -> Result<IdeState, Status> {
|
||||||
Ok(IdeState::OPENING)
|
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> {
|
fn start_open(&self, ide_id: &str, _param: &OpenGitParam) -> Result<IdeState, Status> {
|
||||||
let ide_folder = format!("{}/{}", &self.working_folder.display(), ide_id);
|
let ide_folder = create_ide_folder_path( &self.working_folder, ide_id);
|
||||||
let state_file = format!("{}/.ide_state", ide_folder);
|
let state_file = create_state_file_path_from_ide_folder(&ide_folder);
|
||||||
fs::create_dir_all(&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| {
|
.and_then(|mut file| {
|
||||||
let mut contents = String::new();
|
let mut buffer = String::new();
|
||||||
match file.read_to_string(&mut contents)? {
|
match file.read_to_string(&mut buffer) {
|
||||||
0 => Ok(IdeState::OPENING),
|
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)
|
Ok(status.ide_state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,3 +111,23 @@ fn default_project_folder() -> String {
|
|||||||
fn default_working_folder() -> String {
|
fn default_working_folder() -> String {
|
||||||
"$PWD".to_owned()
|
"$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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user