Skip to content

Commit

Permalink
work on creating public api for commands for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tiptenbrink committed Apr 29, 2024
1 parent 34593f8 commit 445fb47
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 8 deletions.
88 changes: 88 additions & 0 deletions src/next/api.rs
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
})
}
4 changes: 3 additions & 1 deletion src/next/mod.rs
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;
6 changes: 4 additions & 2 deletions src/next/run.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use color_eyre::eyre::{Context, Report};
use tracing::{debug, instrument};

use crate::{archives::extract_archive, filesystem::get_dirs, state::{create_state_create, create_state_run, extra_envs, CliEnvState}};

use super::process::run_entrypoint;

use crate::{archives::extract_archive, filesystem::get_dirs, state::{create_state_create, extra_envs, CliEnvState}};

use super::{process::run_entrypoint, state::create_state_run};

#[instrument(name = "run", level = "debug", skip_all)]
pub(crate) fn run_command(cli_state: CliEnvState, executable: Option<String>, variables: Vec<String>, archive: Option<String>) -> Result<(), Report> {
Expand Down
41 changes: 41 additions & 0 deletions src/next/state.rs
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)?)
}
14 changes: 9 additions & 5 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ use thiserror::Error as ThisError;

use tracing::{debug, span, Level};

/// The different contexts that tidploy will use to populate its configuration. 'None' means it will
/// not consider that it is currently in a Git project and will only pick up configuration in its
/// current directory.
#[non_exhaustive]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
pub(crate) enum StateContext {
pub enum StateContext {
None,
GitRemote,
GitLocal,
Expand Down Expand Up @@ -83,9 +87,9 @@ pub(crate) enum LoadError {
}

#[derive(Clone, Debug)]
struct CliEnvRunState {
envs: Vec<ConfigVar>,
exe_name: Option<String>,
pub(crate) struct CliEnvRunState {
pub(crate) envs: Vec<ConfigVar>,
pub(crate) exe_name: Option<String>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -338,7 +342,7 @@ pub(crate) fn create_state_run(
/// Create a new state, merging the cli_state, env var state and config state and potentially loading it from the
/// context of the supplied path (or current directory if not provided). If cli_run_state is None, no run_state is
/// loaded.
fn create_state(
pub(crate) fn create_state(
cli_state: CliEnvState,
cli_run_state: Option<CliEnvRunState>,
project_path: Option<&Path>,
Expand Down

0 comments on commit 445fb47

Please sign in to comment.