Folder names (#318)

This commit is contained in:
Wyatt Verchere
2023-07-21 20:16:07 -07:00
committed by GitHub
parent 4941260805
commit 3fa33dc241
42 changed files with 1129 additions and 535 deletions

View File

@@ -1,6 +1,6 @@
use crate::{
util::io::{self, IOError},
State,
{state::ProfilePathId, State},
};
use serde::{Deserialize, Serialize};
@@ -11,7 +11,7 @@ pub struct Logs {
}
impl Logs {
async fn build(
profile_uuid: uuid::Uuid,
profile_subpath: &ProfilePathId,
datetime_string: String,
clear_contents: Option<bool>,
) -> crate::Result<Self> {
@@ -20,7 +20,7 @@ impl Logs {
None
} else {
Some(
get_output_by_datetime(profile_uuid, &datetime_string)
get_output_by_datetime(profile_subpath, &datetime_string)
.await?,
)
},
@@ -35,7 +35,18 @@ pub async fn get_logs(
clear_contents: Option<bool>,
) -> crate::Result<Vec<Logs>> {
let state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
let profile_path = if let Some(p) =
crate::profile::get_by_uuid(profile_uuid, None).await?
{
p.profile_id()
} else {
return Err(crate::ErrorKind::UnmanagedProfileError(
profile_uuid.to_string(),
)
.into());
};
let logs_folder = state.directories.profile_logs_dir(&profile_path).await?;
let mut logs = Vec::new();
if logs_folder.exists() {
for entry in std::fs::read_dir(&logs_folder)
@@ -48,7 +59,7 @@ pub async fn get_logs(
if let Some(datetime_string) = path.file_name() {
logs.push(
Logs::build(
profile_uuid,
&profile_path,
datetime_string.to_string_lossy().to_string(),
clear_contents,
)
@@ -69,9 +80,19 @@ pub async fn get_logs_by_datetime(
profile_uuid: uuid::Uuid,
datetime_string: String,
) -> crate::Result<Logs> {
let profile_path = if let Some(p) =
crate::profile::get_by_uuid(profile_uuid, None).await?
{
p.profile_id()
} else {
return Err(crate::ErrorKind::UnmanagedProfileError(
profile_uuid.to_string(),
)
.into());
};
Ok(Logs {
output: Some(
get_output_by_datetime(profile_uuid, &datetime_string).await?,
get_output_by_datetime(&profile_path, &datetime_string).await?,
),
datetime_string,
})
@@ -79,19 +100,31 @@ pub async fn get_logs_by_datetime(
#[tracing::instrument]
pub async fn get_output_by_datetime(
profile_uuid: uuid::Uuid,
profile_subpath: &ProfilePathId,
datetime_string: &str,
) -> crate::Result<String> {
let state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
let logs_folder =
state.directories.profile_logs_dir(profile_subpath).await?;
let path = logs_folder.join(datetime_string).join("stdout.log");
Ok(io::read_to_string(&path).await?)
}
#[tracing::instrument]
pub async fn delete_logs(profile_uuid: uuid::Uuid) -> crate::Result<()> {
let profile_path = if let Some(p) =
crate::profile::get_by_uuid(profile_uuid, None).await?
{
p.profile_id()
} else {
return Err(crate::ErrorKind::UnmanagedProfileError(
profile_uuid.to_string(),
)
.into());
};
let state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
let logs_folder = state.directories.profile_logs_dir(&profile_path).await?;
for entry in std::fs::read_dir(&logs_folder)
.map_err(|e| IOError::with_path(e, &logs_folder))?
{
@@ -109,8 +142,19 @@ pub async fn delete_logs_by_datetime(
profile_uuid: uuid::Uuid,
datetime_string: &str,
) -> crate::Result<()> {
let profile_path = if let Some(p) =
crate::profile::get_by_uuid(profile_uuid, None).await?
{
p.profile_id()
} else {
return Err(crate::ErrorKind::UnmanagedProfileError(
profile_uuid.to_string(),
)
.into());
};
let state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
let logs_folder = state.directories.profile_logs_dir(&profile_path).await?;
let path = logs_folder.join(datetime_string);
io::remove_dir_all(&path).await?;
Ok(())