implement lookup function to find prjfmt.toml (#52)

This commit is contained in:
Andika Demas Riyandi 2021-02-15 20:20:05 +07:00 committed by GitHub
parent b8c28e6b1d
commit c2641b429a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 15 deletions

4
.gitignore vendored
View File

@ -1 +1,3 @@
/target
target
result
result-*

View File

@ -1,18 +1,19 @@
use super::Cli;
use super::lookup_prjfmt_toml;
use crate::engine::run_prjfmt;
use crate::CLOG;
use anyhow::anyhow;
use std::env;
use std::{env, path::PathBuf};
use std::fs;
use std::path::Path;
pub fn format_cmd(cli: Cli) -> anyhow::Result<()> {
let cwd = match cli.files {
Some(path) => path,
None => env::current_dir()?,
pub fn format_cmd(path: Option<PathBuf>) -> anyhow::Result<()> {
let cwd = env::current_dir()?;
let cfg_dir = match path {
Some(p) => p,
None => lookup_prjfmt_toml(cwd)?,
};
let prjfmt_toml = cwd.join("prjfmt.toml");
let prjfmt_toml = cfg_dir.join("prjfmt.toml");
let xdg_cache_dir = match env::var("XDG_CACHE_DIR") {
Ok(path) => path,
Err(err) => {
@ -35,12 +36,12 @@ pub fn format_cmd(cli: Cli) -> anyhow::Result<()> {
CLOG.debug(&format!(
"Found {} at {}",
prjfmt_toml.display(),
cwd.display()
cfg_dir.display()
));
CLOG.debug(&format!("Change current directory into: {}", cwd.display()));
CLOG.debug(&format!("Change current directory into: {}", cfg_dir.display()));
let cache_dir = Path::new(&xdg_cache_dir).join("prjfmt/eval-cache");
fs::create_dir_all(&cache_dir)?;
run_prjfmt(cwd, cache_dir)?;
run_prjfmt(cfg_dir, cache_dir)?;
} else {
CLOG.error(
"file prjfmt.toml couldn't be found. Run `--init` to generate the default setting",

View File

@ -9,6 +9,7 @@ use self::init::init_cmd;
use super::customlog::LogLevel;
use std::path::PathBuf;
use structopt::StructOpt;
use anyhow::{anyhow, Result};
#[derive(Debug, StructOpt)]
/// The various kinds of commands that `prjfmt` can execute.
@ -19,6 +20,12 @@ pub enum Command {
/// path to file or folder
path: Option<PathBuf>,
},
#[structopt(name = "--config")]
/// Specify prjfmt.toml file
PrjFmt {
/// path to file or folder
path: PathBuf,
},
}
/// ✨ format all your language!
@ -39,17 +46,33 @@ pub struct Cli {
#[structopt(long = "log-level", default_value = "debug")]
/// The maximum level of messages that should be logged by prjfmt. [possible values: info, warn, error]
pub log_level: LogLevel,
/// Files to format
pub files: Option<PathBuf>,
}
/// Run a command with the given logger
pub fn run_cli(cli: Cli) -> anyhow::Result<()> {
match cli.cmd {
Some(Command::Init { path }) => init_cmd(path)?,
None => format_cmd(cli)?,
Some(Command::PrjFmt { path }) => format_cmd(Some(path))?,
None => format_cmd(None)?,
}
return Ok(());
}
/// Look up prjfmt toml from current directory up into project's root
pub fn lookup_prjfmt_toml(path: PathBuf) -> Result<PathBuf> {
let mut work = path;
loop {
if work.join("prjfmt.toml").exists() {
return Ok(work);
}
let prev = work.clone();
work = match work.parent() {
Some(x) => x.to_path_buf(),
None => return Err(anyhow!("You already reached root directory"))
};
if prev == work {
return Err(anyhow!("prjfmt.toml could not be found"))
}
}
}