mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-27 11:07:03 +02:00
Ability to sync environments to folder (#207)
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Error::GenericError;
|
||||
use crate::error::Error::{MissingBaseEnvironment, MultipleBaseEnvironments};
|
||||
use crate::error::Result;
|
||||
use crate::models::{Environment, EnvironmentIden};
|
||||
use crate::models::{Environment, EnvironmentIden, EnvironmentVariable};
|
||||
use crate::util::UpdateSource;
|
||||
use log::info;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_environment(&self, id: &str) -> Result<Environment> {
|
||||
@@ -10,35 +11,41 @@ impl<'a> DbContext<'a> {
|
||||
}
|
||||
|
||||
pub fn get_base_environment(&self, workspace_id: &str) -> Result<Environment> {
|
||||
// Will create base environment if it doesn't exist
|
||||
let environments = self.list_environments(workspace_id)?;
|
||||
let environments = self.list_environments_ensure_base(workspace_id)?;
|
||||
let base_environments =
|
||||
environments.into_iter().filter(|e| e.base).collect::<Vec<Environment>>();
|
||||
|
||||
let base_environment = environments
|
||||
.into_iter()
|
||||
.find(|e| e.environment_id == None && e.workspace_id == workspace_id)
|
||||
.ok_or(GenericError(format!("No base environment found for {workspace_id}")))?;
|
||||
if base_environments.len() > 1 {
|
||||
return Err(MultipleBaseEnvironments(workspace_id.to_string()));
|
||||
}
|
||||
|
||||
let base_environment = base_environments.into_iter().find(|e| e.base).ok_or(
|
||||
// Should never happen because one should be created above if it does not exist
|
||||
MissingBaseEnvironment(workspace_id.to_string()),
|
||||
)?;
|
||||
|
||||
Ok(base_environment)
|
||||
}
|
||||
|
||||
pub fn list_environments(&self, workspace_id: &str) -> Result<Vec<Environment>> {
|
||||
/// Lists environments and will create a base environment if one doesn't exist
|
||||
pub fn list_environments_ensure_base(&self, workspace_id: &str) -> Result<Vec<Environment>> {
|
||||
let mut environments =
|
||||
self.find_many::<Environment>(EnvironmentIden::WorkspaceId, workspace_id, None)?;
|
||||
|
||||
let base_environment = environments
|
||||
.iter()
|
||||
.find(|e| e.environment_id == None && e.workspace_id == workspace_id);
|
||||
let base_environment = environments.iter().find(|e| e.base);
|
||||
|
||||
if let None = base_environment {
|
||||
environments.push(self.upsert_environment(
|
||||
let e = self.upsert_environment(
|
||||
&Environment {
|
||||
workspace_id: workspace_id.to_string(),
|
||||
environment_id: None,
|
||||
base: true,
|
||||
name: "Global Variables".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
&UpdateSource::Background,
|
||||
)?);
|
||||
)?;
|
||||
info!("Created base environment {} for {workspace_id}", e.id);
|
||||
environments.push(e);
|
||||
}
|
||||
|
||||
Ok(environments)
|
||||
@@ -49,12 +56,12 @@ impl<'a> DbContext<'a> {
|
||||
environment: &Environment,
|
||||
source: &UpdateSource,
|
||||
) -> Result<Environment> {
|
||||
for environment in
|
||||
self.find_many::<Environment>(EnvironmentIden::EnvironmentId, &environment.id, None)?
|
||||
{
|
||||
self.delete_environment(&environment, source)?;
|
||||
}
|
||||
self.delete(environment, source)
|
||||
let deleted_environment = self.delete(environment, source)?;
|
||||
|
||||
// Recreate the base environment if we happened to delete it
|
||||
self.list_environments_ensure_base(&environment.workspace_id)?;
|
||||
|
||||
Ok(deleted_environment)
|
||||
}
|
||||
|
||||
pub fn delete_environment_by_id(&self, id: &str, source: &UpdateSource) -> Result<Environment> {
|
||||
@@ -69,7 +76,7 @@ impl<'a> DbContext<'a> {
|
||||
) -> Result<Environment> {
|
||||
let mut environment = environment.clone();
|
||||
environment.id = "".to_string();
|
||||
self.upsert(&environment, source)
|
||||
self.upsert_environment(&environment, source)
|
||||
}
|
||||
|
||||
pub fn upsert_environment(
|
||||
@@ -77,6 +84,18 @@ impl<'a> DbContext<'a> {
|
||||
environment: &Environment,
|
||||
source: &UpdateSource,
|
||||
) -> Result<Environment> {
|
||||
self.upsert(environment, source)
|
||||
let cleaned_variables = environment
|
||||
.variables
|
||||
.iter()
|
||||
.filter(|v| !v.name.is_empty() || !v.value.is_empty())
|
||||
.cloned()
|
||||
.collect::<Vec<EnvironmentVariable>>();
|
||||
self.upsert(
|
||||
&Environment {
|
||||
variables: cleaned_variables,
|
||||
..environment.clone()
|
||||
},
|
||||
source,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user