1use std::path::{Path, PathBuf};
26
27pub const URL_FILE: &str = "plm.json";
29pub const TOKEN_FILE: &str = "plm-token";
31pub const URL_ENV: &str = "BREP_PLM_URL";
33pub const TOKEN_ENV: &str = "BREP_PLM_TOKEN";
35pub const TOKEN_PREFIX: &str = "plm_";
38
39pub fn app_config_dir() -> PathBuf {
49 config_dir_from(&|name| std::env::var_os(name), cfg!(windows))
50}
51
52fn config_dir_from(env: &dyn Fn(&str) -> Option<std::ffi::OsString>, windows: bool) -> PathBuf {
55 let var = |name: &str| env(name).filter(|v| !v.is_empty()).map(PathBuf::from);
56 var("XDG_CONFIG_HOME")
57 .or_else(|| if windows { var("APPDATA") } else { None })
58 .or_else(|| var("HOME").map(|h| h.join(".config")))
59 .unwrap_or_else(|| PathBuf::from("."))
60 .join("brep-app")
61}
62
63#[derive(Clone, Copy, Debug, PartialEq, Eq)]
66pub enum Source {
67 File,
68 Env,
69 Flag,
70}
71
72impl Source {
73 pub fn describe(self) -> &'static str {
74 match self {
75 Source::File => "config file",
76 Source::Env => "environment",
77 Source::Flag => "command line",
78 }
79 }
80}
81
82#[derive(Clone, PartialEq, Eq)]
84pub struct PlmConfig {
85 pub url: String,
87 pub url_source: Source,
88 pub token: Option<String>,
90 pub token_source: Option<Source>,
91}
92
93impl std::fmt::Debug for PlmConfig {
95 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96 f.debug_struct("PlmConfig")
97 .field("url", &self.url)
98 .field("url_source", &self.url_source)
99 .field("token", &self.token.as_ref().map(|_| "<redacted>"))
100 .field("token_source", &self.token_source)
101 .finish()
102 }
103}
104
105#[derive(Clone, Debug, Default, PartialEq, Eq)]
107pub struct Flags {
108 pub url: Option<String>,
109 pub token_file: Option<PathBuf>,
110}
111
112pub fn resolve(
117 dir: &Path,
118 env: &dyn Fn(&str) -> Option<String>,
119 flags: &Flags,
120) -> Result<Option<PlmConfig>, String> {
121 let env = |name: &str| env(name).filter(|v| !v.trim().is_empty());
122
123 let (raw_url, url_source) = if let Some(url) = &flags.url {
124 (url.clone(), Source::Flag)
125 } else if let Some(url) = env(URL_ENV) {
126 (url, Source::Env)
127 } else if let Some(url) = read_url_file(dir)? {
128 (url, Source::File)
129 } else {
130 return Ok(None);
131 };
132 let url = normalise_url(&raw_url).map_err(|e| format!("{} ({}): {e}", url_origin(url_source), raw_url))?;
133
134 let token = if let Some(path) = &flags.token_file {
135 Some((read_token_file(path, false)?.ok_or_else(|| format!("--plm-token-file {}: no such file", path.display()))?, Source::Flag))
136 } else if let Some(token) = env(TOKEN_ENV) {
137 Some((token, Source::Env))
138 } else {
139 read_token_file(&dir.join(TOKEN_FILE), true)?.map(|t| (t, Source::File))
140 };
141 let (token, token_source) = match token {
142 Some((token, source)) => {
143 let token = token.trim().to_string();
144 check_token(&token).map_err(|e| format!("{}: {e}", token_origin(source)))?;
145 (Some(token), Some(source))
146 }
147 None => (None, None),
148 };
149 Ok(Some(PlmConfig { url, url_source, token, token_source }))
150}
151
152pub fn write(dir: &Path, url: &str, token: Option<&str>) -> Result<(), String> {
158 let url = normalise_url(url)?;
159 if let Some(token) = token {
160 check_token(token.trim())?;
161 }
162 std::fs::create_dir_all(dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
163 let json = serde_json::to_string_pretty(&serde_json::json!({ "url": url })).expect("a string map serialises");
164 write_atomic(&dir.join(URL_FILE), format!("{json}\n").as_bytes(), false)?;
165 if let Some(token) = token {
166 write_atomic(&dir.join(TOKEN_FILE), format!("{}\n", token.trim()).as_bytes(), true)?;
167 }
168 Ok(())
169}
170
171pub fn repoint(dir: &Path, url: &str) -> Result<(String, bool), String> {
179 let url = normalise_url(url)?;
180 let before = read_url_file(dir).ok().flatten().and_then(|old| normalise_url(&old).ok());
181 let token = dir.join(TOKEN_FILE);
182 let drop_token = before.as_deref() != Some(url.as_str()) && token.exists();
183 if drop_token {
184 std::fs::remove_file(&token).map_err(|e| format!("cannot remove {}: {e}", token.display()))?;
185 }
186 write(dir, &url, None)?;
187 Ok((url, drop_token))
188}
189
190pub fn write_token(dir: &Path, token: &str) -> Result<(), String> {
192 check_token(token.trim())?;
193 std::fs::create_dir_all(dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
194 write_atomic(&dir.join(TOKEN_FILE), format!("{}\n", token.trim()).as_bytes(), true)
195}
196
197pub fn forget(dir: &Path) -> Result<Vec<PathBuf>, String> {
200 let mut removed = Vec::new();
201 for name in [URL_FILE, TOKEN_FILE] {
202 let path = dir.join(name);
203 match std::fs::remove_file(&path) {
204 Ok(()) => removed.push(path),
205 Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
206 Err(e) => return Err(format!("cannot remove {}: {e}", path.display())),
207 }
208 }
209 Ok(removed)
210}
211
212pub fn describe(config: Option<&PlmConfig>, dir: &Path) -> String {
216 let Some(config) = config else {
217 return format!("no PLM configured (no {URL_ENV}, no {} in {}): the file-based app\n", URL_FILE, dir.display());
218 };
219 let token = match (&config.token, config.token_source) {
220 (Some(token), Some(source)) => {
221 let shown: String = token.chars().take(TOKEN_PREFIX.len() + 4).collect();
222 format!("{shown}… (from the {})", source.describe())
223 }
224 _ => "none — sign in from the app".to_string(),
225 };
226 format!("url: {} (from the {})\ntoken: {token}\n", config.url, config.url_source.describe())
227}
228
229pub fn url_origin(source: Source) -> String {
232 match source {
233 Source::Flag => "--plm-url".into(),
234 Source::Env => URL_ENV.into(),
235 Source::File => URL_FILE.into(),
236 }
237}
238
239fn token_origin(source: Source) -> String {
240 match source {
241 Source::Flag => "--plm-token-file".into(),
242 Source::Env => TOKEN_ENV.into(),
243 Source::File => TOKEN_FILE.into(),
244 }
245}
246
247fn normalise_url(raw: &str) -> Result<String, String> {
250 let url = raw.trim().trim_end_matches('/');
251 let rest = url
252 .strip_prefix("https://")
253 .or_else(|| url.strip_prefix("http://"))
254 .ok_or("the PLM address must start with http:// or https://")?;
255 if rest.is_empty() || rest.starts_with('/') {
256 return Err("the PLM address has no host".into());
257 }
258 if rest.contains(['?', '#', ' ']) {
259 return Err("the PLM address must be the server's base URL, with no query, fragment or spaces".into());
260 }
261 Ok(url.to_string())
262}
263
264fn check_token(token: &str) -> Result<(), String> {
265 if !token.starts_with(TOKEN_PREFIX) || token.len() <= TOKEN_PREFIX.len() {
266 return Err(format!("not a PLM API token (they start with `{TOKEN_PREFIX}`)"));
267 }
268 if token.chars().any(|c| c.is_whitespace() || c.is_control()) {
269 return Err("a PLM API token is one word; this one holds whitespace".into());
270 }
271 Ok(())
272}
273
274fn read_url_file(dir: &Path) -> Result<Option<String>, String> {
275 let path = dir.join(URL_FILE);
276 let text = match std::fs::read_to_string(&path) {
277 Ok(text) => text,
278 Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
279 Err(e) => return Err(format!("cannot read {}: {e}", path.display())),
280 };
281 let value: serde_json::Value =
282 serde_json::from_str(&text).map_err(|e| format!("{} is not JSON: {e}", path.display()))?;
283 match value.get("url") {
284 Some(serde_json::Value::String(url)) if !url.trim().is_empty() => Ok(Some(url.clone())),
285 _ => Err(format!("{} has no \"url\" string", path.display())),
286 }
287}
288
289fn read_token_file(path: &Path, strict_mode: bool) -> Result<Option<String>, String> {
293 let text = match std::fs::read_to_string(path) {
294 Ok(text) => text,
295 Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
296 Err(e) => return Err(format!("cannot read {}: {e}", path.display())),
297 };
298 #[cfg(unix)]
299 if strict_mode {
300 use std::os::unix::fs::PermissionsExt;
301 let mode = std::fs::metadata(path).map_err(|e| format!("cannot read {}: {e}", path.display()))?.permissions().mode();
302 if mode & 0o077 != 0 {
303 return Err(format!(
304 "{} is readable by other users (mode {:o}); run `chmod 600 {}` or sign in again",
305 path.display(),
306 mode & 0o777,
307 path.display()
308 ));
309 }
310 }
311 #[cfg(not(unix))]
312 let _ = strict_mode;
313 Ok(Some(text))
314}
315
316fn write_atomic(path: &Path, bytes: &[u8], secret: bool) -> Result<(), String> {
317 crate::store::write_atomic(path, bytes, secret).map_err(|e| format!("cannot write {}: {e}", path.display()))
318}
319