Skip to content

Commit

Permalink
move to cache dir
Browse files Browse the repository at this point in the history
  • Loading branch information
tiptenbrink committed Jan 13, 2024
1 parent c1a7478 commit 9f7e82b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 40 deletions.
86 changes: 84 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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"
6 changes: 3 additions & 3 deletions src/archives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf, RepoError> {
let archive_path_name = archive_path.to_str().ok_or(FileError {
msg: format!("Cannot represent path {:?} as string!", archive_path),
source: FileErrorKind::InvalidPath,
Expand Down Expand Up @@ -110,5 +110,5 @@ pub(crate) fn extract_archive(

println!("Extracted archive.");

Ok(())
Ok(target_path)
}
44 changes: 22 additions & 22 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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)]
Expand All @@ -34,7 +35,7 @@ struct Cli {
#[arg(long, value_enum, global = true)]
context: Option<StateContext>,

/// 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.
Expand Down Expand Up @@ -106,11 +107,11 @@ enum ErrorRepr {
}

fn create_repo(repo: Repo) -> Result<PathBuf, RepoError> {
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)
}
Expand Down Expand Up @@ -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!(
"{}_{}_{}",
Expand All @@ -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,
)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -19,3 +20,20 @@ pub(crate) enum FileErrorKind {
pub(crate) fn get_current_dir() -> Result<PathBuf, FileErrorKind> {
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<Dirs> = 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 }
})
}

0 comments on commit 9f7e82b

Please sign in to comment.