Skip to content

Commit

Permalink
address local working, path and state improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tiptenbrink committed May 4, 2024
1 parent 7fcfa53 commit 4112c58
Show file tree
Hide file tree
Showing 24 changed files with 663 additions and 181 deletions.
101 changes: 101 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ duct = "=0.13.7"
camino = "1.1.6"
once_cell = "1.19.0"
tar = "0.4.40"
flate2 = "1.0.30"
flate2 = "1.0.30"
merkle_hash = "3.6"
2 changes: 2 additions & 0 deletions examples/config/end/here/there/example_im_there.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
echo "I'm there!"
2 changes: 2 additions & 0 deletions examples/config/end/here/there/tidploy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[argument]
executable = "here/there/example_im_there.sh"
2 changes: 2 additions & 0 deletions examples/config/end/here/tidploy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[argument]
executable = "here/there/example_im_not_here.sh"
3 changes: 3 additions & 0 deletions examples/config/start/tidploy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[state.address]
path = "../end"
state_path = "here/there"
2 changes: 1 addition & 1 deletion src/archives.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::errors::{RepoError, TarError};
use crate::filesystem::{FileError, FileErrorKind};
use crate::process::process_out;
use std::fs;
use camino::{Utf8Path, Utf8PathBuf};
use std::fs;
use std::process::Command as Cmd;
use tracing::debug;

Expand Down
50 changes: 33 additions & 17 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,15 @@ enum ErrorRepr {
}

