From bb69549dc8d08924a94cc0bfdb046c849059427d Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sat, 1 Sep 2018 17:14:38 -0500 Subject: [PATCH] fix env --- src/github.rs | 12 ++++++++---- src/handler.rs | 13 +++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/github.rs b/src/github.rs index 0dbd0a9..69efb6d 100644 --- a/src/github.rs +++ b/src/github.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::env; use std::iter::FromIterator; use std::path::PathBuf; use std::process::Command; @@ -14,6 +13,8 @@ use sha1::Sha1; use structopt::StructOpt; use toml::Value as TomlValue; +use handler::Environment; + #[derive(StructOpt)] struct Opt { /// JSON input @@ -50,7 +51,11 @@ fn default_path() -> PathBuf { PathBuf::from(".") } -pub fn main(config: &TomlValue, input: &JsonValue) -> Result { +pub(crate) fn main( + env: &Environment, + config: &TomlValue, + input: &JsonValue, +) -> Result { let config_str = { let mut buf: Vec = Vec::new(); { @@ -94,8 +99,7 @@ pub fn main(config: &TomlValue, input: &JsonValue) -> Result { let payload: GithubPayload = serde_json::from_str(&payload.body).expect("Could not parse Github input into json"); - let mut target_path = - PathBuf::from(env::var("DIP_WORKDIR").expect("Could not determine working directory")); + let mut target_path = env.workdir.clone(); target_path.push(&config.path); Command::new("git") .arg("clone") diff --git a/src/handler.rs b/src/handler.rs index 434a59f..cecddc4 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -26,13 +26,20 @@ pub struct Handler { #[derive(Clone)] pub enum Action { /// A builtin function (for example, the Github handler). - Builtin(fn(&TomlValue, &JsonValue) -> Result), + Builtin(fn(&Environment, &TomlValue, &JsonValue) -> Result), /// A command represents a string to be executed by `bash -c`. Command(String), /// A program represents one of the handlers specified in the `handlers` directory. Program(String), } +/// Describes the environment for running a builtin. +#[derive(Clone)] +pub struct Environment { + /// The current working directory. + pub workdir: PathBuf, +} + impl fmt::Debug for Action { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match &self { @@ -94,7 +101,9 @@ impl Handler { let output: Box + Send> = match action { Action::Builtin(ref func) => { - let result = func(&config, &input); + let workdir = temp_path_cp.clone(); + let env = Environment { workdir }; + let result = func(&env, &config, &input); Box::new(future::result(result)) } Action::Command(ref cmd) => {