Skip to content

Commit

Permalink
b: add view
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Jun 15, 2021
1 parent 66dfc79 commit 6588257
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
25 changes: 20 additions & 5 deletions b/src/main.rs
@@ -1,4 +1,4 @@
use b::use_case;
use b::{use_case, BId};
use std::{io, path::PathBuf};
use structopt::{clap::Shell, StructOpt};

Expand Down Expand Up @@ -36,23 +36,38 @@ enum Subcommand {
)]
template: PathBuf,
},
#[structopt(name = "view", about = "Views the b file")]
View {
#[structopt(long = "data-dir")]
data_dir: PathBuf,
#[structopt(name = "BID")]
id: BId,
},
}

fn main() {
fn main() -> anyhow::Result<()> {
let opt = Opt::from_args();
match opt.subcommand {
Subcommand::Completion { shell } => {
Opt::clap().gen_completions_to("b", shell, &mut io::stdout())
Opt::clap().gen_completions_to("b", shell, &mut io::stdout());
Ok(())
}
Subcommand::List {
data_dir,
json,
query,
time_zone_offset,
} => use_case::list(data_dir, json, query, time_zone_offset, &mut io::stdout()),
} => {
use_case::list(data_dir, json, query, time_zone_offset, &mut io::stdout());
Ok(())
}
Subcommand::New {
data_file,
template,
} => use_case::new(data_file, template),
} => {
use_case::new(data_file, template);
Ok(())
}
Subcommand::View { data_dir, id } => use_case::view(data_dir, id, &mut io::stdout()),
}
}
2 changes: 2 additions & 0 deletions b/src/use_case.rs
@@ -1,5 +1,7 @@
mod list;
mod new;
mod view;

pub use list::list;
pub use new::new;
pub use view::view;
36 changes: 36 additions & 0 deletions b/src/use_case/view.rs
@@ -0,0 +1,36 @@
use crate::brepository::BRepository;
use crate::{BId, TimeZoneOffset};
use anyhow::Context;
use std::{fs, io, path::PathBuf};

pub fn view(data_dir: PathBuf, id: BId, writer: &mut impl io::Write) -> anyhow::Result<()> {
let time_zone_offset = TimeZoneOffset::default(); // TODO
let repository = BRepository::new(data_dir, time_zone_offset);
let meta = repository.find_meta(id)?;
let meta = meta.with_context(|| "b not found")?;
let content = fs::read_to_string(repository.to_content_path_buf(&meta.id).as_path())?;
write!(writer, "{}", content)?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use std::{fs, str::FromStr};
use tempfile::tempdir;

#[test]
fn test() {
let dir = tempdir().unwrap();
let dir20210203 = dir.path().join("flow").join("2021").join("02").join("03");
fs::create_dir_all(dir20210203.as_path()).unwrap();
let meta = dir20210203.join("20210203T000000Z.json");
fs::write(meta.as_path(), "{}").unwrap();
let content = meta.with_extension("md");
fs::write(content.as_path(), "Hello, world!").unwrap();
let bid = BId::from_str("20210203T000000Z").unwrap();
let mut output = vec![];
view(dir.path().to_path_buf(), bid, &mut output).unwrap();
assert_eq!(output, b"Hello, world!");
}
}

0 comments on commit 6588257

Please sign in to comment.