fn create_repo(repo: Repo) -> Result<Utf8PathBuf, RepoError> {

let cache_dir = get_dirs().map_err(|e| RepoError::File(FileError {
msg: "Error getting dirs!".to_owned(),
source: e
}))?.cache.clone();
let cache_dir = get_dirs()
.map_err(|e| {
RepoError::File(FileError {
msg: "Error getting dirs!".to_owned(),
source: e,
})
})?
.cache
.clone();
let repo_name = repo.dir_name();
let repo_path = cache_dir.join(&repo_name);

Expand Down Expand Up @@ -158,10 +162,15 @@ fn switch_to_revision(
}

fn prepare_from_state(state: &State, repo_path: &Utf8Path) -> Result<(), ErrorRepr> {
let cache_dir = get_dirs().map_err(|e| ErrorRepr::Repo(RepoError::File(FileError {
msg: "Cache dir not UTF-8!".to_owned(),
source: e
})))?.cache.clone();
let cache_dir = get_dirs()
.map_err(|e| {
ErrorRepr::Repo(RepoError::File(FileError {
msg: "Cache dir not UTF-8!".to_owned(),
source: e,
}))
})?
.cache
.clone();
let archives = cache_dir.join("archives");
let deploy_encoded = B64USNP.encode(state.deploy_path.as_str());
let archive_name = format!(
Expand Down Expand Up @@ -219,10 +228,15 @@ fn prepare_command(
let _prep_enter = prepare_san.enter();

let repo_path = if no_create {
let cache_dir = get_dirs().map_err(|e| ErrorRepr::Repo(RepoError::File(FileError {
msg: "Cache dir not UTF-8!".to_owned(),
source: e
})))?.cache.clone();
let cache_dir = get_dirs()
.map_err(|e| {
ErrorRepr::Repo(RepoError::File(FileError {
msg: "Cache dir not UTF-8!".to_owned(),
source: e,
}))
})?
.cache
.clone();
let repo_path = cache_dir.join(repo.dir_name());

if !repo_path.exists() {
Expand Down Expand Up @@ -297,10 +311,12 @@ pub fn run_cli() -> Result<ExitCode, Report> {
enter_dl.exit();

let state = prepare_command(cli_state.clone(), no_create, state.repo)?.unwrap();
let dirs = get_dirs().map_err(|e| ErrorRepr::Repo(RepoError::File(FileError {
msg: "Cache dir not UTF-8!".to_owned(),
source: e
})))?;
let dirs = get_dirs().map_err(|e| {
ErrorRepr::Repo(RepoError::File(FileError {
msg: "Cache dir not UTF-8!".to_owned(),
source: e,
}))
})?;
let cache_dir = dirs.cache.as_path();
let tmp_dir = dirs.tmp.as_path();
let deploy_encoded = B64USNP.encode(state.deploy_path.as_str());
Expand Down
44 changes: 38 additions & 6 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use camino::{Utf8Path, Utf8PathBuf};
use directories::ProjectDirs;
use once_cell::sync::OnceCell;
use relative_path::{RelativePath, RelativePathBuf};
use std::{env, io::Error as StdIOError};
use thiserror::Error as ThisError;
use once_cell::sync::OnceCell;

#[derive(ThisError, Debug)]
#[error("{msg} {source}")]
Expand Down Expand Up @@ -44,26 +44,58 @@ pub(crate) fn get_dirs() -> Result<&'static Dirs, FileErrorKind> {
})
}

#[derive(Debug, ThisError)]
pub(crate) enum RelativePathError {
#[error("The full path {0} is not a child of the root (did you use too many ..?")]
Child(String),
#[error(
"An error occurred when canonicalizing the full path. Does it exist and is it UTF-8? {0}"
)]
Canonicalize(#[from] std::io::Error),
}

pub trait WrapToPath {
fn to_utf8_path<P: AsRef<Utf8Path>>(&self, path: P) -> Utf8PathBuf;

// fn to_path_canon_checked(&self, root: &Utf8Path) -> Result<Utf8PathBuf, RelativePathError>;
}

impl WrapToPath for RelativePath
{
impl WrapToPath for RelativePath {
fn to_utf8_path<P: AsRef<Utf8Path>>(&self, path: P) -> Utf8PathBuf {
let path = path.as_ref().as_std_path();
let std_path = self.to_path(path);
// Since we started with Utf8Path, we know this will work
Utf8PathBuf::from_path_buf(std_path).unwrap()
}

// fn to_path_canon_checked(&self, root: &Utf8Path) -> Result<Utf8PathBuf, RelativePathError> {
// let full = self.to_utf8_path(root);


// if !full_canon.starts_with(root) {
// Err(RelativePathError::Child(full_canon.to_string()))
// } else {
// Ok(full_canon)
// }
// }
}

impl WrapToPath for RelativePathBuf
{
impl WrapToPath for RelativePathBuf {
fn to_utf8_path<P: AsRef<Utf8Path>>(&self, path: P) -> Utf8PathBuf {
let path = path.as_ref().as_std_path();
let std_path = self.to_path(path);
// Since we started with Utf8Path, we know this will work
Utf8PathBuf::from_path_buf(std_path).unwrap()
}
}

// fn to_path_canon_checked(&self, root: &Utf8Path) -> Result<Utf8PathBuf, RelativePathError> {
// let full = self.to_utf8_path(root);
// let full_canon = full.canonicalize_utf8()?;

// if !full_canon.starts_with(root) {
// Err(RelativePathError::Child(full_canon.to_string()))
// } else {
// Ok(full_canon)
// }
// }
}
10 changes: 8 additions & 2 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,17 @@ pub(crate) fn checkout(repo_path: &Utf8Path, commit_sha: &str) -> Result<(), Rep
Ok(())
}

pub(crate) fn checkout_path(repo_path: &Utf8Path, deploy_path: &RelativePath) -> Result<(), RepoError> {
pub(crate) fn checkout_path(
repo_path: &Utf8Path,
deploy_path: &RelativePath,
) -> Result<(), RepoError> {
checkout_paths(repo_path, vec![deploy_path])
}

pub(crate) fn checkout_paths(repo_path: &Utf8Path, paths: Vec<&RelativePath>) -> Result<(), RepoError> {
pub(crate) fn checkout_paths(
repo_path: &Utf8Path,
paths: Vec<&RelativePath>,
) -> Result<(), RepoError> {
if !repo_path.exists() {
return Err(RepoError::NotCreated);
}
Expand Down
Loading

0 comments on commit 4112c58

Please sign in to comment.