CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
88 lines
3.3 KiB
Rust
88 lines
3.3 KiB
Rust
use anyhow::{bail, Context, Result};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::{fs, path::PathBuf};
|
|
|
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
|
pub struct Config {
|
|
#[serde(default)] pub server_url: Option<String>,
|
|
#[serde(default)] pub device_token: Option<String>,
|
|
#[serde(default)] pub accept_invalid_certs: bool,
|
|
#[serde(default)] pub update_checked_at: u64,
|
|
#[serde(default)] pub update_latest: Option<String>,
|
|
}
|
|
|
|
impl Config {
|
|
fn path() -> Result<PathBuf> {
|
|
let dir = dirs::config_dir().context("No config directory")?.join("frislit");
|
|
fs::create_dir_all(&dir)?;
|
|
Ok(dir.join("config.json"))
|
|
}
|
|
|
|
pub fn load() -> Result<Self> {
|
|
let path = Self::path()?;
|
|
if !path.exists() { return Ok(Self::default()); }
|
|
serde_json::from_str(&fs::read_to_string(&path)?).context("Failed to parse config")
|
|
}
|
|
|
|
pub fn save(&self) -> Result<()> {
|
|
Ok(fs::write(Self::path()?, serde_json::to_string_pretty(self)?)?)
|
|
}
|
|
|
|
pub fn server_url(&self) -> Option<String> {
|
|
std::env::var("FRISLIT_SERVER").ok().filter(|v| !v.trim().is_empty())
|
|
.or_else(|| self.server_url.clone())
|
|
.map(|v| normalize_url(&v))
|
|
}
|
|
|
|
pub fn token(&self) -> Option<String> {
|
|
std::env::var("FRISLIT_TOKEN").ok().filter(|v| !v.trim().is_empty()).or_else(|| self.device_token.clone())
|
|
}
|
|
|
|
pub fn require_server(&self) -> Result<String> {
|
|
self.server_url().ok_or_else(|| anyhow::anyhow!("No server configured. Run `frislit login` or pass --server / FRISLIT_SERVER"))
|
|
}
|
|
|
|
pub fn require_auth(&self) -> Result<(String, String)> {
|
|
let url = self.require_server()?;
|
|
let token = self.token().ok_or_else(|| anyhow::anyhow!("This device is not linked. Run `frislit login` first"))?;
|
|
Ok((url, token))
|
|
}
|
|
}
|
|
|
|
pub fn normalize_url(url: &str) -> String {
|
|
let trimmed = url.trim().trim_end_matches('/');
|
|
if trimmed.starts_with("http://") || trimmed.starts_with("https://") { trimmed.to_string() } else { format!("https://{trimmed}") }
|
|
}
|
|
|
|
pub fn set(key: &str, value: &str) -> Result<()> {
|
|
let mut cfg = Config::load()?;
|
|
match key {
|
|
"server-url" => cfg.server_url = Some(normalize_url(value)),
|
|
"device-token" => cfg.device_token = Some(value.to_string()),
|
|
"accept-invalid-certs" => cfg.accept_invalid_certs = value.parse().context("Must be 'true' or 'false'")?,
|
|
_ => bail!("Unknown key: {key}. Valid: server-url, device-token, accept-invalid-certs"),
|
|
}
|
|
cfg.save()?;
|
|
println!("Set {key} = {}", if key == "device-token" { "(set)" } else { value });
|
|
Ok(())
|
|
}
|
|
|
|
pub fn get(key: &str) -> Result<()> {
|
|
let cfg = Config::load()?;
|
|
println!("{}", match key {
|
|
"server-url" => cfg.server_url.unwrap_or_default(),
|
|
"accept-invalid-certs" => cfg.accept_invalid_certs.to_string(),
|
|
"device-token" => cfg.device_token.map(|_| "(set)".into()).unwrap_or_default(),
|
|
_ => bail!("Unknown key: {key}"),
|
|
});
|
|
Ok(())
|
|
}
|
|
|
|
pub fn show() -> Result<()> {
|
|
let cfg = Config::load()?;
|
|
println!("server-url: {}", cfg.server_url.as_deref().unwrap_or("(not set)"));
|
|
println!("accept-invalid-certs: {}", cfg.accept_invalid_certs);
|
|
println!("device-token: {}", if cfg.device_token.is_some() { "(set)" } else { "(not set)" });
|
|
Ok(())
|
|
}
|