Skip to content

Commit

Permalink
Merge pull request #25 from jjcomer/master
Browse files Browse the repository at this point in the history
BREAKING -- Support multiple git SHA targets
  • Loading branch information
jonathanmorley authored Aug 8, 2019
2 parents 9ab7340 + 1024f76 commit 4bed3ed
Show file tree
Hide file tree
Showing 7 changed files with 701 additions and 381 deletions.
524 changes: 253 additions & 271 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hogan"
version = "0.2.10"
version = "0.3.0"
authors = ["Jonathan Morley <[email protected]>"]
edition = "2018"

Expand All @@ -11,26 +11,27 @@ doc = false

[dependencies]
failure = "0.1"
git2 = "0.7"
handlebars = "1"
itertools = "0.7"
git2 = "0.9"
lru_time_cache = "0.9.0"
handlebars = "2"
itertools = "0.8"
json-patch = "0.2"
log = "0.4"
regex = "1"
rouille = "2"
rouille = "3"
serde = "1"
serde_derive = "1"
serde_json = "1"
shellexpand = "1"
stderrlog = "0.4"
structopt = "0.2"
tempfile = "3"
url = "1"
url = "2"
walkdir = "2"
zip = "0.4"

[dev-dependencies]
assert_cmd = "0.9"
assert_cmd = "0.11"
dir-diff = "0.3"
fs_extra = "1"
predicates = "0.9"
predicates = "1"
85 changes: 68 additions & 17 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::find_file_paths;
use crate::git;
use failure::Error;
use git2::Repository;
use json_patch::merge;
use regex::Regex;
use serde_json::{self, Value};
Expand Down Expand Up @@ -90,7 +89,9 @@ pub enum ConfigDir {
directory: PathBuf,
},
Git {
git_repo: Repository,
url: Url,
head_sha: String,
ssh_key_path: PathBuf,
temp_dir: TempDir,
directory: PathBuf,
},
Expand All @@ -110,16 +111,22 @@ impl ConfigDir {
&url,
branch.as_ref().map(|x| &**x),
temp_dir.path(),
Some(ssh_key_path),
Some(&ssh_key_path),
)?;

let head_sha = git::get_head_sha(&git_repo)?;

let directory = match git_repo.workdir() {
Some(workdir) => workdir.join(internal_path),
None => bail!("No working directory found for git repository"),
};
let url = url.clone();
let ssh_key_path = ssh_key_path.to_owned();

Ok(ConfigDir::Git {
git_repo,
url,
head_sha,
ssh_key_path,
temp_dir,
directory,
})
Expand All @@ -139,21 +146,65 @@ impl ConfigDir {
config_dir
}

pub fn extend(&self, branch: &str) -> Result<ConfigDir, Error> {
match self {
ConfigDir::Git {
url, ssh_key_path, ..
} => ConfigDir::new(
ConfigUrl::Git {
url: url.clone(),
branch: Some(branch.to_owned()),
internal_path: PathBuf::new(),
},
ssh_key_path,
),
ConfigDir::File { .. } => Err(format_err!("Can not extend file config")),
}
}

pub fn directory(&self) -> &Path {
match *self {
ConfigDir::File { ref directory, .. } => directory,
ConfigDir::Git { ref directory, .. } => directory,
}
}

// TODO: Implement being able to re-checkout a git repo
pub fn refresh(&self) -> &Self {
match *self {
ConfigDir::File { .. } => {}
ConfigDir::Git { .. } => {}
pub fn refresh(&self, remote: Option<&str>, target: Option<&str>) -> Option<String> {
match self {
ConfigDir::File { .. } => None,
ConfigDir::Git {
directory,
url,
ssh_key_path,
..
} => {
let git_repo = match git::build_repo(directory.to_str().unwrap()) {
Ok(repo) => repo,
Err(e) => {
error!(
"Error: {} \n Unable to find the git repo: {}",
e,
directory.to_str().unwrap()
);
return None;
}
};
match git::reset(
&git_repo,
remote.unwrap_or("origin"),
Some(ssh_key_path),
Some(url),
target,
false,
) {
Ok(sha) => Some(sha),
Err(e) => {
error!("Error refreshing to {:?} {:?}", target, e);
None
}
}
}
}

self
}

pub fn find(&self, filter: Regex) -> Vec<Environment> {
Expand Down Expand Up @@ -192,7 +243,7 @@ impl ConfigDir {
find_file_paths(self.directory(), filter)
.filter_map(|p| File::open(p).ok())
.filter_map(|f| serde_json::from_reader(f).ok())
.filter_map(|c: Config| c.as_environment()),
.filter_map(|c: Config| c.into_environment()),
)
}

Expand All @@ -208,7 +259,7 @@ impl ConfigDir {
File::open(&path)
.ok()
.and_then(|f| serde_json::from_reader(f).ok())
.and_then(|c: Config| c.as_environment_type())
.and_then(|c: Config| c.into_environment_type())
.and_then(|mut e| {
e.environment_type = env_type;
Some(e)
Expand All @@ -226,30 +277,30 @@ enum Config {
}

impl Config {
fn as_environment(self) -> Option<Environment> {
fn into_environment(self) -> Option<Environment> {
match self {
Config::Environment(e) => Some(e),
_ => None,
}
}

fn as_environment_type(self) -> Option<EnvironmentType> {
fn into_environment_type(self) -> Option<EnvironmentType> {
match self {
Config::EnvironmentType(e) => Some(e),
_ => None,
}
}
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct Environment {
pub environment: String,
pub environment_type: Option<String>,
pub config_data: Value,
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
struct EnvironmentType {
environment_type: String,
Expand Down
Loading

0 comments on commit 4bed3ed

Please sign in to comment.