task/manager/
properties.rs

1// Properties module - handles task properties like user, group, and environment variables
2
3use alloc::string::ToString;
4
5use super::*;
6use alloc::{string::String, vec::Vec};
7use users::{GroupIdentifier, UserIdentifier};
8
9impl Manager {
10    pub async fn set_user(
11        &self,
12        task_identifier: TaskIdentifier,
13        user: UserIdentifier,
14    ) -> Result<()> {
15        Self::get_task_mutable(&mut *self.0.write().await, task_identifier)?.user = user;
16
17        Ok(())
18    }
19
20    pub async fn set_group(
21        &self,
22        task_identifier: TaskIdentifier,
23        group: GroupIdentifier,
24    ) -> Result<()> {
25        Self::get_task_mutable(&mut *self.0.write().await, task_identifier)?.group = group;
26
27        Ok(())
28    }
29
30    pub async fn set_environment_variable(
31        &self,
32        task_identifier: TaskIdentifier,
33        name: &str,
34        value: &str,
35    ) -> Result<()> {
36        let environment_variable = EnvironmentVariable::new(name, value);
37
38        // Keep the write lock for the entire operation
39        let mut inner = self.0.write().await;
40        let metadata = Self::get_task_mutable(&mut inner, task_identifier)?;
41
42        // We remove any existing environment variable with the same name
43        metadata
44            .environment_variables
45            .retain(|variable| variable.get_name() != name);
46        // Add the new environment variable
47        metadata.environment_variables.push(environment_variable);
48
49        Ok(())
50    }
51
52    pub async fn set_environment_variables(
53        &self,
54        task_identifier: TaskIdentifier,
55        environment_variables: &[(&str, &str)],
56    ) -> Result<()> {
57        let mut inner = self.0.write().await;
58        let metadata = Self::get_task_mutable(&mut inner, task_identifier)?;
59
60        environment_variables.iter().for_each(|(name, value)| {
61            let environment_variable = EnvironmentVariable::new(name, value);
62            metadata.environment_variables.push(environment_variable);
63        });
64
65        Ok(())
66    }
67
68    pub async fn remove_environment_variable(
69        &self,
70        task_identifier: TaskIdentifier,
71        name: &str,
72    ) -> Result<()> {
73        Self::get_task_mutable(&mut *self.0.write().await, task_identifier)?
74            .environment_variables
75            .retain(|variable| variable.get_name() != name);
76
77        Ok(())
78    }
79
80    /// Get user identifier of the owner of a task.
81    pub async fn get_user(&self, task_identifier: TaskIdentifier) -> Result<UserIdentifier> {
82        Self::get_task(&*self.0.read().await, task_identifier).map(|task| task.user)
83    }
84
85    /// Get group identifier of the owner of a task.
86    pub async fn get_group(&self, task_identifier: TaskIdentifier) -> Result<GroupIdentifier> {
87        Self::get_task(&*self.0.read().await, task_identifier).map(|task| task.group)
88    }
89
90    pub async fn get_environment_variable(
91        &self,
92        task_identifier: TaskIdentifier,
93        name: &str,
94    ) -> Result<EnvironmentVariable> {
95        Self::get_task(&*self.0.read().await, task_identifier)?
96            .environment_variables
97            .iter()
98            .find(|variable| variable.get_name() == name)
99            .cloned()
100            // If the variable is not found, return an error
101            .ok_or(Error::InvalidEnvironmentVariable)
102    }
103
104    pub async fn get_environment_variables(
105        &self,
106        task_identifier: TaskIdentifier,
107    ) -> Result<Vec<EnvironmentVariable>> {
108        Self::get_task(&*self.0.read().await, task_identifier)
109            .map(|task| task.environment_variables.clone())
110            .map_err(|_| Error::InvalidTaskIdentifier)
111    }
112
113    /// # Arguments
114    /// * `Task_identifier` - The identifier of the task.
115    pub async fn get_name(&self, task_identifier: TaskIdentifier) -> Result<String> {
116        Self::get_task(&*self.0.read().await, task_identifier).map(|task| task.name.to_string())
117    }
118}