1use super::consts::{BBCLIENT_CACHE_ENV, BEDBASE_API_ENV};
2use biocrs::models::Resource;
3use dirs::home_dir;
4use std::env;
5use std::fs::create_dir_all;
6use std::path::PathBuf;
7use tabled::{Table, Tabled};
8
9use shellexpand;
10
11#[derive(Tabled)]
12pub struct ResourcePrint {
13 id: String,
14 path: String,
15}
16
17pub fn get_abs_path(path: Option<PathBuf>, create_folder: Option<bool>) -> PathBuf {
25 let raw_path = path.unwrap_or_else(get_default_cache_folder);
26
27 let raw_str = raw_path.to_string_lossy().into_owned();
28
29 let expanded_str = shellexpand::env(&raw_str)
30 .unwrap_or_else(|_| raw_str.clone().into()) .into_owned(); let abs_path = PathBuf::from(expanded_str);
34
35 if create_folder.unwrap_or(true) {
36 create_dir_all(&abs_path).expect("Failed to create directory");
37 }
38
39 abs_path
40}
41
42pub fn get_default_cache_folder() -> PathBuf {
47 if let Ok(val) = env::var(BBCLIENT_CACHE_ENV) {
48 PathBuf::from(val)
49 } else {
50 let home = env::var("HOME")
51 .or_else(|_| {
52 home_dir()
53 .map(|p| p.to_string_lossy().into_owned())
54 .ok_or(std::env::VarError::NotPresent)
55 })
56 .unwrap_or_else(|_| "/tmp".to_string());
57
58 let mut path = PathBuf::from(home);
59 path.push(".bbcache/");
60 path
61 }
62}
63
64pub fn get_bedbase_api() -> String {
69 env::var(BEDBASE_API_ENV).unwrap_or_else(|_| "https://api.bedbase.org".to_string())
70}
71
72pub fn print_resources(resources: Vec<Resource>) {
73 let mut resource_print: Vec<ResourcePrint> = Vec::new();
74
75 for resource in resources {
76 resource_print.push(ResourcePrint {
77 id: resource.rname,
78 path: resource.rpath,
79 })
80 }
81
82 let table = Table::new(resource_print);
83
84 println!("{}", table);
85}