Skip to content

Commit

Permalink
use utf-8 path
Browse files Browse the repository at this point in the history
  • Loading branch information
tiptenbrink committed May 3, 2024
1 parent f2bdf07 commit 2ffb5ba
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 113 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ tracing-error = "=0.2.0"
directories = "=5.0.1"
color-eyre = "=0.6.3"
test-log = { version="=0.2.15", default-features = false, features = ["trace"] }
duct = "=0.13.7"
duct = "=0.13.7"
camino = "1.1.6"
once_cell = "1.19.0"
24 changes: 9 additions & 15 deletions src/archives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use crate::errors::{RepoError, TarError};
use crate::filesystem::{FileError, FileErrorKind};
use crate::process::process_out;
use std::fs;
use std::path::{Path, PathBuf};
use camino::{Utf8Path, Utf8PathBuf};
use std::process::Command as Cmd;
use tracing::debug;

pub(crate) fn make_archive(
archives_path: &Path,
current_dir: &Path,
archives_path: &Utf8Path,
current_dir: &Utf8Path,
source_name: &str,
target_name: &str,
) -> Result<PathBuf, RepoError> {
) -> Result<Utf8PathBuf, RepoError> {
if !archives_path.exists() {
fs::create_dir_all(archives_path).map_err(|e| {
RepoError::from_io(
Expand All @@ -24,10 +24,7 @@ pub(crate) fn make_archive(
let archive_name = format!("{}.tar.gz", target_name);

let archive_path = archives_path.join(archive_name);
let archive_path_name = archive_path.to_str().ok_or(FileError {
msg: format!("Cannot represent path {:?} as string!", archive_path),
source: FileErrorKind::InvalidPath,
})?;
let archive_path_name = archive_path.as_str();

if archive_path.exists() {
return Ok(archive_path);
Expand Down Expand Up @@ -56,14 +53,11 @@ pub(crate) fn make_archive(
}

pub(crate) fn extract_archive(
archive_path: &Path,
current_dir: &Path,
archive_path: &Utf8Path,
current_dir: &Utf8Path,
target_name: &str,
) -> 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,
})?;
) -> Result<Utf8PathBuf, RepoError> {
let archive_path_name = archive_path.as_str();

let target_path = current_dir.join(target_name);
debug!("Extracting archive {:?} to {:?}", archive_path, target_path);
Expand Down
40 changes: 27 additions & 13 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::path::{Path, PathBuf};
use camino::{Utf8Path, Utf8PathBuf};
use std::process::ExitCode;

use crate::archives::{extract_archive, make_archive};
use crate::errors::{ProcessError, RepoError};
use crate::filesystem::get_dirs;
use crate::filesystem::{get_dirs, FileError, FileErrorKind};
use crate::git::{checkout, checkout_path, repo_clone, Repo};
use crate::next::commands::{match_command, NextSub};
use crate::next::run::run_command_input_old_state;
Expand Down Expand Up @@ -111,20 +111,24 @@ enum ErrorRepr {
Repo(#[from] RepoError),
}

fn create_repo(repo: Repo) -> Result<PathBuf, RepoError> {
let cache_dir = get_dirs().cache.as_path();
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 repo_name = repo.dir_name();
let repo_path = cache_dir.join(&repo_name);

repo_clone(cache_dir, &repo_name, &repo.url)?;
repo_clone(&cache_dir, &repo_name, &repo.url)?;

Ok(repo_path)
}

fn switch_to_revision(
cli_state: CliEnvState,
state: State,
repo_path: &Path,
repo_path: &Utf8Path,
) -> Result<State, ErrorRepr> {
let commit_short = &state.commit_sha[0..7];
let deploy_path_str = format!("{:?}", state.deploy_path);
Expand Down Expand Up @@ -153,8 +157,11 @@ fn switch_to_revision(
Ok(state)
}

fn prepare_from_state(state: &State, repo_path: &Path) -> Result<(), ErrorRepr> {
let cache_dir = get_dirs().cache.as_path();
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 archives = cache_dir.join("archives");
let deploy_encoded = B64USNP.encode(state.deploy_path.as_str());
let archive_name = format!(
Expand All @@ -164,8 +171,8 @@ fn prepare_from_state(state: &State, repo_path: &Path) -> Result<(), ErrorRepr>

make_archive(
&archives,
cache_dir,
repo_path.file_name().unwrap().to_string_lossy().as_ref(),
&cache_dir,
repo_path.file_name().unwrap(),
&archive_name,
)
.map_err(ErrorRepr::Repo)?;
Expand Down Expand Up @@ -212,7 +219,10 @@ fn prepare_command(
let _prep_enter = prepare_san.enter();

let repo_path = if no_create {
let cache_dir = get_dirs().cache.as_path();
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 @@ -287,8 +297,12 @@ pub fn run_cli() -> Result<ExitCode, Report> {
enter_dl.exit();

let state = prepare_command(cli_state.clone(), no_create, state.repo)?.unwrap();
let cache_dir = get_dirs().cache.as_path();
let tmp_dir = get_dirs().tmp.as_path();
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());
let archive_name = format!(
"{}_{}_{}",
Expand Down
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use camino::Utf8Path;
use relative_path::{RelativePath, RelativePathBuf};
use serde::Deserialize;
use std::{
Expand Down Expand Up @@ -127,7 +128,7 @@ fn overwrite_config(root_config: DployConfig, overwrite_config: DployConfig) ->
/// Looks at config at start_path and appends levels from final_path, looking at a config at every level. It then
/// combines them.
pub(crate) fn traverse_configs(
start_path: &Path,
start_path: &Utf8Path,
final_path: &RelativePath,
) -> Result<DployConfig, ConfigError> {
debug!(
Expand Down
48 changes: 39 additions & 9 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use camino::{Utf8Path, Utf8PathBuf};
use directories::ProjectDirs;
use std::{env, io::Error as StdIOError, path::PathBuf, sync::OnceLock};
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 All @@ -17,23 +20,50 @@ pub(crate) enum FileErrorKind {
InvalidPath,
}

pub(crate) fn get_current_dir() -> Result<PathBuf, FileErrorKind> {
env::current_dir().map_err(FileErrorKind::IO)
pub(crate) fn get_current_dir() -> Result<Utf8PathBuf, FileErrorKind> {
let current_dir = env::current_dir().map_err(FileErrorKind::IO)?;
Utf8PathBuf::from_path_buf(current_dir).map_err(|_e| FileErrorKind::InvalidPath)
}

pub(crate) struct Dirs {
pub(crate) cache: PathBuf,
pub(crate) tmp: PathBuf,
pub(crate) cache: Utf8PathBuf,
pub(crate) tmp: Utf8PathBuf,
}

pub(crate) fn get_dirs() -> &'static Dirs {
static DIRS: OnceLock<Dirs> = OnceLock::new();
DIRS.get_or_init(|| {
pub(crate) fn get_dirs() -> Result<&'static Dirs, FileErrorKind> {
static DIRS: OnceCell<Dirs> = OnceCell::new();
DIRS.get_or_try_init(|| {
let project_dirs = ProjectDirs::from("", "", "tidploy").unwrap();

let cache = project_dirs.cache_dir().to_owned();
let tmp = env::temp_dir();
let cache = Utf8PathBuf::from_path_buf(cache).map_err(|_e| FileErrorKind::InvalidPath)?;
let tmp = Utf8PathBuf::from_path_buf(tmp).map_err(|_e| FileErrorKind::InvalidPath)?;

Dirs { cache, tmp }
Ok(Dirs { cache, tmp })
})
}

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

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()
}
}

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()
}
}
18 changes: 9 additions & 9 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use crate::errors::{GitError, RepoError, RepoParseError};

use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64USNP;
use base64::Engine;
use camino::Utf8Path;
use relative_path::RelativePath;
use spinoff::{spinners, Spinner};

use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::process::{Command as Cmd, Stdio};
use tracing::debug;

Expand Down Expand Up @@ -70,7 +70,7 @@ pub(crate) fn parse_repo_url(url: String) -> Result<Repo, RepoParseError> {
}

fn run_git<S: AsRef<OsStr>>(
current_dir: &Path,
current_dir: &Utf8Path,
args: Vec<S>,
op_name: &'static str,
) -> Result<String, GitError> {
Expand Down Expand Up @@ -100,7 +100,7 @@ fn run_git<S: AsRef<OsStr>>(
.to_owned())
}

pub(crate) fn git_root_origin_url(path: &Path) -> Result<String, GitError> {
pub(crate) fn git_root_origin_url(path: &Utf8Path) -> Result<String, GitError> {
let args = vec!["config", "--get", "remote.origin.url"];

let url = run_git(path, args, "get git root origin url")?;
Expand All @@ -110,20 +110,20 @@ pub(crate) fn git_root_origin_url(path: &Path) -> Result<String, GitError> {
Ok(url)
}

pub(crate) fn git_root_dir(path: &Path) -> Result<String, GitError> {
pub(crate) fn git_root_dir(path: &Utf8Path) -> Result<String, GitError> {
let args = vec!["rev-parse", "--show-toplevel"];

run_git(path, args, "get git root dir")
}

pub(crate) fn rev_parse_tag(tag: &str, path: &Path) -> Result<String, GitError> {
pub(crate) fn rev_parse_tag(tag: &str, path: &Utf8Path) -> Result<String, GitError> {
let args = vec!["rev-parse", tag];

run_git(path, args, "rev parse tag")
}

pub(crate) fn repo_clone(
current_dir: &Path,
current_dir: &Utf8Path,
target_name: &str,
repo_url: &str,
) -> Result<(), RepoError> {
Expand Down Expand Up @@ -188,7 +188,7 @@ pub(crate) fn repo_clone(
Ok(())
}

pub(crate) fn checkout(repo_path: &Path, commit_sha: &str) -> Result<(), RepoError> {
pub(crate) fn checkout(repo_path: &Utf8Path, commit_sha: &str) -> Result<(), RepoError> {
if !repo_path.exists() {
return Err(RepoError::NotCreated);
}
Expand Down Expand Up @@ -227,11 +227,11 @@ pub(crate) fn checkout(repo_path: &Path, commit_sha: &str) -> Result<(), RepoErr
Ok(())
}

pub(crate) fn checkout_path(repo_path: &Path, 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: &Path, 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
12 changes: 6 additions & 6 deletions src/next/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::{
collections::HashMap,
fs,
ops::ControlFlow,
path::{Path, PathBuf},
};

use camino::{Utf8Path, Utf8PathBuf};
use relative_path::{RelativePath, RelativePathBuf};
use serde::Deserialize;
use tracing::debug;

use crate::{next::errors::WrapConfigErr, state::State};
use crate::{filesystem::WrapToPath, next::errors::WrapConfigErr, state::State};

use super::errors::ConfigError;

Expand Down Expand Up @@ -55,7 +55,7 @@ pub(crate) struct Config {
pub(crate) state: Option<StateConfig>
}

pub(crate) fn load_dploy_config(config_dir_path: &Path) -> Result<Config, ConfigError> {
pub(crate) fn load_dploy_config(config_dir_path: &Utf8Path) -> Result<Config, ConfigError> {
let toml_path = config_dir_path.join("tidploy.toml");
let json_path = config_dir_path.join("tidploy.json");
let choose_json = json_path.exists();
Expand Down Expand Up @@ -177,7 +177,7 @@ fn overwrite_config(root_config: Config, overwrite_config: Config) -> Config {
}

pub(crate) fn traverse_configs(
start_path: &Path,
start_path: &Utf8Path,
final_path: &RelativePath,
) -> Result<Config, ConfigError> {
debug!(
Expand All @@ -187,11 +187,11 @@ pub(crate) fn traverse_configs(

let root_config = load_dploy_config(start_path)?;

let paths: Vec<PathBuf> = final_path
let paths: Vec<Utf8PathBuf> = final_path
.components()
.scan(RelativePathBuf::new(), |state, component| {
state.push(component);
Some(state.to_path(start_path))
Some(state.to_utf8_path(start_path))
})
.collect();

Expand Down
Loading

0 comments on commit 2ffb5ba

Please sign in to comment.