Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better default for the PRDoc work dir #9

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ fn main() -> color_eyre::Result<()> {

Some(SubCommand::Check(cmd_opts)) => {
debug!("cmd_opts: {cmd_opts:#?}");
let results = CheckCmd::run(&cmd_opts.directory, cmd_opts.file, cmd_opts.number, cmd_opts.list);
let dir = match cmd_opts.directory {
Some(d) => d,
None => prdoclib::utils::get_pr_doc_folder().expect("Always have a default"),
};
debug!("PRDoc folder: {dir:?}");
let results = CheckCmd::run(&dir, cmd_opts.file, cmd_opts.number, cmd_opts.list);

if !opts.json {
for (number, result) in &results {
Expand All @@ -57,21 +62,29 @@ fn main() -> color_eyre::Result<()> {

Some(SubCommand::Scan(cmd_opts)) => {
debug!("cmd_opts: {cmd_opts:#?}");
ScanCmd::run(&cmd_opts.directory, cmd_opts.all);
let dir = match cmd_opts.directory {
Some(d) => d,
None => prdoclib::utils::get_pr_doc_folder().expect("Always have a default"),
};
debug!("PRDoc folder: {dir:?}");
ScanCmd::run(&dir, cmd_opts.all);
Ok(())
}

Some(SubCommand::Load(cmd_opts)) => {
debug!("cmd_opts: {cmd_opts:#?}");
let result =
LoadCmd::run(&cmd_opts.directory, cmd_opts.file, cmd_opts.number, cmd_opts.list, opts.json).unwrap();
let dir = match cmd_opts.directory {
Some(d) => d,
None => prdoclib::utils::get_pr_doc_folder().expect("Always have a default"),
};
debug!("PRDoc folder: {dir:?}");

let result = LoadCmd::run(&dir, cmd_opts.file, cmd_opts.number, cmd_opts.list, opts.json).unwrap();

if result.is_some_and(|r| !r) {
eprintln!(
"⚠️ There are errors with files in {}",
std::fs::canonicalize(cmd_opts.directory)
.map(|p| p.display().to_string())
.unwrap_or("?".to_string())
std::fs::canonicalize(dir).map(|p| p.display().to_string()).unwrap_or("?".to_string())
);
std::process::exit(exitcode::DATAERR)
} else {
Expand Down
18 changes: 9 additions & 9 deletions cli/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ pub struct GenOpts {
/// Check one or MORE `prdoc` files for validity.
#[derive(Parser, Debug)]
pub struct CheckOpts {
/// Base directory for the files
#[clap(short, long, default_value = ".")]
pub directory: PathBuf,
/// Base directory for the files. Default to folder `PRDOC_DIR` under the root of your project.
#[clap(short, long)]
pub directory: Option<PathBuf>,

/// Directly specify the file to be checked. It can be relative to the base directory.
#[clap(short, long, conflicts_with = "number")]
Expand All @@ -99,9 +99,9 @@ pub struct CheckOpts {
/// Scan a directory for prdoc files based on their name
#[derive(Parser, Debug)]
pub struct ScanOpts {
/// directory path
#[clap(index = 1, default_value = ".")]
pub directory: PathBuf,
/// Base directory for the files. Default to folder `PRDOC_DIR` under the root of your project.
#[clap(index = 1)]
pub directory: Option<PathBuf>,

/// Also return invalid files
#[clap(short, long)]
Expand All @@ -111,9 +111,9 @@ pub struct ScanOpts {
/// Load one or more prdoc
#[derive(Parser, Debug)]
pub struct LoadOpts {
/// directory path
#[clap(short, long, default_value = ".")]
pub directory: PathBuf,
/// Base directory for the files. Default to folder `PRDOC_DIR` under the root of your project.
#[clap(short, long)]
pub directory: Option<PathBuf>,

/// file path
#[clap(short, long, conflicts_with = "number")]
Expand Down
6 changes: 4 additions & 2 deletions prdoclib/src/commands/check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::utils::get_numbers_from_file;
use crate::{common::PRNumber, doc_filename::DocFileName, docfile::DocFile, error, schema::Schema};
use crate::{
common::PRNumber, doc_filename::DocFileName, docfile::DocFile, error, schema::Schema,
utils::get_numbers_from_file,
};
use log::debug;
use std::{
collections::HashSet,
Expand Down
3 changes: 1 addition & 2 deletions prdoclib/src/commands/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use crate::{
docfile_wrapper::DocFileWrapper,
error::{self, Result},
schema::Schema,
utils::get_numbers_from_file,
};
use std::{
collections::HashSet,
path::{Path, PathBuf},
};

use super::utils::get_numbers_from_file;

pub struct LoadCmd;

impl LoadCmd {
Expand Down
1 change: 0 additions & 1 deletion prdoclib/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ pub mod generate;
pub mod load;
pub mod scan;
pub mod schema;
pub mod utils;
pub mod version;
17 changes: 0 additions & 17 deletions prdoclib/src/commands/utils.rs

This file was deleted.

1 change: 1 addition & 0 deletions prdoclib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pub mod docfile_wrapper;
pub mod title;

pub mod schema;
pub mod utils;

pub mod error;
55 changes: 55 additions & 0 deletions prdoclib/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::{
env,
fs::read_dir,
io::{self, ErrorKind},
path::PathBuf,
};

use crate::{common::PRNumber, error};

pub type ParseResult = (String, bool, Option<PRNumber>);

pub(crate) fn get_numbers_from_file(file: &PathBuf) -> error::Result<Vec<ParseResult>> {
Ok(std::fs::read_to_string(file)?
.lines()
.map(|line| {
let num = line.parse::<PRNumber>();
let valid = num.is_ok();
let number = num.ok();
(line.to_string(), valid, number)
})
.collect())
}

/// Get the project root (relative to closest Cargo.lock file)
/// ```rust
/// match project_root::get_project_root() {
/// Ok(p) => println!("Current project root is {:?}", p),
/// Err(e) => println!("Error obtaining project root {:?}", e)
/// };
/// ```
pub fn get_project_root() -> io::Result<PathBuf> {
let path = env::current_dir()?;
let path_ancestors = path.as_path().ancestors();

for p in path_ancestors {
let has_cargo = read_dir(p)?.any(|p| p.unwrap().file_name() == "Cargo.lock");
if has_cargo {
return Ok(PathBuf::from(p))
}
}
Err(io::Error::new(ErrorKind::NotFound, "Ran out of places to find Cargo.toml"))
}

/// Return the path of the folder where PRDoc are stored
pub fn get_pr_doc_folder() -> io::Result<PathBuf> {
// TODO: Use config as well
let root = get_project_root()?;
let subdir = match env::var("PRDOC_DIR") {
Ok(v) => v,
Err(_) => "prdoc".to_string(),
};

let dir = root.join(subdir);
Ok(dir)
}
Loading