-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work on creating public api for commands for tests
- Loading branch information
1 parent
34593f8
commit 445fb47
Showing
5 changed files
with
145 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use super::run::{run_command as inner_run_command}; | ||
use crate::state::CliEnvState; | ||
pub use crate::state::StateContext; | ||
use color_eyre::eyre::Report; | ||
use thiserror::Error as ThisError; | ||
|
||
#[non_exhaustive] | ||
pub struct GlobalArguments { | ||
pub context: Option<StateContext>, | ||
pub repo_url: Option<String>, | ||
pub deploy_path: Option<String>, | ||
pub tag: Option<String>, | ||
} | ||
|
||
impl Default for GlobalArguments { | ||
fn default() -> Self { | ||
GlobalArguments { | ||
context: None, | ||
repo_url: None, | ||
deploy_path: None, | ||
tag: None | ||
} | ||
} | ||
} | ||
|
||
impl GlobalArguments { | ||
pub fn cli_env(context: Option<StateContext>, repo_url: Option<String>, deploy_path: Option<String>, tag: Option<String>) -> Self { | ||
GlobalArguments { | ||
context, | ||
repo_url, | ||
deploy_path, | ||
tag | ||
} | ||
} | ||
} | ||
|
||
#[non_exhaustive] | ||
pub struct RunArguments { | ||
pub executable: Option<String>, | ||
pub variables: Vec<String>, | ||
pub archive: Option<String> | ||
} | ||
|
||
impl Default for RunArguments { | ||
fn default() -> Self { | ||
RunArguments { | ||
executable: None, | ||
variables: Vec::new(), | ||
archive: None | ||
} | ||
} | ||
} | ||
|
||
impl RunArguments { | ||
pub fn with(executable: Option<String>, variables: Vec<String>, archive: Option<String>) -> Self { | ||
RunArguments { | ||
executable, | ||
variables, | ||
archive | ||
} | ||
} | ||
} | ||
|
||
impl From<GlobalArguments> for CliEnvState { | ||
fn from(args: GlobalArguments) -> Self { | ||
CliEnvState { | ||
context: args.context, | ||
repo_url: args.repo_url, | ||
deploy_path: args.deploy_path, | ||
tag: args.tag, | ||
} | ||
} | ||
} | ||
|
||
#[derive(ThisError, Debug)] | ||
#[error("{msg} {source}")] | ||
pub struct CommandError { | ||
msg: String, | ||
source: Report | ||
} | ||
|
||
pub fn run_command(global_args: GlobalArguments, args: RunArguments) -> Result<(), CommandError> { | ||
inner_run_command(global_args.into(), args.executable, args.variables, args.archive).map_err(|e| | ||
CommandError { | ||
msg: "An error occurred in the inner application layer.".to_owned(), | ||
source: e | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pub(crate) mod process; | ||
pub(crate) mod run; | ||
pub(crate) mod run; | ||
pub(crate) mod state; | ||
pub mod api; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::path::Path; | ||
|
||
use color_eyre::eyre::Report; | ||
use relative_path::RelativePath; | ||
use tracing::{debug, span, Level}; | ||
|
||
use crate::{config::ConfigVar, state::{create_state, CliEnvRunState, CliEnvState, State}}; | ||
|
||
/// Parses the list of strings given and interprets them as each pair of two being a secret key and target | ||
/// env name. | ||
fn parse_cli_envs(envs: Vec<String>) -> Vec<ConfigVar> { | ||
// Our chunk size is 2 so we know first and second exist | ||
// Any final element that does not have something to pair with will be ommitted | ||
envs.chunks_exact(2) | ||
.map(|c| ConfigVar { | ||
key: c.first().unwrap().to_owned(), | ||
env_name: c.get(1).unwrap().to_owned(), | ||
}) | ||
.collect() | ||
} | ||
|
||
/// Creates the state that is used to run the executable. Adds envs provided through CLI to `create_state`. | ||
pub(crate) fn create_state_run( | ||
cli_state: CliEnvState, | ||
exe_name: Option<String>, | ||
envs: Vec<String>, | ||
path: Option<&Path>, | ||
deploy_path: Option<&RelativePath>, | ||
load_tag: bool, | ||
) -> Result<State, Report> { | ||
// Exits when the function returns | ||
let run_state_span = span!(Level::DEBUG, "run_state"); | ||
let _enter = run_state_span.enter(); | ||
|
||
let cli_run_state = CliEnvRunState { | ||
exe_name, | ||
envs: parse_cli_envs(envs), | ||
}; | ||
debug!("Parsed CLI envs as {:?}", cli_run_state); | ||
Ok(create_state(cli_state, Some(cli_run_state), path, deploy_path, load_tag)?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters