Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: execution of devenv version in read only directory #1267

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading