Skip to content

Commit 6c7bff4

Browse files
committed
index the contact after add a contact
1 parent 5ee1d18 commit 6c7bff4

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/cli.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ fn get_envvar(key: &str) -> Option<String> {
2626
}
2727
}
2828

29+
pub fn index_contact(index_path: &path::Path, contact: &utils::Contact) -> Result<()> {
30+
let mut index_fp = fs::OpenOptions::new()
31+
.append(true)
32+
.write(true)
33+
.open(&index_path)?;
34+
35+
let index_entry = utils::index_item_from_contact(contact)?;
36+
index_fp.write_all(index_entry.as_bytes())?;
37+
Ok(())
38+
}
39+
2940
pub fn build_index(outfile: &path::Path, dir: &path::Path) -> Result<()> {
3041
if !dir.is_dir() {
3142
return Err(anyhow!("MATES_DIR must be a directory."));

src/main.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
extern crate anyhow;
33
use anyhow::Result;
44
use clap::{Arg, Command};
5-
use std::fs;
65
use std::io;
7-
use std::io::{Read, Write};
6+
use std::io::Read;
87

98
use mates::cli;
109
use mates::utils;
@@ -90,6 +89,8 @@ fn main() -> Result<()> {
9089
&config.vdir_path,
9190
);
9291
contact.write_create()?;
92+
93+
cli::index_contact(&config.index_path, &contact)?
9394
}
9495
Some(("add-email", _)) => {
9596
let stdin = io::stdin();
@@ -98,13 +99,7 @@ fn main() -> Result<()> {
9899
let contact = utils::add_contact_from_email(&config.vdir_path, &email[..])?;
99100
println!("{}", contact.path.display());
100101

101-
let mut index_fp = fs::OpenOptions::new()
102-
.append(true)
103-
.write(true)
104-
.open(&config.index_path)?;
105-
106-
let index_entry = utils::index_item_from_contact(&contact)?;
107-
index_fp.write_all(index_entry.as_bytes())?;
102+
cli::index_contact(&config.index_path, &contact)?
108103
}
109104
Some(("edit", args)) => {
110105
if let Some(value) = args.value_of("file-or-query") {

0 commit comments

Comments
 (0)