Skip to content

Commit

Permalink
Large change to resolving logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tiptenbrink committed May 16, 2024
1 parent 6dfecf9 commit 423a37b
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 237 deletions.
2 changes: 1 addition & 1 deletion examples/config/end/here/there/tidploy.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[argument]
executable = "here/there/example_im_there.sh"
executable = "example_im_there.sh"
2 changes: 2 additions & 0 deletions examples/config/move_resolve/inner/alternate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
echo "I'm in here!"
5 changes: 5 additions & 0 deletions examples/config/move_resolve/inner/tidploy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[state.address]
path = "."

[argument]
executable = "alternate.sh"
51 changes: 50 additions & 1 deletion src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use camino::{Utf8Path, Utf8PathBuf};
use camino::{Utf8Component, Utf8Path, Utf8PathBuf};
use directories::ProjectDirs;
use once_cell::sync::OnceCell;
use relative_path::{RelativePath, RelativePathBuf};
Expand Down Expand Up @@ -98,3 +98,52 @@ impl WrapToPath for RelativePathBuf {
// }
// }
}

pub trait PathClean {
fn clean(&self) -> Utf8PathBuf;
}

impl PathClean for Utf8Path {
fn clean(&self) -> Utf8PathBuf {
clean(self)
}
}

impl PathClean for Utf8PathBuf {
fn clean(&self) -> Utf8PathBuf {
clean(self)
}
}

/// From https://github.com/danreeves/path-clean/blob/3876d7cb5367997bcda17ce165bf69c4f434cb93/src/lib.rs
/// By Dan Reeves, used under the Apache License 2.0
/// Changed to work for Utf8Path
pub fn clean<P>(path: P) -> Utf8PathBuf
where
P: AsRef<Utf8Path>,
{
let mut out = Vec::new();

for comp in path.as_ref().components() {
match comp {
Utf8Component::CurDir => (),
Utf8Component::ParentDir => match out.last() {
Some(Utf8Component::RootDir) => (),
Some(Utf8Component::Normal(_)) => {
out.pop();
}
None
| Some(Utf8Component::CurDir)
| Some(Utf8Component::ParentDir)
| Some(Utf8Component::Prefix(_)) => out.push(comp),
},
comp => out.push(comp),
}
}

if !out.is_empty() {
out.iter().collect()
} else {
Utf8PathBuf::from(".")
}
}
4 changes: 2 additions & 2 deletions src/next/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ impl GlobalArguments {
fn run_in(&self) -> AddressIn {
match self.address.clone() {
Some(address) => address,
None => AddressIn::from_run(None, None, None),
None => AddressIn::from_run(None, None),
}
}

fn secret_in(&self) -> AddressIn {
match self.address.clone() {
Some(address) => address,
None => AddressIn::from_secret(None, None, None),
None => AddressIn::from_secret(None, None),
}
}

Expand Down
17 changes: 7 additions & 10 deletions src/next/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ pub struct NextSub {
/// the current directory or Git root dir
#[arg(long = "resolve-root")]
resolve_root: Option<String>,

/// Location relative to state root to stop reading configs, inclusive.
#[arg(long = "state-root")]
state_root: Option<String>,
// /// Location relative to state root to stop reading configs, inclusive.
// #[arg(long = "state-root")]
// state_root: Option<String>,
}

#[derive(Subcommand, Debug)]
Expand Down Expand Up @@ -79,7 +78,7 @@ pub enum NextCommands {
repo: Option<String>,

#[arg(long = "local")]
local: bool
local: bool,
},
}

