From e148f143ea5f9bab23fb9e3ea403810d2eab029b Mon Sep 17 00:00:00 2001 From: J H Date: Mon, 25 Mar 2024 14:18:09 -0600 Subject: [PATCH] wip: Properties --- core/models/src/procedure_name.rs | 2 + core/startos/src/properties.rs | 66 ++++--------------------------- core/startos/src/service/mod.rs | 11 ++++++ 3 files changed, 21 insertions(+), 58 deletions(-) diff --git a/core/models/src/procedure_name.rs b/core/models/src/procedure_name.rs index bf69b06b8..c42068be3 100644 --- a/core/models/src/procedure_name.rs +++ b/core/models/src/procedure_name.rs @@ -9,6 +9,7 @@ pub enum ProcedureName { GetConfig, SetConfig, CreateBackup, + Properties, RestoreBackup, ActionMetadata, RunAction(ActionId), @@ -29,6 +30,7 @@ impl ProcedureName { ProcedureName::SetConfig => "/config/set".to_string(), ProcedureName::GetConfig => "/config/get".to_string(), ProcedureName::CreateBackup => "/backup/create".to_string(), + ProcedureName::Properties => "/properties".to_string(), ProcedureName::RestoreBackup => "/backup/restore".to_string(), ProcedureName::ActionMetadata => "/actions/metadata".to_string(), ProcedureName::RunAction(id) => format!("/actions/{}/run", id), diff --git a/core/startos/src/properties.rs b/core/startos/src/properties.rs index cfb0dd66b..5482bdb58 100644 --- a/core/startos/src/properties.rs +++ b/core/startos/src/properties.rs @@ -1,4 +1,4 @@ -use std::collections::BTreeMap; +use std::{borrow::Borrow, collections::BTreeMap}; use clap::Parser; use imbl_value::{json, InOMap, InternedString, Value}; @@ -15,48 +15,6 @@ pub fn display_properties(response: Value) { println!("{}", response); } -trait IntoProperties { - fn into_properties(self, store: &Value) -> Value; -} -impl IntoProperties for ExposedUI { - fn into_properties(self, store: &Value) -> Value { - match self { - ExposedUI::Object { value, description } => { - json!({ - "type": "object", - "description": description, - "value": value.into_iter().map(|(k, v)| (k, v.into_properties(store))).collect::>() - }) - } - ExposedUI::String { - path, - description, - masked, - copyable, - qr, - } => json!({ - "type": "string", - "description": description, - "value": path.get(store).cloned().unwrap_or_default(), - "copyable": copyable, - "qr": qr, - "masked": masked - }), - } - } -} - -impl IntoProperties for StoreExposedUI { - fn into_properties(self, store: &Value) -> Value { - Value::Object( - self.0 - .into_iter() - .map(|(k, v)| (k, v.into_properties(store))) - .collect::>(), - ) - } -} - #[derive(Deserialize, Serialize, Parser)] #[serde(rename_all = "camelCase")] #[command(rename_all = "kebab-case")] @@ -68,19 +26,11 @@ pub async fn properties( ctx: RpcContext, PropertiesParam { id }: PropertiesParam, ) -> Result { - let peeked = ctx.db.peek().await; - let data = peeked - .as_private() - .as_package_stores() - .as_idx(&id) - .map(|x| x.de()) - .unwrap_or_else(|| Ok(json!({})))?; - Ok(peeked - .as_public() - .as_package_data() - .as_idx(&id) - .or_not_found(&id)? - .as_store_exposed_ui() - .de()? - .into_properties(&data)) + match &*ctx.services.get(&id).await { + Some(service) => service.properties().await, + None => Err(Error::new( + eyre!("Could not find a service with id {id}"), + ErrorKind::NotFound, + )), + } } diff --git a/core/startos/src/service/mod.rs b/core/startos/src/service/mod.rs index fb521ca34..2feb4ca34 100644 --- a/core/startos/src/service/mod.rs +++ b/core/startos/src/service/mod.rs @@ -325,6 +325,17 @@ impl Service { .await .with_kind(ErrorKind::Action) } + pub async fn properties(&self) -> Result { + let container = &self.seed.persistent_container; + container + .execute::( + ProcedureName::Properties, + Value::Null, + Some(Duration::from_secs(30)), + ) + .await + .with_kind(ErrorKind::Unknown) + } pub async fn shutdown(self) -> Result<(), Error> { self.actor