git-add-coauthor
This commit is contained in:
parent
7b65f8e7b7
commit
8e46759b0a
|
|
@ -0,0 +1,24 @@
|
|||
use git_mob::{Author, get_available_coauthors, write_coauthors_file} ;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(StructOpt,Debug)]
|
||||
struct Opt {
|
||||
/// Co-author initials
|
||||
initials: String,
|
||||
/// The name of the co-author, in quotes, e.g. "Foo Bar"
|
||||
name: String,
|
||||
/// The email of the co-author
|
||||
email: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let opt = Opt::from_args();
|
||||
let mut authors = get_available_coauthors();
|
||||
let new_author = Author {
|
||||
name: opt.name,
|
||||
email: opt.email,
|
||||
};
|
||||
authors.insert(opt.initials, new_author);
|
||||
|
||||
write_coauthors_file(authors);
|
||||
}
|
||||
24
src/lib.rs
24
src/lib.rs
|
|
@ -1,4 +1,5 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use git2::{Config, Repository};
|
||||
use std::fmt;
|
||||
use std::collections::BTreeMap;
|
||||
|
|
@ -12,8 +13,8 @@ use std::process;
|
|||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct Author {
|
||||
name: String,
|
||||
email: String,
|
||||
pub name: String,
|
||||
pub email: String,
|
||||
}
|
||||
|
||||
impl fmt::Display for Author {
|
||||
|
|
@ -44,7 +45,7 @@ pub fn get_available_coauthors() -> BTreeMap<String, Author> {
|
|||
}
|
||||
|
||||
fn parse_coauthors_file() -> Result<BTreeMap<String, Author>, Box<dyn Error>> {
|
||||
let coauthors_path = home_dir().unwrap().join(".git-coauthors");
|
||||
let coauthors_path = coauthors_file_path();
|
||||
let coauthors_file = File::open(coauthors_path)?;
|
||||
let reader = BufReader::new(coauthors_file);
|
||||
|
||||
|
|
@ -69,3 +70,20 @@ pub fn with_repo_or_exit<F: FnOnce(Repository)>(f: F) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_coauthors_file(authors: BTreeMap<String, Author>) {
|
||||
let mut wrapper_tree = BTreeMap::new();
|
||||
wrapper_tree.insert("coauthors", authors);
|
||||
let json_data = serde_json::to_string_pretty(&wrapper_tree).unwrap();
|
||||
match fs::write(coauthors_file_path(), json_data) {
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
eprintln!("Error writing git-coauthors file: {:?}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn coauthors_file_path() -> std::path::PathBuf {
|
||||
home_dir().unwrap().join(".git-coauthors")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue