From 5b7521ad083f3dfc307d25d8aa00d8bcd810659b Mon Sep 17 00:00:00 2001 From: Hugo Dutka Date: Wed, 28 Oct 2020 13:03:31 +0100 Subject: [PATCH] put generated files in .generated --- src/action/close.rs | 5 ++--- src/action/get.rs | 14 ++++++++++---- src/action/open.rs | 12 ++++++------ src/core/dir.rs | 12 +++++++++++- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/action/close.rs b/src/action/close.rs index c9bbcf1..d2ee945 100644 --- a/src/action/close.rs +++ b/src/action/close.rs @@ -1,14 +1,13 @@ use super::Action; use crate::cmd::close::CloseCmd; -use crate::core::dir::get_app_dir; +use crate::core::dir::get_project_dir; use crate::core::shell::run_command; use anyhow::Result; use std::process::Command; impl Action for CloseCmd { fn run(&self) -> Result<()> { - let hocus_dir = get_app_dir()?; - let project_dir = hocus_dir.join(&self.name); + let project_dir = get_project_dir(&self.name)?; println!("Shutting down the Docker environment..."); run_command( diff --git a/src/action/get.rs b/src/action/get.rs index ba8b352..7b303f0 100644 --- a/src/action/get.rs +++ b/src/action/get.rs @@ -1,8 +1,9 @@ use super::Action; use crate::cmd::get::GetCmd; -use crate::core::dir::get_app_dir; +use crate::core::dir::{get_app_dir, get_generated_dir, get_project_dir}; use crate::core::shell::run_command; -use anyhow::Result; +use anyhow::{Context, Result}; +use std::fs; use std::process::Command; impl Action for GetCmd { @@ -17,9 +18,14 @@ impl Action for GetCmd { .arg(&self.name), )?; + let project_dir = get_project_dir(&self.name)?; + + fs::create_dir_all(get_generated_dir(&project_dir)) + .context("failed to create the generated directory")?; + println!( - "The environment is now available in {}", - app_dir.join(&self.name).display() + "\nYou can now open the environment with:\n$ hocus open {}\n", + &self.name ); Ok(()) diff --git a/src/action/open.rs b/src/action/open.rs index 02f7fda..c6db018 100644 --- a/src/action/open.rs +++ b/src/action/open.rs @@ -1,7 +1,7 @@ use super::Action; use crate::cmd::open::OpenCmd; use crate::core::config::ProjectConfig; -use crate::core::dir::get_app_dir; +use crate::core::dir::{get_generated_dir, get_project_dir}; use crate::core::shell::{code_command, run_command}; use crate::core::state::ProjectState; use anyhow::Result; @@ -10,8 +10,8 @@ use std::process::Command; impl Action for OpenCmd { fn run(&self) -> Result<()> { - let hocus_dir = get_app_dir()?; - let project_dir = hocus_dir.join(&self.name); + let project_dir = get_project_dir(&self.name)?; + let generated_dir = get_generated_dir(&project_dir); let project_config = ProjectConfig::open(&project_dir)?; @@ -32,10 +32,10 @@ impl Action for OpenCmd { service = project_config.mount_service ); - let mut project_state = ProjectState::open(&project_dir)?; + let mut project_state = ProjectState::open(&generated_dir)?; if !project_state.is_init { println!("Creating the .env file using template.env..."); - fs::copy(project_dir.join("template.env"), project_dir.join(".env"))?; + fs::copy(project_dir.join("template.env"), generated_dir.join(".env"))?; println!("Running the init.sh script..."); run_command( @@ -53,7 +53,7 @@ impl Action for OpenCmd { )), )?; project_state.is_init = true; - project_state.save(&project_dir)?; + project_state.save(&generated_dir)?; } println!("Opening the project in VSCode..."); diff --git a/src/core/dir.rs b/src/core/dir.rs index 60aba89..7700bcc 100644 --- a/src/core/dir.rs +++ b/src/core/dir.rs @@ -1,9 +1,19 @@ use anyhow::{anyhow, Result}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; const HOCUS_DIR_NAME: &str = ".hocus"; +const GENERATED_DIR_NAME: &str = ".generated"; pub fn get_app_dir() -> Result { let dir = dirs::home_dir().ok_or(anyhow!("failed to get the path to the home directory"))?; Ok(dir.join(HOCUS_DIR_NAME)) } + +pub fn get_project_dir(project_name: &str) -> Result { + let hocus_dir = get_app_dir()?; + Ok(hocus_dir.join(project_name)) +} + +pub fn get_generated_dir(project_dir: &Path) -> PathBuf { + project_dir.join(GENERATED_DIR_NAME) +}