Skip to main content

upstream_rs/application/
context.rs

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