From 9f7e82b4d34347b7b33fe8f3e743eae49178c8e6 Mon Sep 17 00:00:00 2001 From: Tip ten Brink <75669206+tiptenbrink@users.noreply.github.com> Date: Sat, 13 Jan 2024 12:24:21 +0100 Subject: [PATCH] move to cache dir --- Cargo.lock | 86 +++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 25 +++++++------- src/archives.rs | 6 ++-- src/commands.rs | 44 ++++++++++++------------ src/filesystem.rs | 20 ++++++++++- 5 files changed, 141 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b360b93..6444484 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,6 +62,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + [[package]] name = "byteorder" version = "1.5.0" @@ -146,12 +152,44 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +[[package]] +name = "directories" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "getrandom" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hashbrown" version = "0.14.3" @@ -205,13 +243,24 @@ version = "0.2.152" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" +[[package]] +name = "libredox" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8" +dependencies = [ + "bitflags 2.4.1", + "libc", + "redox_syscall", +] + [[package]] name = "linux-keyutils" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f27bb67f6dd1d0bb5ab582868e4f65052e58da6401188a08f0da09cf512b84b" dependencies = [ - "bitflags", + "bitflags 1.3.2", "libc", ] @@ -243,6 +292,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "overload" version = "0.1.1" @@ -279,6 +334,26 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "redox_users" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "relative-path" version = "1.9.2" @@ -318,7 +393,7 @@ version = "2.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" dependencies = [ - "bitflags", + "bitflags 1.3.2", "core-foundation", "core-foundation-sys", "libc", @@ -454,6 +529,7 @@ version = "0.13.0" dependencies = [ "base64", "clap", + "directories", "keyring", "relative-path", "rpassword", @@ -575,6 +651,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index b72dd30..e00c0da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,15 +9,16 @@ repository="https://github.com/tiptenbrink/tidploy" readme="README.md" [dependencies] -base64 = "0.21.5" -clap = { version = "4.4.10", features = ["derive"] } -keyring = { version = "2.3.1", default-features = false, features=[ "linux-no-secret-service", "platform-windows", "platform-macos" ] } -relative-path = "1.9.0" -rpassword = "7.3.1" -serde = { version = "1.0.193", features = ["derive"] } -serde_json = "1.0.108" -spinoff = { version = "0.8.0", features = ["line"] } -thiserror = "1.0.50" -toml = "0.8.8" -tracing = "0.1" -tracing-subscriber = "0.3" \ No newline at end of file +base64 = "=0.21.7" +clap = { version = "=4.4.16", features = ["derive"] } +keyring = { version = "=2.3.1", default-features = false, features=[ "linux-no-secret-service", "platform-windows", "platform-macos" ] } +relative-path = "=1.9.2" +rpassword = "=7.3.1" +serde = { version = "=1.0.195", features = ["derive"] } +serde_json = "=1.0.111" +spinoff = { version = "=0.8.0", features = ["line"] } +thiserror = "=1.0.56" +toml = "=0.8.8" +tracing = "=0.1.40" +tracing-subscriber = "=0.3.18" +directories = "=5.0.1" \ No newline at end of file diff --git a/src/archives.rs b/src/archives.rs index ebff24b..be515b3 100644 --- a/src/archives.rs +++ b/src/archives.rs @@ -57,9 +57,9 @@ pub(crate) fn make_archive( pub(crate) fn extract_archive( archive_path: &Path, - current_dir: &Path, // TMP DIR + current_dir: &Path, target_name: &str, -) -> Result<(), RepoError> { +) -> Result { let archive_path_name = archive_path.to_str().ok_or(FileError { msg: format!("Cannot represent path {:?} as string!", archive_path), source: FileErrorKind::InvalidPath, @@ -110,5 +110,5 @@ pub(crate) fn extract_archive( println!("Extracted archive."); - Ok(()) + Ok(target_path) } diff --git a/src/commands.rs b/src/commands.rs index 49d3e7a..89fb5a8 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -2,6 +2,7 @@ use std::path::{Path, PathBuf}; use crate::archives::{extract_archive, make_archive}; use crate::errors::{ProcessError, RepoError}; +use crate::filesystem::get_dirs; use crate::git::{checkout, checkout_path, repo_clone, Repo}; use crate::secret::{secret_command, AuthError}; @@ -15,14 +16,14 @@ use clap::{Parser, Subcommand}; use std::fmt::Debug; use std::time::Instant; + use thiserror::Error as ThisError; use tracing::span; use tracing::{debug, Level}; pub(crate) const DEFAULT_INFER: &str = "default_infer"; -pub(crate) const TIDPLOY_DEFAULT: &str = "_tidploy_default"; +pub(crate) const TIDPLOY_DEFAULT: &str = "tidploy_default"; pub(crate) const DEFAULT: &str = "default"; -pub(crate) const TMP_DIR: &str = "/tmp/tidploy"; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -34,7 +35,7 @@ struct Cli { #[arg(long, value_enum, global = true)] context: Option, - /// Set the repository URL, defaults to 'default_infer', in which case it is inferred from the current repository. + /// Set the repository URL, defaults to 'default_infer', in which case it is inferred from the current repository. /// Set to 'default' to not set it. /// Falls back to environment variable using TIDPLOY_REPO and then to config with key 'repo_url' /// For infering, it looks at the URL set to the 'origin' remote. @@ -106,11 +107,11 @@ enum ErrorRepr { } fn create_repo(repo: Repo) -> Result { - let tmp_dir = Path::new(TMP_DIR); + let cache_dir = get_dirs().cache.as_path(); let repo_name = repo.dir_name(); - let repo_path = tmp_dir.join(&repo_name); + let repo_path = cache_dir.join(&repo_name); - repo_clone(tmp_dir, &repo_name, &repo.url)?; + repo_clone(cache_dir, &repo_name, &repo.url)?; Ok(repo_path) } @@ -144,8 +145,8 @@ fn switch_to_revision( } fn prepare_from_state(state: &State, repo_path: &Path) -> Result<(), ErrorRepr> { - let tmp_dir = Path::new(TMP_DIR); - let archives = tmp_dir.join("archives"); + let cache_dir = get_dirs().cache.as_path(); + let archives = cache_dir.join("archives"); let deploy_encoded = B64USNP.encode(state.deploy_path.as_str()); let archive_name = format!( "{}_{}_{}", @@ -154,7 +155,7 @@ fn prepare_from_state(state: &State, repo_path: &Path) -> Result<(), ErrorRepr> make_archive( &archives, - tmp_dir, + cache_dir, repo_path.file_name().unwrap().to_string_lossy().as_ref(), &archive_name, ) @@ -202,7 +203,7 @@ fn prepare_command( let _prep_enter = prepare_san.enter(); let repo_path = if no_create { - let tmp_dir = Path::new(TMP_DIR); + let tmp_dir = get_dirs().cache.as_path(); let repo_path = tmp_dir.join(repo.dir_name()); if !repo_path.exists() { @@ -296,14 +297,14 @@ pub(crate) fn run_cli() -> Result<(), Error> { enter_dl.exit(); let state = prepare_command(cli_state.clone(), no_create, state.repo)?.unwrap(); - - let tmp_dir = Path::new(TMP_DIR); + let cache_dir = get_dirs().cache.as_path(); + let tmp_dir = get_dirs().tmp.as_path(); let deploy_encoded = B64USNP.encode(state.deploy_path.as_str()); let archive_name = format!( "{}_{}_{}", state.repo.name, state.commit_sha, deploy_encoded ); - let archive_path = tmp_dir + let archive_path = cache_dir .join("archives") .join(&archive_name) .with_extension("tar.gz"); @@ -331,21 +332,20 @@ pub(crate) fn run_cli() -> Result<(), Error> { // Only loads archive if it is given, otherwise path is None let path = if let Some(archive) = archive { - let tmp_dir = Path::new(TMP_DIR); - let archive_path = tmp_dir + let cache_dir = get_dirs().cache.as_path(); + let archive_path = cache_dir .join("archives") .join(&archive) .with_extension("tar.gz"); - // Running archives doesn't fully work as it doesn't yet know how to retrieve the deploy path - // Splitting by '_' doesn't work easily as base64url includes '_' chars - extract_archive(&archive_path, tmp_dir, &archive).map_err(ErrorRepr::Repo)?; - let archive_path = tmp_dir.join(&archive); - debug!("Extracted and loaded archive at {:?}", &archive_path); + let tmp_dir = get_dirs().tmp.as_path(); + let extracted_path = + extract_archive(&archive_path, tmp_dir, &archive).map_err(ErrorRepr::Repo)?; + debug!("Extracted and loaded archive at {:?}", &extracted_path); - let state = create_state_create(cli_state.clone(), Some(&archive_path), true) + let state = create_state_create(cli_state.clone(), Some(&extracted_path), true) .map_err(ErrorRepr::Load)?; - let target_path = state.deploy_path.to_path(&archive_path); + let target_path = state.deploy_path.to_path(&extracted_path); Some(target_path) } else { diff --git a/src/filesystem.rs b/src/filesystem.rs index cc9c170..3589ddc 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -1,4 +1,5 @@ -use std::{env, io::Error as StdIOError, path::PathBuf}; +use directories::ProjectDirs; +use std::{env, io::Error as StdIOError, path::PathBuf, sync::OnceLock}; use thiserror::Error as ThisError; #[derive(ThisError, Debug)] @@ -19,3 +20,20 @@ pub(crate) enum FileErrorKind { pub(crate) fn get_current_dir() -> Result { env::current_dir().map_err(FileErrorKind::IO) } + +pub(crate) struct Dirs { + pub(crate) cache: PathBuf, + pub(crate) tmp: PathBuf, +} + +pub(crate) fn get_dirs() -> &'static Dirs { + static DIRS: OnceLock = OnceLock::new(); + DIRS.get_or_init(|| { + let project_dirs = ProjectDirs::from("", "", "tidploy").unwrap(); + + let cache = project_dirs.cache_dir().to_owned(); + let tmp = env::temp_dir(); + + Dirs { cache, tmp } + }) +}