Skip to main content

ghactions_toolcache/
builder.rs

1//! # ToolCacheBuilder
2//!
3//! A builder for the ToolCache struct.
4//!
5//! This allows you to customize the ToolCache instance before creating it.
6//!
7//! # Example
8//!
9//! ```no_run
10//! # #[cfg(feature = "download")] {
11//! # use anyhow::Result;
12//! use ghactions_toolcache::ToolCache;
13//!
14//! # #[tokio::main]
15//! # async fn main() -> Result<()> {
16//!
17//! // Create a new ToolCache instance using the builder
18//! let tool_cache = ToolCache::build()
19//!     .retry_count(5)
20//!     .client(reqwest::Client::new())
21//!     .build();
22//!
23//! # Ok(())
24//! # }
25//! # }
26//! ```
27use std::path::PathBuf;
28#[cfg(feature = "download")]
29use std::sync::{Arc, OnceLock};
30
31use crate::{
32    ToolCache, ToolCacheArch, ToolPlatform,
33    cache::{RETRY_COUNT, get_tool_cache_path},
34};
35
36/// A builder for the ToolCache struct.
37///
38/// This allows you to customize the ToolCache instance before creating it.
39#[derive(Debug, Clone, Default)]
40pub struct ToolCacheBuilder {
41    pub(crate) tool_cache: Option<PathBuf>,
42    pub(crate) arch: Option<crate::ToolCacheArch>,
43    pub(crate) platform: Option<crate::platform::ToolPlatform>,
44
45    pub(crate) retry_count: Option<u8>,
46    #[cfg(feature = "download")]
47    pub(crate) client: Option<reqwest::Client>,
48}
49
50impl ToolCacheBuilder {
51    /// Create a new ToolCacheBuilder
52    pub fn new() -> Self {
53        Self::default()
54    }
55
56    /// Sets the path to the tool cache directory.
57    ///
58    /// # Parameters
59    /// - `path`: The path to use for the tool cache directory.
60    pub fn tool_cache(mut self, path: impl Into<PathBuf>) -> Self {
61        self.tool_cache = Some(path.into());
62        self
63    }
64
65    /// Sets the architecture for the tool cache.
66    ///
67    /// # Parameters
68    /// - `arch`: The architecture to use (e.g., x64, arm64).
69    pub fn arch(mut self, arch: crate::ToolCacheArch) -> Self {
70        self.arch = Some(arch);
71        self
72    }
73
74    /// Sets the platform for the tool cache.
75    ///
76    /// # Parameters
77    /// - `platform`: The platform to use (e.g., Windows, Linux, macOS).
78    pub fn platform(mut self, platform: crate::platform::ToolPlatform) -> Self {
79        self.platform = Some(platform);
80        self
81    }
82
83    /// Sets the number of retry attempts for cache operations.
84    ///
85    /// # Parameters
86    /// - `count`: The number of retries to attempt.
87    pub fn retry_count(mut self, count: u8) -> Self {
88        self.retry_count = Some(count);
89        self
90    }
91
92    /// Sets the HTTP client to use for downloading tools.
93    ///
94    /// # Parameters
95    /// - `client`: The `reqwest::Client` instance to use.
96    #[cfg(feature = "download")]
97    pub fn client(mut self, client: reqwest::Client) -> Self {
98        self.client = Some(client);
99        self
100    }
101
102    /// Build the ToolCache
103    pub fn build(&self) -> ToolCache {
104        let tool_cache = self.tool_cache.clone().unwrap_or_else(get_tool_cache_path);
105        let arch = self.arch.unwrap_or(match std::env::consts::ARCH {
106            "x86_64" | "amd64" => ToolCacheArch::X64,
107            "aarch64" => ToolCacheArch::ARM64,
108            _ => ToolCacheArch::Any,
109        });
110
111        let platform = self.platform.unwrap_or_else(ToolPlatform::from_current_os);
112
113        #[cfg(feature = "download")]
114        let client = {
115            let client = OnceLock::new();
116            if let Some(reqwest_client) = self.client.clone() {
117                let _ = client.set(reqwest_client);
118            }
119            Arc::new(client)
120        };
121
122        ToolCache {
123            tool_cache,
124            arch,
125            platform,
126            retry_count: self.retry_count.unwrap_or(RETRY_COUNT),
127            #[cfg(feature = "download")]
128            client,
129        }
130    }
131}