Skip to content

Commit

Permalink
feat: use fs-err (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
shixinhuang99 committed Oct 26, 2023
1 parent 75b14d4 commit 8d7cba3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 166 deletions.
120 changes: 7 additions & 113 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ anyhow = "1.0.70"
camino = "1.1.6"
chrono = "0.4.24"
clap = { version = "4.1.13", features = ["derive"] }
dircpy = "0.3.15"
dircpy = { version = "0.3.15", default-features = false }
flate2 = "1.0.25"
fs-err = "2.9.0"
home = "0.5.4"
owo-colors = "3.5.0"
regex = "1.7.3"
Expand Down
3 changes: 0 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::path::PathBuf;

use camino::Utf8PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ScafalraError {
#[error("Error accessing specified path: `{}`", .0)]
IOError(Utf8PathBuf),
#[error("No GitHub personal access token configured")]
NoToken,
#[error("Call to GitHub api error")]
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod store;
mod toml_content;
mod utils;

use std::env;

use anyhow::Result;
use camino::Utf8PathBuf;
use clap::Parser;
Expand All @@ -34,7 +36,7 @@ fn try_main() -> Result<()> {

let cli = Cli::parse();

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

Expand Down
3 changes: 2 additions & 1 deletion src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{fs, io, sync::OnceLock};
use std::{io, sync::OnceLock};

use anyhow::{anyhow, Result};
use camino::{Utf8Path, Utf8PathBuf};
use flate2::read::GzDecoder;
use fs_err as fs;
use regex::Regex;
use remove_dir_all::remove_dir_all;

Expand Down
20 changes: 8 additions & 12 deletions src/scafalra.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{env, fs};
use std::env;

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

use crate::{
cli::{AddArgs, CreateArgs, ListArgs, MvArgs, RemoveArgs, TokenArgs},
Expand Down Expand Up @@ -31,8 +32,7 @@ impl Scafalra {
let cache_dir = root_dir.join("cache");

if !cache_dir.exists() {
fs::create_dir_all(&cache_dir)
.context(ScafalraError::IOError(cache_dir.clone()))?;
fs::create_dir_all(&cache_dir)?;
}

let config = Config::new(&root_dir)?;
Expand Down Expand Up @@ -100,7 +100,7 @@ impl Scafalra {
let mut scaffold_path =
repo.cache(&api_result.tarball_url, &self.cache_dir)?;

debug!("{}", scaffold_path);
debug!("scaffold_path: {}", scaffold_path);

if let Some(ref subdir) = repo.subdir {
subdir
Expand All @@ -110,7 +110,7 @@ impl Scafalra {
scaffold_path.push(c);
});

debug!("{}", scaffold_path);
debug!("scaffold_path: {}", scaffold_path);

if let Some(name) = scaffold_path.file_name() {
scaffold_name = name.to_string();
Expand All @@ -126,10 +126,7 @@ impl Scafalra {
}

if args.depth == 1 {
for entry in scaffold_path
.read_dir_utf8()
.context(ScafalraError::IOError(scaffold_path.clone()))?
{
for entry in scaffold_path.read_dir_utf8()? {
let entry = entry?;
let file_type = entry.file_type()?;
let file_name = entry.file_name();
Expand Down Expand Up @@ -181,8 +178,7 @@ impl Scafalra {
anyhow::bail!("`{}` is already exists", dst);
}

dircpy::copy_dir(&scaffold.local, &dst)
.context(ScafalraError::IOError(dst.clone()))?;
dircpy::copy_dir(&scaffold.local, &dst)?;

println!("Created in `{}`", dst);

Expand Down
51 changes: 16 additions & 35 deletions src/toml_content.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
use std::fs;

use anyhow::{Context, Result};
use anyhow::Result;
use camino::Utf8Path;
use fs_err as fs;
use serde::{de::DeserializeOwned, Serialize};

use crate::error::ScafalraError;

fn load_or_default<T>(file: &Utf8Path, defalut: T) -> Result<T>
where
T: TomlContent,
{
let content: T = {
if file.exists() {
toml::from_str(&fs::read_to_string(file)?)?
} else {
fs::File::create(file)?;
defalut
}
};

Ok(content)
}

fn save_content<T>(file: &Utf8Path, that: &T) -> Result<()>
where
T: TomlContent,
{
let content = toml::to_string_pretty(that)?;
fs::write(file, content)?;

Ok(())
}

pub trait TomlContent: DeserializeOwned + Serialize + Default {
fn load(file: &Utf8Path) -> Result<Self> {
load_or_default(file, Self::default())
.context(ScafalraError::IOError(file.to_path_buf()))
let content: Self = {
if file.exists() {
toml::from_str(&fs::read_to_string(file)?)?
} else {
fs::File::create(file)?;
Self::default()
}
};

Ok(content)
}

fn save(&self, file: &Utf8Path) -> Result<()> {
save_content(file, self)
.context(ScafalraError::IOError(file.to_path_buf()))
let content = toml::to_string_pretty(self)?;
fs::write(file, content)?;

Ok(())
}
}

Expand Down

0 comments on commit 8d7cba3

Please sign in to comment.