blob: cc9f05ff3257bc51213482ae8e72e96a1fde37f3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// Local config file for the TUI's account state - a saved personal-access
// token, not a browser cookie, since a terminal app has no cookie jar.
// Lives at the OS config dir so it survives between runs the same way any
// other CLI tool's saved credentials would.
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AuthConfig {
pub token: Option<String>,
pub username: Option<String>,
}
fn config_path() -> Option<PathBuf> {
let mut dir = dirs::config_dir()?;
dir.push("typerpunk");
Some(dir.join("cli_auth.json"))
}
pub fn load() -> AuthConfig {
let Some(path) = config_path() else { return AuthConfig::default() };
let Ok(raw) = std::fs::read_to_string(&path) else { return AuthConfig::default() };
serde_json::from_str(&raw).unwrap_or_default()
}
pub fn save(config: &AuthConfig) {
let Some(path) = config_path() else { return };
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
if let Ok(json) = serde_json::to_string_pretty(config) {
let _ = std::fs::write(&path, json);
}
}
pub fn server_url() -> String {
std::env::var("TYPERPUNK_SERVER_URL").unwrap_or_else(|_| "http://localhost:8787".to_string())
}
/// Same host as server_url(), just ws:// (or wss:// if the HTTP base was
/// already secured) instead of http(s):// - for the multiplayer WebSocket.
pub fn server_ws_url() -> String {
let http = server_url();
if let Some(rest) = http.strip_prefix("https://") {
format!("wss://{rest}")
} else if let Some(rest) = http.strip_prefix("http://") {
format!("ws://{rest}")
} else {
format!("ws://{http}")
}
}
|