hyper_scripter/util/
init_repo.rs1use super::main_util;
2use crate::args::RootArgs;
3use crate::config::{Config, Recent};
4use crate::error::{Contextable, Error, Result};
5use crate::path;
6use crate::script_repo::{DBEnv, RecentFilter, ScriptRepo};
7use futures::try_join;
8use fxhash::FxHashSet as HashSet;
9use hyper_scripter_historian::Historian;
10
11pub async fn init_env(mut need_journal: bool) -> Result<(DBEnv, bool)> {
13 async fn init_historian() -> Result<Historian> {
14 let h = Historian::new(path::get_home().to_owned()).await?;
15 Ok(h)
16 }
17 let ((pool, init), historian) =
18 try_join!(crate::db::get_pool(&mut need_journal), init_historian())?;
19 Ok((DBEnv::new(pool, historian, need_journal), init))
20}
21
22pub async fn init_repo(args: RootArgs, need_journal: bool) -> Result<ScriptRepo> {
24 let RootArgs {
25 no_trace,
26 humble,
27 archaeology,
28 select,
29 toggle,
30 recent,
31 timeless,
32 ..
33 } = args;
34
35 let conf = Config::get();
36
37 let recent = if timeless {
38 Recent::Timeless
39 } else if let Some(recent) = recent {
40 Recent::Days(recent)
41 } else {
42 conf.recent
43 };
44
45 let tag_group = {
47 let mut toggle: HashSet<_> = toggle.into_iter().collect();
48 let mut tag_group = conf.get_tag_selector_group(&mut toggle);
49 if let Some(name) = toggle.into_iter().next() {
50 return Err(Error::TagSelectorNotFound(name));
51 }
52 for select in select.into_iter() {
53 tag_group.push(select);
54 }
55 tag_group
56 };
57
58 let (env, init) = init_env(need_journal).await?;
59 let mut repo = ScriptRepo::new(
60 RecentFilter {
61 recent,
62 archaeology,
63 },
64 env,
65 &tag_group,
66 )
67 .await
68 .context("載入腳本倉庫失敗")?;
69 if no_trace {
70 repo.no_trace();
71 } else if humble {
72 repo.humble();
73 }
74
75 if init {
76 log::info!("初次使用,載入好用工具和預執行腳本");
77 main_util::load_utils(&mut repo, Some(&tag_group)).await?;
78 main_util::prepare_pre_run(None)?;
79 main_util::load_templates()?;
80 }
81
82 Ok(repo)
83}