From 9f1cabf49a05d1a2f673bf81696e2fbd6b88e538 Mon Sep 17 00:00:00 2001 From: Louis Chmn Date: Mon, 7 Sep 2026 17:56:25 +0200 Subject: [PATCH] feat(pin): Support pining stoped instances Signed-off-by: Louis Chmn --- src/clean.rs | 2 +- src/cloud.rs | 36 +++++++++++++++++------------------- src/main.rs | 10 +++------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/clean.rs b/src/clean.rs index f5b5dcd..81cbc7b 100644 --- a/src/clean.rs +++ b/src/clean.rs @@ -11,7 +11,7 @@ pub async fn clean(docker: &Docker, config: &HazeConfig) -> Result<()> { let mut retain = Vec::new(); let mut remove = Vec::new(); for cloud in list { - if cloud.is_pinned(&docker).await.unwrap_or(false) { + if cloud.is_pinned() { retain.push(cloud); } else { remove.push(cloud); diff --git a/src/cloud.rs b/src/cloud.rs index 515537f..d69e89d 100644 --- a/src/cloud.rs +++ b/src/cloud.rs @@ -53,7 +53,7 @@ fn get_max_php_version(base_path: &Utf8Path) -> Option { PhpVersion::from_number(major, minor) } -const PINNED_FILE: &str = "/var/www/html/data/.haze-pinned"; +const PINNED_FILE: &str = ".haze-pinned"; fn enabled_apps(config: &HazeConfig, options: &CloudOptions) -> Vec { let mut apps: Vec = config.auto_setup.enable_apps.clone(); @@ -949,28 +949,26 @@ impl Cloud { Ok(()) } - pub async fn is_pinned(&self, docker: &Docker) -> Result { - let mut content = Vec::new(); - self.exec_with_output( - docker, - vec!["cat", PINNED_FILE], - Some(&mut content), - Vec::::new(), - ) - .await? - .to_result()?; + pub fn is_pinned(&self) -> bool { + let path = self.workdir.join("data").join(PINNED_FILE); + match read_to_string(&path) { + Ok(content) => content.trim() == "1", + Err(_) => false, + } + } - String::from_utf8(content) + pub fn pin(&self) -> Result<()> { + let path = self.workdir.join("data").join(PINNED_FILE); + write(path.clone(), "1") .into_diagnostic() - .map(|content| content.trim() == "1") + .wrap_err_with(|| format!("Failed to write {path}")) } - pub async fn pin(&self, docker: &Docker) -> Result<()> { - self.write_file(docker, PINNED_FILE, "1").await - } - - pub async fn unpin(&self, docker: &Docker) -> Result<()> { - self.write_file(docker, PINNED_FILE, "0").await + pub fn unpin(&self) -> Result<()> { + let path = self.workdir.join("data").join(PINNED_FILE); + write(path.clone(), "0") + .into_diagnostic() + .wrap_err_with(|| format!("Failed to write {path}")) } pub fn services(&self) -> impl Iterator { diff --git a/src/main.rs b/src/main.rs index 08ceaba..a182f0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,11 +87,7 @@ async fn main() -> Result { .collect(); services.push(cloud.db().name()); let services = services.join(", "); - let pin = if cloud.is_pinned(&docker).await.unwrap_or(false) { - "*" - } else { - "" - }; + let pin = if cloud.is_pinned() { "*" } else { "" }; let version = match cloud.options.version.as_ref() { Some(version) => format!(", v{version}"), None => String::new(), @@ -368,11 +364,11 @@ async fn main() -> Result { } HazeArgs::Pin { filter } => { let cloud = Cloud::get_by_filter(&docker, filter, &config).await?; - cloud.pin(&docker).await?; + cloud.pin()?; } HazeArgs::Unpin { filter } => { let cloud = Cloud::get_by_filter(&docker, filter, &config).await?; - cloud.unpin(&docker).await?; + cloud.unpin()?; } HazeArgs::Proxy => { proxy(docker, config).await?;