-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Eyal Kalderon <[email protected]>
- Loading branch information
Showing
9 changed files
with
141 additions
and
90 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,41 @@ | ||
use std::fs; | ||
use std::io::prelude::*; | ||
use crate::BoxResult; | ||
use std::path::Path; | ||
use crate::copirate::CoPirate; | ||
use std::fs::OpenOptions; | ||
|
||
const ACTIVE_COPIRATES_PATH: &str = ".git/.git-rmob-template"; | ||
|
||
#[derive(Debug)] | ||
pub struct ActiveCoPirates { | ||
file: fs::File, | ||
} | ||
|
||
impl ActiveCoPirates { | ||
pub fn create_empty(repo_dir: &Path) -> BoxResult<ActiveCoPirates> { | ||
let active_copirates_path = repo_dir.join(ACTIVE_COPIRATES_PATH); | ||
fs::write(&active_copirates_path, "")?; | ||
|
||
let file = OpenOptions::new() | ||
.append(true) | ||
.open(active_copirates_path)?; | ||
|
||
Ok(ActiveCoPirates { file }) | ||
} | ||
|
||
pub fn save(mut self, copirates: &[&CoPirate]) -> BoxResult<()> { | ||
for pirate in copirates { | ||
writeln!(self.file, "Co-authored-by: {} <{}>", pirate.name, pirate.email)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn get(repo_dir: &Path) -> BoxResult<String> { | ||
let active_copirates = fs::read_to_string(repo_dir.join(ACTIVE_COPIRATES_PATH))?; | ||
|
||
Ok(active_copirates) | ||
} | ||
} | ||
|
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
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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
use std::process; | ||
|
||
|
||
fn main() { | ||
if let Err(e) = rmob::run() { | ||
eprintln!("{}", e); | ||
process::exit(1); | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -1,55 +1,29 @@ | ||
//! start sub-command | ||
extern crate dirs; | ||
use crate::{BoxResult, COPIRATES_FILE, ACTIVE_COPIRATES_FILE}; | ||
use crate::{BoxResult, COPIRATES_PATH}; | ||
|
||
use std::fs; | ||
use std::collections::{HashSet}; | ||
use std::fs::OpenOptions; | ||
use std::io::prelude::*; | ||
use crate::copirate::CoPirates; | ||
use crate::copirate::{CoPirates}; | ||
use std::path::Path; | ||
use crate::active_copirate::ActiveCoPirates; | ||
|
||
pub fn sail(copirates: &[String]) -> BoxResult { | ||
pub fn sail(copirates: &[String], repo_dir: &Path) -> BoxResult<()> { | ||
let ship = dirs::home_dir().ok_or("Could not find yer ship oy!")?; | ||
let raw_copirates = fs::read_to_string(ship.join(COPIRATES_FILE))?; | ||
let existing_copirates: CoPirates = serde_json::from_str(&raw_copirates[..])?; | ||
let existing_copirates = CoPirates::open(&ship.join(COPIRATES_PATH))?; | ||
|
||
fail_if_pirate_not_present(copirates, &existing_copirates)?; | ||
|
||
empty_copirates_file()?; | ||
|
||
save_copirates(copirates, existing_copirates)?; | ||
save_copirates(copirates, existing_copirates, repo_dir)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn save_copirates(copirates: &[String], existing_copirates: CoPirates) -> BoxResult { | ||
let mut file = OpenOptions::new() | ||
.append(true) | ||
.open(ACTIVE_COPIRATES_FILE) | ||
.unwrap(); | ||
for pirate in copirates { | ||
let existing_pirate = existing_copirates.copirates.get(pirate).ok_or("Wait what Sally it was right there?")?; | ||
writeln!(file, "Co-authored-by: {} <{}>", existing_pirate.name, existing_pirate.email)?; | ||
} | ||
|
||
println!("{:?}", copirates); | ||
|
||
Ok(()) | ||
} | ||
fn save_copirates(copirates: &[String], existing_copirates: CoPirates, repo_dir: &Path) -> BoxResult<()> { | ||
|
||
fn empty_copirates_file() -> BoxResult { | ||
fs::write(ACTIVE_COPIRATES_FILE, "")?; | ||
let copirates: Vec<_> = copirates.iter().map(| initial| existing_copirates.get(initial)).collect::<Result<_, _>>()?; | ||
|
||
Ok(()) | ||
} | ||
let active_copirates = ActiveCoPirates::create_empty(repo_dir)?; | ||
active_copirates.save(&copirates[..])?; | ||
|
||
fn fail_if_pirate_not_present(copirates: &[String], existing_copirates: &CoPirates) -> BoxResult { | ||
let existing_copirates: HashSet<&String> = existing_copirates.copirates.keys().collect(); | ||
let copirates: HashSet<&String> = copirates.into_iter().collect(); | ||
if !copirates.is_subset(&existing_copirates) { | ||
return Err(Box::from("We didn't recognize this pirate's initials. Please add to your ~/.git-copirates file!")); | ||
} | ||
println!("Sail away!"); | ||
|
||
Ok(()) | ||
} |
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