Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: action and atoms for git.clone #155

Merged
merged 3 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/_cicd-intermediates
/Cargo.lock
/.build
4 changes: 4 additions & 0 deletions examples/git/clone.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
actions:
- action: git.clone
repository: https://github.com/comtrya/comtrya
directory: ./.build/comtrya
27 changes: 27 additions & 0 deletions src/actions/git/clone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::contexts::Contexts;
use crate::steps::Step;
use crate::{actions::Action, manifests::Manifest};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct GitClone {
pub repository: String,
pub reference: Option<String>,
pub directory: String,
}

impl Action for GitClone {
fn plan(&self, _: &Manifest, _: &Contexts) -> Vec<Step> {
vec![Step {
atom: Box::new(crate::atoms::git::Clone {
repository: self.repository.clone(),
reference: self.reference.clone(),
directory: PathBuf::from(self.directory.clone()),
..Default::default()
}),
initializers: vec![],
finalizers: vec![],
}]
}
}
2 changes: 2 additions & 0 deletions src/actions/git/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod clone;
pub use clone::GitClone;
6 changes: 6 additions & 0 deletions src/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod command;
mod directory;
mod file;
mod git;
mod package;

use crate::contexts::{to_koto, Contexts};
Expand All @@ -11,6 +12,7 @@ use directory::{DirectoryCopy, DirectoryCreate};
use file::copy::FileCopy;
use file::download::FileDownload;
use file::link::FileLink;
use git::GitClone;
use koto::{Koto, KotoSettings};
use package::install::PackageInstall;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -123,6 +125,9 @@ pub enum Actions {
#[serde(alias = "file.link")]
FileLink(ConditionalVariantAction<FileLink>),

#[serde(alias = "git.clone")]
GitClone(ConditionalVariantAction<GitClone>),

#[serde(alias = "package.install", alias = "package.installed")]
PackageInstall(ConditionalVariantAction<PackageInstall>),
}
Expand All @@ -136,6 +141,7 @@ impl Actions {
Actions::FileCopy(a) => a,
Actions::FileDownload(a) => a,
Actions::FileLink(a) => a,
Actions::GitClone(a) => a,
Actions::PackageInstall(a) => a,
}
}
Expand Down
100 changes: 100 additions & 0 deletions src/atoms/git/clone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use std::path::PathBuf;

use super::super::Atom;
use gitsync::GitSync;
use tracing::instrument;

#[derive(Default)]
pub struct Clone {
pub repository: String,
pub directory: PathBuf,
pub reference: Option<String>,
}

impl std::fmt::Display for Clone {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"GitClone {}#{} to {:?}",
self.repository,
self.reference.clone().unwrap_or(String::from("main")),
self.directory,
)
}
}

impl Atom for Clone {
#[instrument(name = "git.clone.plan", level = "info", skip(self))]
fn plan(&self) -> bool {
!self.directory.exists()
}

#[instrument(name = "git.clone.execute", level = "info", skip(self))]
fn execute(&mut self) -> anyhow::Result<()> {
let git_sync = GitSync {
repo: self.repository.clone(),
branch: self.reference.clone(),
dir: self.directory.clone(),
..Default::default()
};

// we may add .sync as another atom
git_sync
.bootstrap()
.map_err(|err| anyhow::anyhow!("{:?}", err))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_can_plan() {
let temp_dir = match tempfile::tempdir() {
std::result::Result::Ok(dir) => dir,
std::result::Result::Err(_) => {
assert_eq!(false, true);
return;
}
};

let git_clone = Clone {
repository: String::from("https://github.com/comtrya/comtrya"),
directory: temp_dir.path().to_path_buf(),
..Default::default()
};

assert_eq!(false, git_clone.plan());

let git_clone = Clone {
repository: String::from("https://github.com/comtrya/comtrya"),
directory: temp_dir.path().join("nonexistent").to_path_buf(),
..Default::default()
};

assert_eq!(true, git_clone.plan());
}

#[test]
fn it_can_execute() {
let temp_dir = match tempfile::tempdir() {
std::result::Result::Ok(dir) => dir,
std::result::Result::Err(_) => {
assert_eq!(false, true);
return;
}
};

let mut git_clone = Clone {
repository: String::from("https://github.com/comtrya/comtrya"),
directory: temp_dir.path().join("clone").to_path_buf(),
..Default::default()
};

match git_clone.execute() {
Ok(_) => (),
Err(_) => assert_eq!(false, true),
}
}
}
2 changes: 2 additions & 0 deletions src/atoms/git/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod clone;
pub use clone::Clone;
1 change: 1 addition & 0 deletions src/atoms/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod command;
pub mod directory;
pub mod file;
pub mod git;
pub mod http;

pub trait Atom: std::fmt::Display {
Expand Down