From 0b363bfd0ee8dcc3f6440dfd9d8ef04b24d5560f Mon Sep 17 00:00:00 2001 From: Rishi Kumar Date: Wed, 12 Jun 2024 01:01:22 +0530 Subject: [PATCH 1/3] fix: execute devenv version in read only directory --- devenv/src/main.rs | 80 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/devenv/src/main.rs b/devenv/src/main.rs index 2c03abd94..fe8a656e2 100644 --- a/devenv/src/main.rs +++ b/devenv/src/main.rs @@ -201,6 +201,9 @@ enum Commands { #[clap(hide = true)] Assemble, + #[clap(hide = true)] + CreateDirectories, + #[clap(hide = true)] PrintDevEnv { #[arg(long)] @@ -268,6 +271,7 @@ struct App { has_processes: Option, container_name: Option, assembled: bool, + dirs_created: Option, // all kinds of paths devenv_root: PathBuf, devenv_dotfile: PathBuf, @@ -288,33 +292,26 @@ fn main() -> Result<()> { log::Level::Info }; + let logger = log::Logger::new(level); + + let mut config = config::Config::load()?; + + for input in cli.override_input.chunks_exact(2) { + config.add_input(&input[0].clone(), &input[1].clone(), &[]); + } + let xdg_dirs = xdg::BaseDirectories::with_prefix("devenv").unwrap(); - xdg_dirs - .create_data_directory(Path::new("devenv")) - .expect("Failed to create DEVENV_HOME directory"); let devenv_home = xdg_dirs.get_data_home(); let devenv_home_gc = devenv_home.join("gc"); - std::fs::create_dir_all(&devenv_home_gc).expect("Failed to create DEVENV_HOME_GC directory"); - let devenv_root = std::env::current_dir().expect("Failed to get current directory"); - let devenv_dot_gc = devenv_root.join(".devenv").join("gc"); - std::fs::create_dir_all(&devenv_dot_gc).expect("Failed to create .devenv/gc directory"); - let devenv_dotfile = devenv_root.join(".devenv"); + let devenv_tmp = std::env::var("XDG_RUNTIME_DIR") .unwrap_or_else(|_| std::env::var("TMPDIR").unwrap_or_else(|_| "/tmp".to_string())); - // first 7 chars of sha256 hash of devenv_state - let devenv_state_hash = { - let mut hasher = sha2::Sha256::new(); - hasher.update(devenv_dotfile.to_string_lossy().as_bytes()); - let result = hasher.finalize(); - hex::encode(result) - }; - let devenv_runtime = Path::new(&devenv_tmp).join(format!("devenv-{}", &devenv_state_hash[..7])); + + // using dummy values for initialization + let devenv_root: PathBuf = "./".into(); + let devenv_runtime = Path::new(&devenv_tmp).join(format!("devenv-{}", 1234.to_string())); let cachix_trusted_keys = devenv_home.join("cachix_trusted_keys.json"); - let logger = log::Logger::new(level); - let mut config = config::Config::load()?; - for input in cli.override_input.chunks_exact(2) { - config.add_input(&input[0].clone(), &input[1].clone(), &[]); - } + let mut app = App { cli, config, @@ -322,9 +319,10 @@ fn main() -> Result<()> { has_processes: None, logger, container_name: None, - devenv_root, - devenv_dotfile, - devenv_dot_gc, + dirs_created: None, + devenv_root: devenv_root.clone(), + devenv_dotfile: devenv_root.join(".devenv"), + devenv_dot_gc: devenv_root.join(".devenv").join("gc"), devenv_home_gc, devenv_tmp, devenv_runtime, @@ -332,6 +330,10 @@ fn main() -> Result<()> { cachix_caches: None, }; + if !matches!(app.cli.command, Commands::Version {} | Commands::Gc { .. }) { + app.create_directories()?; + } + match app.cli.command.clone() { Commands::Shell { cmd, args } => app.shell(&cmd, &args, true), Commands::Test { @@ -407,6 +409,7 @@ fn main() -> Result<()> { InputsCommand::Add { name, url, follows } => app.inputs_add(&name, &url, &follows), }, // hidden + Commands::CreateDirectories => app.create_directories(), Commands::Assemble => app.assemble(false), Commands::PrintDevEnv { json } => app.print_dev_env(json), Commands::GenerateJSONSchema => { @@ -1079,6 +1082,35 @@ impl App { Ok(()) } + fn create_directories(&mut self) -> Result<()> { + if self.dirs_created != Some(true) { + let xdg_dirs = xdg::BaseDirectories::with_prefix("devenv").unwrap(); + xdg_dirs + .create_data_directory(Path::new("devenv")) + .expect("Failed to create DEVENV_HOME directory"); + std::fs::create_dir_all(&self.devenv_home_gc) + .expect("Failed to create DEVENV_HOME_GC directory"); + self.devenv_root = std::env::current_dir().expect("Failed to get current directory"); + self.devenv_dot_gc = self.devenv_root.join(".devenv").join("gc"); + std::fs::create_dir_all(&self.devenv_dot_gc) + .expect("Failed to create .devenv/gc directory"); + self.devenv_dotfile = self.devenv_root.join(".devenv"); + + // first 7 chars of sha256 hash of devenv_state + let devenv_state_hash = { + let mut hasher = sha2::Sha256::new(); + hasher.update(self.devenv_dotfile.to_string_lossy().as_bytes()); + let result = hasher.finalize(); + hex::encode(result) + }; + self.devenv_runtime = + Path::new(&self.devenv_tmp).join(format!("devenv-{}", &devenv_state_hash[..7])); + + self.dirs_created = Some(true); + } + Ok(()) + } + fn assemble(&mut self, is_testing: bool) -> Result<()> { if !self.assembled { if !PathBuf::from("devenv.nix").exists() { From e1806fd73d16dab802d60096e96f4c6e27301a42 Mon Sep 17 00:00:00 2001 From: Rishi Kumar Date: Wed, 12 Jun 2024 11:21:32 +0530 Subject: [PATCH 2/3] chore: remove unneccesary complexity --- devenv/src/main.rs | 62 +++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/devenv/src/main.rs b/devenv/src/main.rs index fe8a656e2..a0209e48a 100644 --- a/devenv/src/main.rs +++ b/devenv/src/main.rs @@ -201,9 +201,6 @@ enum Commands { #[clap(hide = true)] Assemble, - #[clap(hide = true)] - CreateDirectories, - #[clap(hide = true)] PrintDevEnv { #[arg(long)] @@ -271,7 +268,7 @@ struct App { has_processes: Option, container_name: Option, assembled: bool, - dirs_created: Option, + dirs_created: bool, // all kinds of paths devenv_root: PathBuf, devenv_dotfile: PathBuf, @@ -292,26 +289,28 @@ fn main() -> Result<()> { log::Level::Info }; - let logger = log::Logger::new(level); - - let mut config = config::Config::load()?; - - for input in cli.override_input.chunks_exact(2) { - config.add_input(&input[0].clone(), &input[1].clone(), &[]); - } - let xdg_dirs = xdg::BaseDirectories::with_prefix("devenv").unwrap(); let devenv_home = xdg_dirs.get_data_home(); let devenv_home_gc = devenv_home.join("gc"); - + let devenv_root = std::env::current_dir().expect("Failed to get current directory"); + let devenv_dot_gc = devenv_root.join(".devenv").join("gc"); + let devenv_dotfile = devenv_root.join(".devenv"); let devenv_tmp = std::env::var("XDG_RUNTIME_DIR") .unwrap_or_else(|_| std::env::var("TMPDIR").unwrap_or_else(|_| "/tmp".to_string())); - - // using dummy values for initialization - let devenv_root: PathBuf = "./".into(); - let devenv_runtime = Path::new(&devenv_tmp).join(format!("devenv-{}", 1234.to_string())); + // first 7 chars of sha256 hash of devenv_state + let devenv_state_hash = { + let mut hasher = sha2::Sha256::new(); + hasher.update(devenv_dotfile.to_string_lossy().as_bytes()); + let result = hasher.finalize(); + hex::encode(result) + }; + let devenv_runtime = Path::new(&devenv_tmp).join(format!("devenv-{}", &devenv_state_hash[..7])); let cachix_trusted_keys = devenv_home.join("cachix_trusted_keys.json"); - + let logger = log::Logger::new(level); + let mut config = config::Config::load()?; + for input in cli.override_input.chunks_exact(2) { + config.add_input(&input[0].clone(), &input[1].clone(), &[]); + } let mut app = App { cli, config, @@ -319,10 +318,10 @@ fn main() -> Result<()> { has_processes: None, logger, container_name: None, - dirs_created: None, - devenv_root: devenv_root.clone(), - devenv_dotfile: devenv_root.join(".devenv"), - devenv_dot_gc: devenv_root.join(".devenv").join("gc"), + dirs_created: false, + devenv_root, + devenv_dotfile, + devenv_dot_gc, devenv_home_gc, devenv_tmp, devenv_runtime, @@ -409,7 +408,6 @@ fn main() -> Result<()> { InputsCommand::Add { name, url, follows } => app.inputs_add(&name, &url, &follows), }, // hidden - Commands::CreateDirectories => app.create_directories(), Commands::Assemble => app.assemble(false), Commands::PrintDevEnv { json } => app.print_dev_env(json), Commands::GenerateJSONSchema => { @@ -1083,30 +1081,16 @@ impl App { } fn create_directories(&mut self) -> Result<()> { - if self.dirs_created != Some(true) { + if !self.dirs_created { let xdg_dirs = xdg::BaseDirectories::with_prefix("devenv").unwrap(); xdg_dirs .create_data_directory(Path::new("devenv")) .expect("Failed to create DEVENV_HOME directory"); std::fs::create_dir_all(&self.devenv_home_gc) .expect("Failed to create DEVENV_HOME_GC directory"); - self.devenv_root = std::env::current_dir().expect("Failed to get current directory"); - self.devenv_dot_gc = self.devenv_root.join(".devenv").join("gc"); std::fs::create_dir_all(&self.devenv_dot_gc) .expect("Failed to create .devenv/gc directory"); - self.devenv_dotfile = self.devenv_root.join(".devenv"); - - // first 7 chars of sha256 hash of devenv_state - let devenv_state_hash = { - let mut hasher = sha2::Sha256::new(); - hasher.update(self.devenv_dotfile.to_string_lossy().as_bytes()); - let result = hasher.finalize(); - hex::encode(result) - }; - self.devenv_runtime = - Path::new(&self.devenv_tmp).join(format!("devenv-{}", &devenv_state_hash[..7])); - - self.dirs_created = Some(true); + self.dirs_created = true; } Ok(()) } From bfc18cec8c340cd88160f9a7aa4b3959cb63654f Mon Sep 17 00:00:00 2001 From: Sander Date: Wed, 12 Jun 2024 12:29:54 +0000 Subject: [PATCH 3/3] devenv: improve error messages when creating dirs --- devenv/src/main.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/devenv/src/main.rs b/devenv/src/main.rs index a0209e48a..04d64072a 100644 --- a/devenv/src/main.rs +++ b/devenv/src/main.rs @@ -5,7 +5,7 @@ mod log; use clap::{crate_version, Parser, Subcommand}; use cli_table::{print_stderr, Table, WithTitle}; use include_dir::{include_dir, Dir}; -use miette::{bail, Result}; +use miette::{bail, IntoDiagnostic, Result, WrapErr}; use serde::Deserialize; use sha2::Digest; use std::collections::HashMap; @@ -1082,14 +1082,19 @@ impl App { fn create_directories(&mut self) -> Result<()> { if !self.dirs_created { - let xdg_dirs = xdg::BaseDirectories::with_prefix("devenv").unwrap(); + let xdg_dirs = xdg::BaseDirectories::with_prefix("devenv") + .into_diagnostic() + .wrap_err("Failed to get XDG directories")?; xdg_dirs .create_data_directory(Path::new("devenv")) - .expect("Failed to create DEVENV_HOME directory"); + .into_diagnostic() + .wrap_err("Failed to create DEVENV_HOME directory")?; std::fs::create_dir_all(&self.devenv_home_gc) - .expect("Failed to create DEVENV_HOME_GC directory"); + .into_diagnostic() + .wrap_err("Failed to create DEVENV_HOME_GC directory")?; std::fs::create_dir_all(&self.devenv_dot_gc) - .expect("Failed to create .devenv/gc directory"); + .into_diagnostic() + .wrap_err("Failed to create .devenv/gc directory")?; self.dirs_created = true; } Ok(())