Skip to content

Commit

Permalink
Merge pull request #460 from stepchowfun/typed-path
Browse files Browse the repository at this point in the history
Use `typed-path`
  • Loading branch information
stepchowfun authored Mar 8, 2023
2 parents 82c02b2 + f37fe69 commit 72eea47
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 225 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.46.2] - 2023-03-08

### Fixed
- Various bugs related to paths on Windows have been fixed with the help of [`typed-path`](https://crates.io/crates/typed-path).

## [0.46.1] - 2023-03-02

### Fixed
Expand Down
16 changes: 8 additions & 8 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "toast"
version = "0.46.1"
version = "0.46.2"
authors = ["Stephan Boyer <stephan@stephanboyer.com>"]
edition = "2021"
description = "Containerize your development and continuous integration environments."
Expand All @@ -25,8 +25,8 @@ serde_yaml = "0.8"
sha2 = "0.9"
tar = "0.4"
tempfile = "3"
typed-path = "0.3"
walkdir = "2"
path-slash = "0.2.1"

[dependencies.clap]
version = "2"
Expand Down
46 changes: 38 additions & 8 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
io::Read,
path::{Path, PathBuf},
},
typed_path::{UnixPath, UnixPathBuf},
};

#[cfg(unix)]
Expand Down Expand Up @@ -68,6 +69,18 @@ impl CryptoHash for PathBuf {
}
}

impl CryptoHash for UnixPath {
fn crypto_hash(&self) -> String {
hex::encode(Sha256::digest(self.as_bytes()))
}
}

impl CryptoHash for UnixPathBuf {
fn crypto_hash(&self) -> String {
hex::encode(Sha256::digest(self.as_bytes()))
}
}

// Combine two strings into a hash. The guarantees:
// 1. For all `x` and `y`, `combine(x, y)` = `combine(x, y)`.
// 2. For all known `x1`, `x2`, `y1`, and `y2`,
Expand Down Expand Up @@ -150,6 +163,7 @@ mod tests {
toastfile::{Task, Toastfile, DEFAULT_LOCATION, DEFAULT_USER},
},
std::{collections::HashMap, path::Path},
typed_path::UnixPath,
};

