Skip to content

Commit

Permalink
refactor: use directories (#39)
Browse files Browse the repository at this point in the history
* done

* doc

* fix
  • Loading branch information
shixinhuang99 authored Dec 29, 2023
1 parent 91d8fe8 commit 7db982a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 43 deletions.
95 changes: 82 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ camino = "1.1.6"
chrono = "0.4.24"
clap = { version = "4.1.13", features = ["derive"] }
dircpy = { version = "0.3.15", default-features = false }
directories = "5.0.1"
flate2 = "1.0.25"
fs-err = "2.9.0"
home = "0.5.4"
owo-colors = "3.5.0"
regex = "1.7.3"
remove_dir_all = "0.8.2"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Commands:
Options:
--debug Use debug output
--token <TOKEN> Specify the GitHub personal access token(classic)
--root-dir Display root dir of scafalra
--proj-dir Display of scafalra's data storage location
-h, --help Print help
-V, --version Print version
```
Expand Down
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub struct Cli {
#[arg(long, global = true)]
pub token: Option<String>,

/// Display root dir of scafalra
/// Display of scafalra's data storage location
#[arg(long)]
pub root_dir: bool,
pub proj_dir: bool,
}

#[derive(Subcommand)]
Expand Down
18 changes: 11 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ fn main() {
}

fn try_main() -> Result<()> {
let home_dir = home::home_dir()
.ok_or(anyhow::anyhow!("Impossible to get your home directory"))?
.into_utf8_path_buf()?;

let cli = Cli::parse();

if cli.debug || env::var("DEBUG_LOG").is_ok() {
trun_on_debug();
}

let mut scafalra = Scafalra::new(&home_dir, None, cli.token.as_deref())?;
let proj_dir = directories::ProjectDirs::from("", "", "scafalra")
.ok_or(anyhow::anyhow!(
"No valid home directory path could be retrieved from your \
operating system"
))?
.config_dir()
.into_utf8_path_buf()?;

let mut scafalra = Scafalra::new(proj_dir, None, cli.token.as_deref())?;

if cli.root_dir {
println!("{}", scafalra.root_dir);
if cli.proj_dir {
println!("{}", scafalra.proj_dir);
return Ok(());
}

Expand Down
34 changes: 15 additions & 19 deletions src/scafalra.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;

use anyhow::Result;
use camino::{Utf8Component, Utf8Path, Utf8PathBuf};
use camino::{Utf8Component, Utf8PathBuf};
use fs_err as fs;

#[cfg(all(unix, feature = "self_update"))]
Expand All @@ -24,7 +24,7 @@ use crate::{
};

pub struct Scafalra {
pub root_dir: Utf8PathBuf,
pub proj_dir: Utf8PathBuf,
cache_dir: Utf8PathBuf,
#[cfg(feature = "self_update")]
update_dir: Utf8PathBuf,
Expand All @@ -34,35 +34,33 @@ pub struct Scafalra {
}

impl Scafalra {
const ROOT_DIR_NAME: &'static str = ".scafalra";
const CACHE_DIR_NAME: &'static str = "cache";
#[cfg(feature = "self_update")]
const UPDATE_DIR_NAME: &'static str = "update";

pub fn new(
home_dir: &Utf8Path,
proj_dir: Utf8PathBuf,
endpoint: Option<&str>,
token: Option<&str>,
) -> Result<Self> {
let root_dir = home_dir.join(Self::ROOT_DIR_NAME);
let cache_dir = root_dir.join(Self::CACHE_DIR_NAME);
let cache_dir = proj_dir.join(Self::CACHE_DIR_NAME);
#[cfg(feature = "self_update")]
let update_dir = root_dir.join(Self::UPDATE_DIR_NAME);
let update_dir = proj_dir.join(Self::UPDATE_DIR_NAME);

if !cache_dir.exists() {
fs::create_dir_all(&cache_dir)?;
}

let config = Config::new(&root_dir)?;
let store = Store::new(&root_dir)?;
let config = Config::new(&proj_dir)?;
let store = Store::new(&proj_dir)?;
let github_api = GitHubApi::new(endpoint);

if let Some(token) = token.or_else(|| config.token()) {
github_api.set_token(token);
}

Ok(Self {
root_dir,
proj_dir,
cache_dir,
config,
store,
Expand Down Expand Up @@ -295,7 +293,7 @@ impl Scafalra {
debug!("args: {:#?}", args);

if !args.keep_data {
remove_dir_all::remove_dir_all(&self.root_dir)?;
remove_dir_all::remove_dir_all(&self.proj_dir)?;
}

if cfg!(not(test)) {
Expand Down Expand Up @@ -335,21 +333,19 @@ mod tests {
init_content: bool,
) -> Result<(Scafalra, TempDir)> {
let temp_dir = tempdir()?;
let temp_dir_path = temp_dir.path().into_utf8_path_buf()?;
let proj_dir = temp_dir.path().into_utf8_path_buf()?.join("scafalra");

if init_content {
let root_dir = temp_dir_path.join(Scafalra::ROOT_DIR_NAME);
let cache_dir = root_dir.join(Scafalra::CACHE_DIR_NAME);
let store_file = root_dir.join(Store::FILE_NAME);
let cache_dir = proj_dir.join(Scafalra::CACHE_DIR_NAME);
let store_file = proj_dir.join(Store::FILE_NAME);
let foo_dir = cache_dir.join("foo");
let bar_dir = foo_dir.join("bar");
fs::create_dir_all(&bar_dir)?;
fs::write(bar_dir.join("baz.txt"), "")?;
fs::write(store_file, mock_store_json([("bar", bar_dir)]))?;
}

let scafalra =
Scafalra::new(&temp_dir_path, Some(endpoint), Some("token"))?;
let scafalra = Scafalra::new(proj_dir, Some(endpoint), Some("token"))?;

Ok((scafalra, temp_dir))
}
Expand Down Expand Up @@ -643,7 +639,7 @@ mod tests {

scafalra.uninstall(UninstallArgs { keep_data: false })?;

assert!(!scafalra.root_dir.exists());
assert!(!scafalra.proj_dir.exists());

Ok(())
}
Expand All @@ -655,7 +651,7 @@ mod tests {

scafalra.uninstall(UninstallArgs { keep_data: true })?;

assert!(scafalra.root_dir.exists());
assert!(scafalra.proj_dir.exists());

Ok(())
}
Expand Down

0 comments on commit 7db982a

Please sign in to comment.