mirror of
https://gitlab.com/gabmus/envision.git
synced 2025-08-03 14:49:04 +00:00
feat: common function to get file writer
This commit is contained in:
parent
e005208dd9
commit
77289d2f45
3 changed files with 53 additions and 33 deletions
40
src/file_utils.rs
Normal file
40
src/file_utils.rs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
use std::{
|
||||||
|
fs::{create_dir_all, OpenOptions},
|
||||||
|
io::BufWriter,
|
||||||
|
path::Path, env,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::constants::CMD_NAME;
|
||||||
|
|
||||||
|
pub fn get_writer(path_s: &String) -> BufWriter<std::fs::File> {
|
||||||
|
let path = Path::new(path_s);
|
||||||
|
match path.parent() {
|
||||||
|
Some(parent) => {
|
||||||
|
if !parent.is_dir() {
|
||||||
|
create_dir_all(parent).expect("Could not create dir")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
};
|
||||||
|
let file = OpenOptions::new()
|
||||||
|
.write(true)
|
||||||
|
.create(true)
|
||||||
|
.open(path)
|
||||||
|
.expect("Could not open file");
|
||||||
|
BufWriter::new(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_config_dir() -> String {
|
||||||
|
match env::var("XDG_CONFIG_HOME") {
|
||||||
|
Ok(conf_home) => format!(
|
||||||
|
"{chome}/{name}",
|
||||||
|
chome = conf_home,
|
||||||
|
name = CMD_NAME
|
||||||
|
),
|
||||||
|
Err(_) => format!(
|
||||||
|
"{home}/.config/{name}",
|
||||||
|
home = env::var("HOME").expect("HOME env var not defined"),
|
||||||
|
name = CMD_NAME
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,10 +3,12 @@ use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fs::{create_dir_all, File, OpenOptions},
|
fs::{create_dir_all, File, OpenOptions},
|
||||||
io::{BufReader, BufWriter},
|
io::{BufReader, BufWriter},
|
||||||
path::Path,
|
path::Path, fmt::Display,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
use crate::file_utils::get_writer;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct Profile {
|
pub struct Profile {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub monado_path: String,
|
pub monado_path: String,
|
||||||
|
@ -20,6 +22,12 @@ pub struct Profile {
|
||||||
pub environment: HashMap<String, String>,
|
pub environment: HashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for Profile {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.write_str(&self.name.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_profile(path: &String) -> Result<Profile, serde_json::Error> {
|
pub fn load_profile(path: &String) -> Result<Profile, serde_json::Error> {
|
||||||
let file = File::open(path).unwrap();
|
let file = File::open(path).unwrap();
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
|
@ -27,21 +35,7 @@ pub fn load_profile(path: &String) -> Result<Profile, serde_json::Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dump_profile(profile: Profile, path_s: &String) -> () {
|
pub fn dump_profile(profile: Profile, path_s: &String) -> () {
|
||||||
let path = Path::new(path_s);
|
let writer = get_writer(path_s);
|
||||||
match path.parent() {
|
|
||||||
Some(parent) => {
|
|
||||||
if !parent.is_dir() {
|
|
||||||
create_dir_all(parent).expect("Could not create dir")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {}
|
|
||||||
};
|
|
||||||
let file = OpenOptions::new()
|
|
||||||
.write(true)
|
|
||||||
.create(true)
|
|
||||||
.open(path)
|
|
||||||
.expect("Could not open file");
|
|
||||||
let writer = BufWriter::new(file);
|
|
||||||
serde_json::to_writer_pretty(writer, &profile).expect("Could not write profile")
|
serde_json::to_writer_pretty(writer, &profile).expect("Could not write profile")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ use std::{
|
||||||
process::{Child, Command, Stdio},
|
process::{Child, Command, Stdio},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::profile::Profile;
|
use crate::{profile::Profile, file_utils::get_writer};
|
||||||
|
|
||||||
pub struct Runner {
|
pub struct Runner {
|
||||||
environment: HashMap<String, String>,
|
environment: HashMap<String, String>,
|
||||||
|
@ -118,21 +118,7 @@ impl Runner {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_log(path_s: String, log: &Vec<String>) -> Result<(), std::io::Error> {
|
fn save_log(path_s: String, log: &Vec<String>) -> Result<(), std::io::Error> {
|
||||||
let path = Path::new(&path_s);
|
let mut writer = get_writer(&path_s);
|
||||||
match path.parent() {
|
|
||||||
Some(parent) => {
|
|
||||||
if !parent.is_dir() {
|
|
||||||
create_dir_all(parent).expect("Could not create dir")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {}
|
|
||||||
};
|
|
||||||
let file = OpenOptions::new()
|
|
||||||
.write(true)
|
|
||||||
.create(true)
|
|
||||||
.open(path)
|
|
||||||
.expect("Could not open file");
|
|
||||||
let mut writer = BufWriter::new(file);
|
|
||||||
let log_s = log.concat();
|
let log_s = log.concat();
|
||||||
writer.write_all(log_s.as_ref())
|
writer.write_all(log_s.as_ref())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue