Skip to main content

adk_deploy/
config.rs

1use std::{fs, path::PathBuf};
2
3use serde::{Deserialize, Serialize};
4
5use crate::{DeployError, DeployResult};
6
7#[derive(Debug, Clone, Serialize, Deserialize, Default)]
8#[serde(rename_all = "camelCase")]
9pub struct DeployClientConfig {
10    pub endpoint: String,
11    #[serde(default, skip_serializing_if = "Option::is_none")]
12    pub token: Option<String>,
13    #[serde(default, skip_serializing_if = "Option::is_none")]
14    pub workspace_id: Option<String>,
15}
16
17impl DeployClientConfig {
18    pub fn default_path() -> DeployResult<PathBuf> {
19        let base = dirs::config_dir().ok_or_else(|| DeployError::Config {
20            message: "could not determine config directory".to_string(),
21        })?;
22        Ok(base.join("adk-deploy").join("config.json"))
23    }
24
25    pub fn load() -> DeployResult<Self> {
26        let path = Self::default_path()?;
27        if !path.exists() {
28            return Ok(Self {
29                endpoint: "http://127.0.0.1:8090".to_string(),
30                token: None,
31                workspace_id: None,
32            });
33        }
34        let raw = fs::read_to_string(path)?;
35        serde_json::from_str(&raw)
36            .map_err(|error| DeployError::Config { message: error.to_string() })
37    }
38
39    pub fn save(&self) -> DeployResult<()> {
40        let path = Self::default_path()?;
41        if let Some(parent) = path.parent() {
42            fs::create_dir_all(parent)?;
43        }
44        let payload = serde_json::to_string_pretty(self)
45            .map_err(|error| DeployError::Config { message: error.to_string() })?;
46        fs::write(path, payload)?;
47        Ok(())
48    }
49}