Expand All @@ -101,7 +100,6 @@ pub enum AddressSubCommands {
pub fn match_command(next_sub: NextSub, _cmd: Command) -> Result<ExitCode, Report> {
let NextSub {
subcommand,
state_root,
resolve_root,
} = next_sub;

Expand All @@ -111,7 +109,7 @@ pub fn match_command(next_sub: NextSub, _cmd: Command) -> Result<ExitCode, Repor
cwd_infer,
state_path,
} => {
let addr_in = AddressIn::from_secret(resolve_root, state_path, state_root);
let addr_in = AddressIn::from_secret(resolve_root, state_path);

secret_command(addr_in, cwd_infer, None, None, key, None)?;

Expand All @@ -124,7 +122,7 @@ pub fn match_command(next_sub: NextSub, _cmd: Command) -> Result<ExitCode, Repor
git_infer,
state_path,
} => {
let addr_in = AddressIn::from_run(resolve_root, state_path, state_root);
let addr_in = AddressIn::from_run(resolve_root, state_path);
let out = run_command(
addr_in,
git_infer,
Expand All @@ -147,8 +145,7 @@ pub fn match_command(next_sub: NextSub, _cmd: Command) -> Result<ExitCode, Repor
git_ref,
state_path,
} => {
let addr_in =
AddressIn::from_deploy(repo, local, git_ref, resolve_root, state_path, state_root);
let addr_in = AddressIn::from_deploy(repo, local, git_ref, resolve_root, state_path);
let out = run_command(
addr_in,
!cwd_infer,
Expand Down
109 changes: 76 additions & 33 deletions src/next/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ pub(crate) enum ConfigAddress {
Local {
path: String,
state_path: Option<String>,
state_root: Option<String>,
// arg_root: Option<String>,
// arg_path: Option<String>,
},
Git {
url: String,
local: Option<bool>,
git_ref: String,
target_path: Option<String>,
state_path: Option<String>,
state_root: Option<String>,
// arg_root: Option<String>,
// arg_path: Option<String>,
},
}

#[derive(Deserialize, Debug)]
pub(crate) struct StateConfig {
pub(crate) state_root: Option<String>,
pub(crate) state_path: Option<String>,
pub(crate) address: Option<ConfigAddress>,
}

Expand Down Expand Up @@ -93,6 +93,13 @@ pub(crate) fn load_dploy_config(config_dir_path: &Utf8Path) -> Result<Config, Co
Ok(dploy_config)
}

// pub(crate) fn load_arg_config(config_dir_path: &Utf8Path) -> Result<Option<ArgumentConfig>, ConfigError> {
// let config = load_dploy_config(config_dir_path)?;
// config.a

// Ok(dploy_config)
// }

pub(crate) fn overwrite_option<T>(original: Option<T>, replacing: Option<T>) -> Option<T> {
match replacing {
Some(replacing) => Some(replacing),
Expand Down Expand Up @@ -164,32 +171,40 @@ fn overwrite_arguments(
}
}

fn overwrite_state_config(base: StateConfig, replacing: StateConfig) -> StateConfig {
StateConfig {
state_path: replacing.state_path.or(base.state_path),
state_root: replacing.state_root.or(base.state_root),
address: replacing.address.or(base.address),
}
}

fn overwrite_config(root_config: Config, overwrite_config: Config) -> Config {
Config {
argument: merge_option(
root_config.argument,
overwrite_config.argument,
&overwrite_arguments,
),
state: merge_option(
root_config.state,
overwrite_config.state,
&overwrite_state_config,
),
}
// fn overwrite_state_config(base: StateConfig, replacing: StateConfig) -> StateConfig {
// StateConfig {
// address: replacing.address.or(base.address),
// }
// }

// fn overwrite_config(root_config: Config, overwrite_config: Config) -> Config {
// Config {
// argument: merge_option(
// root_config.argument,
// overwrite_config.argument,
// &overwrite_arguments,
// ),
// state: merge_option(
// root_config.state,
// overwrite_config.state,
// &overwrite_state_config,
// ),
// }
// }

fn overwrite_arg_config(
root_config: Option<ArgumentConfig>,
overwrite_config: Option<ArgumentConfig>,
) -> Option<ArgumentConfig> {
merge_option(root_config, overwrite_config, &overwrite_arguments)
}

/// The relative path is normalized, so if it contains symlinks unexpected behavior might happen.
/// This is designed to work only for simple descent down a directory.
fn get_component_paths(start_path: &Utf8Path, final_path: &RelativePath) -> Vec<Utf8PathBuf> {
pub(crate) fn get_component_paths(
start_path: &Utf8Path,
final_path: &RelativePath,
) -> Vec<Utf8PathBuf> {
let paths: Vec<Utf8PathBuf> = final_path
.normalize()
.components()
Expand All @@ -202,26 +217,54 @@ fn get_component_paths(start_path: &Utf8Path, final_path: &RelativePath) -> Vec<
paths
}

/// Be sure the relative path is just a simple ./child/child/child2 ...etc relative path (the leading
/// ./ is optional)
pub(crate) fn traverse_configs(
// /// Be sure the relative path is just a simple ./child/child/child2 ...etc relative path (the leading
// /// ./ is optional)
// pub(crate) fn traverse_configs(
// start_path: &Utf8Path,
// final_path: &RelativePath,
// ) -> Result<Config, ConfigError> {
// debug!(
// "Traversing configs from {:?} to relative {:?}",
// start_path, final_path
// );

// let root_config = load_dploy_config(start_path)?;

// let paths = get_component_paths(start_path, final_path);

// let combined_config = paths.iter().try_fold(root_config, |state, path| {
// let inner_config = load_dploy_config(path);

// match inner_config {
// Ok(config) => ControlFlow::Continue(overwrite_config(state, config)),
// Err(source) => ControlFlow::Break(source),
// }
// });

// match combined_config {
// ControlFlow::Break(e) => Err(e),
// ControlFlow::Continue(config) => Ok(config),
// }
// }

pub(crate) fn traverse_arg_configs(
start_path: &Utf8Path,
final_path: &RelativePath,
) -> Result<Config, ConfigError> {
) -> Result<Option<ArgumentConfig>, ConfigError> {
debug!(
"Traversing configs from {:?} to relative {:?}",
start_path, final_path
);

let root_config = load_dploy_config(start_path)?;
let root_config = load_dploy_config(start_path)?.argument;

let paths = get_component_paths(start_path, final_path);

let combined_config = paths.iter().try_fold(root_config, |state, path| {
let inner_config = load_dploy_config(path);
let inner_config = load_dploy_config(path).map(|c| c.argument);

match inner_config {
Ok(config) => ControlFlow::Continue(overwrite_config(state, config)),
Ok(config) => ControlFlow::Continue(overwrite_arg_config(state, config)),
Err(source) => ControlFlow::Break(source),
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/next/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub(crate) enum StateErrorKind {
InvalidPath,
#[error("State manipulation failed due to IO error! {0}")]
IO(#[from] IOError),
#[error("{0}")]
InvalidRoot(String),
// #[error("{0}")]
// InvalidRoot(String),
#[error("{0}")]
Secret(#[from] SecretError),
#[error("{0}")]
Expand Down
Loading

0 comments on commit 423a37b

Please sign in to comment.