fn toastfile_with_task(foo_task: Task) -> Toastfile {
Expand All @@ -159,7 +173,7 @@ mod tests {
Toastfile {
image: "encom:os-12".to_owned(),
default: None,
location: Path::new(DEFAULT_LOCATION).to_owned(),
location: UnixPath::new(DEFAULT_LOCATION).to_owned(),
user: DEFAULT_USER.to_owned(),
command_prefix: String::new(),
tasks,
Expand All @@ -174,7 +188,7 @@ mod tests {
Toastfile {
image: "encom:os-12".to_owned(),
default: None,
location: Path::new(DEFAULT_LOCATION).to_owned(),
location: UnixPath::new(DEFAULT_LOCATION).to_owned(),
user: DEFAULT_USER.to_owned(),
command_prefix: String::new(),
tasks,
Expand Down Expand Up @@ -207,6 +221,22 @@ mod tests {
);
}

#[test]
fn hash_unix_path_pure() {
assert_eq!(
UnixPath::new("foo").crypto_hash(),
UnixPath::new("foo").crypto_hash(),
);
}

#[test]
fn hash_unix_path_not_constant() {
assert_ne!(
UnixPath::new("foo").crypto_hash(),
UnixPath::new("bar").crypto_hash(),
);
}

#[test]
fn combine_pure() {
assert_eq!(combine("foo", "bar"), combine("foo", "bar"));
Expand Down Expand Up @@ -299,8 +329,8 @@ mod tests {
dependencies: vec![],
cache: true,
environment,
input_paths: vec![Path::new("flob").to_owned()],
excluded_input_paths: vec![Path::new("thud").to_owned()],
input_paths: vec![UnixPath::new("flob").to_owned()],
excluded_input_paths: vec![UnixPath::new("thud").to_owned()],
output_paths: vec![],
output_paths_on_failure: vec![],
mount_paths: vec![],
Expand Down Expand Up @@ -613,8 +643,8 @@ mod tests {
dependencies: vec![],
cache: true,
environment: HashMap::new(),
input_paths: vec![Path::new("flob").to_owned()],
excluded_input_paths: vec![Path::new("thud").to_owned()],
input_paths: vec![UnixPath::new("flob").to_owned()],
excluded_input_paths: vec![UnixPath::new("thud").to_owned()],
output_paths: vec![],
output_paths_on_failure: vec![],
mount_paths: vec![],
Expand Down Expand Up @@ -671,7 +701,7 @@ mod tests {
mount_paths: vec![],
mount_readonly: false,
ports: vec![],
location: Some(Path::new("/foo").to_owned()),
location: Some(UnixPath::new("/foo").to_owned()),
user: None,
command: "echo wibble".to_owned(),
command_prefix: None,
Expand All @@ -690,7 +720,7 @@ mod tests {
mount_paths: vec![],
mount_readonly: false,
ports: vec![],
location: Some(Path::new("/bar").to_owned()),
location: Some(UnixPath::new("/bar").to_owned()),
user: None,
command: "echo wibble".to_owned(),
command_prefix: None,
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use {
crate::{failure, failure::Failure},
serde::{Deserialize, Serialize},
serde::Deserialize,
};

pub const REPO_DEFAULT: &str = "toast";
pub const EMPTY_CONFIG: &str = "{}";
const DOCKER_CLI_DEFAULT: &str = "docker";

// A program configuration
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive(Debug, Deserialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
#[allow(clippy::struct_excessive_bools)]
pub struct Config {
Expand Down
25 changes: 15 additions & 10 deletions src/docker.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use {
crate::{failure, failure::Failure, format::CodeStr, spinner::spin, toastfile::MappingPath},
path_slash::PathBufExt,
std::{
collections::HashMap,
env::current_dir,
fs::{copy, create_dir_all, rename, symlink_metadata, Metadata},
io,
io::Read,
path::{Path, PathBuf},
path::Path,
process::{ChildStdin, Command, Stdio},
string::ToString,
sync::{
Expand All @@ -16,6 +15,7 @@ use {
},
},
tempfile::tempdir,
typed_path::{TryAsRef, UnixPath, UnixPathBuf},
walkdir::WalkDir,
};

Expand Down Expand Up @@ -119,7 +119,7 @@ pub fn create_container(
mount_paths: &[MappingPath],
mount_readonly: bool,
ports: &[String],
location: &Path,
location: &UnixPath,
user: &str,
command: &str,
extra_args: &[String],
Expand Down Expand Up @@ -251,8 +251,8 @@ fn rename_or_copy_file_or_symlink(
pub fn copy_from_container(
docker_cli: &str,
container: &str,
paths: &[PathBuf],
source_dir: &Path,
paths: &[UnixPathBuf],
source_dir: &UnixPath,
destination_dir: &Path,
interrupted: &Arc<AtomicBool>,
) -> Result<(), Failure> {
Expand All @@ -278,7 +278,12 @@ pub fn copy_from_container(
// Figure out what needs to go where.
let source = source_dir.join(path);
let intermediate = temp_dir.path().join("data");
let destination = destination_dir.join(path);
let destination = destination_dir.join(path.try_as_ref().ok_or_else(|| {
Failure::User(
format!("Invalid path {}", path.to_string_lossy().code_str()),
None,
)
})?);

// Get the path from the container.
run_quiet(
Expand Down Expand Up @@ -454,7 +459,7 @@ pub fn spawn_shell(
image: &str,
source_dir: &Path,
environment: &HashMap<String, String>,
location: &Path,
location: &UnixPath,
mount_paths: &[MappingPath],
mount_readonly: bool,
ports: &[String],
Expand Down Expand Up @@ -501,7 +506,7 @@ pub fn spawn_shell(
fn container_args(
source_dir: &Path,
environment: &HashMap<String, String>,
location: &Path,
location: &UnixPath,
mount_paths: &[MappingPath],
mount_readonly: bool,
ports: &[String],
Expand Down Expand Up @@ -556,15 +561,15 @@ fn container_args(
absolute_source_dir
.join(&mount_path.host_path)
.to_string_lossy(),
location.join(&mount_path.container_path).to_slash_lossy(),
location.join(&mount_path.container_path).to_string_lossy(),
)
} else {
format!(
"type=bind,source={},target={}",
absolute_source_dir
.join(&mount_path.host_path)
.to_string_lossy(),
location.join(&mount_path.container_path).to_slash_lossy(),
location.join(&mount_path.container_path).to_string_lossy(),
)
},
]
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use {
},
},
toastfile::{default_task_mount_readonly, location, user, DEFAULT_USER},
typed_path::UnixPath,
};

#[macro_use]
Expand Down Expand Up @@ -750,9 +751,9 @@ fn entry() -> Result<(), Failure> {
// There is no last task, so the context will be the base image. Use default
// settings.
(
HashMap::default(), // [ref:default_environment]
Path::new("/").to_owned(), // `toastfile::DEFAULT_LOCATION` might not exist.
Vec::default(), // [ref:default_mount_paths]
HashMap::default(), // [ref:default_environment]
UnixPath::new("/").to_owned(), // `toastfile::DEFAULT_LOCATION` might not exist.
Vec::default(), // [ref:default_mount_paths]
default_task_mount_readonly(),
Vec::default(), // [ref:default_ports]
DEFAULT_USER.to_owned(),
Expand Down
Loading

0 comments on commit 72eea47

Please sign in to comment.