Skip to content

Commit

Permalink
Merge pull request #1267 from k3yss/work/keys/version-fix
Browse files Browse the repository at this point in the history
fix: execution of devenv version in read only directory
  • Loading branch information
domenkozar authored Jun 12, 2024
2 parents 5c5b90f + bfc18ce commit 0376cde
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions devenv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -268,6 +268,7 @@ struct App {
has_processes: Option<bool>,
container_name: Option<String>,
assembled: bool,
dirs_created: bool,
// all kinds of paths
devenv_root: PathBuf,
devenv_dotfile: PathBuf,
Expand All @@ -289,15 +290,10 @@ fn main() -> Result<()> {
};

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()));
Expand All @@ -322,6 +318,7 @@ fn main() -> Result<()> {
has_processes: None,
logger,
container_name: None,
dirs_created: false,
devenv_root,
devenv_dotfile,
devenv_dot_gc,
Expand All @@ -332,6 +329,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 {
Expand Down Expand Up @@ -1079,6 +1080,26 @@ impl App {
Ok(())
}

fn create_directories(&mut self) -> Result<()> {
if !self.dirs_created {
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"))
.into_diagnostic()
.wrap_err("Failed to create DEVENV_HOME directory")?;
std::fs::create_dir_all(&self.devenv_home_gc)
.into_diagnostic()
.wrap_err("Failed to create DEVENV_HOME_GC directory")?;
std::fs::create_dir_all(&self.devenv_dot_gc)
.into_diagnostic()
.wrap_err("Failed to create .devenv/gc directory")?;
self.dirs_created = true;
}
Ok(())
}

fn assemble(&mut self, is_testing: bool) -> Result<()> {
if !self.assembled {
if !PathBuf::from("devenv.nix").exists() {
Expand Down

0 comments on commit 0376cde

Please sign in to comment.