Skip to main content

upstream_rs/application/
context.rs

1use anyhow::Result;
2
3use crate::{
4    providers::provider_manager::ProviderManager,
5    services::trust::TrustedSignatureKeys,
6    storage::{
7        database::PackageDatabase,
8        system::{config::ConfigStorage, trust::TrustStorage},
9    },
10    utils::static_paths::UpstreamPaths,
11};
12
13pub struct CommandContext {
14    pub paths: UpstreamPaths,
15    pub provider_manager: ProviderManager,
16}
17
18impl CommandContext {
19    pub fn new() -> Result<Self> {
20        let paths = UpstreamPaths::new()?;
21        let config = ConfigStorage::new(&paths.config.config_file)?;
22        let app_config = config.get_config();
23        let provider_manager = ProviderManager::new(
24            app_config.github.api_token.as_deref(),
25            app_config.gitlab.api_token.as_deref(),
26            app_config.gitea.api_token.as_deref(),
27            app_config.download,
28        )?;
29
30        Ok(Self {
31            paths,
32            provider_manager,
33        })
34    }
35
36    pub fn package_database(&self) -> Result<PackageDatabase> {
37        PackageDatabase::open(&self.paths.config.packages_database_file)
38    }
39
40    pub fn trust_storage(&self) -> Result<TrustStorage> {
41        TrustStorage::new(&self.paths.config.trust_file)
42    }
43
44    pub fn trusted_keys(&self) -> Result<TrustedSignatureKeys> {
45        Ok(self.trust_storage()?.trusted_signature_keys())
46    }
47}