use std::collections::BTreeMap;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Instance {
pub sandbox: String,
#[serde(default)]
pub project: String,
pub container: String,
pub folder: PathBuf,
#[serde(default)]
pub base_folder: PathBuf,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub worktree: Option<PathBuf>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub branch: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub shell_history: Option<PathBuf>,
pub workspace: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub workspace_file: Option<String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub remote_env: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub remote_user: Option<String>,
pub created_unix: u64,
}
impl Instance {
pub fn now() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct State {
#[serde(default, rename = "instance")]
pub instances: BTreeMap<String, Instance>,
}
impl State {
pub fn path() -> Result<PathBuf> {
let base = std::env::var_os("XDG_DATA_HOME")
.map(PathBuf::from)
.or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".local/share")))
.context("cannot determine user data dir ($XDG_DATA_HOME or $HOME)")?;
Ok(base.join("devsandbox/state.toml"))
}
pub fn load() -> Result<Self> {
let path = Self::path()?;
match std::fs::read_to_string(&path) {
Ok(contents) => toml::from_str(&contents)
.with_context(|| format!("invalid state file {}", path.display())),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Self::default()),
Err(e) => Err(e).with_context(|| format!("cannot read {}", path.display())),
}
}
pub fn save(&self) -> Result<()> {
let path = Self::path()?;
let dir = path.parent().unwrap();
std::fs::create_dir_all(dir)
.with_context(|| format!("cannot create {}", dir.display()))?;
let contents = toml::to_string_pretty(self).context("cannot serialize state")?;
std::fs::write(&path, contents)
.with_context(|| format!("cannot write {}", path.display()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roundtrips() {
let mut state = State::default();
state.instances.insert(
"repo-abc1".into(),
Instance {
sandbox: "repository-1".into(),
project: "abc12345".into(),
container: "devsandbox-repo-abc1".into(),
folder: "/home/u/repository-1".into(),
base_folder: "/home/u/repository-1".into(),
worktree: None,
branch: None,
shell_history: None,
workspace: "/workspaces/repository-1".into(),
workspace_file: None,
remote_env: BTreeMap::new(),
remote_user: None,
created_unix: Instance::now(),
},
);
let text = toml::to_string_pretty(&state).unwrap();
let back: State = toml::from_str(&text).unwrap();
assert_eq!(back.instances["repo-abc1"].sandbox, "repository-1");
}
}