Skip to content

Commit

Permalink
Add cargo deny init argument. (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
foresterre authored and Jake-Shadle committed Dec 2, 2019
1 parent e1c9347 commit 2f349d4
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 8 deletions.
48 changes: 48 additions & 0 deletions resources/template.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[licenses]
# This section is considered when running `cargo deny check license`
# More documentation for the licenses section can be found here:
# https://github.com/EmbarkStudios/cargo-deny#the-licenses-section

# Uncomment the following line to change the lint level for unlicensed crates [possible values: "deny", "allow" or "warn"].
#unlicensed = "deny"

# Uncomment the following line to explictly allow certain licenses [possible values: any SPDX 2.1 identifier].
#allow = []

# Uncomment the following line to explictly disallow certain licenses [possible values: any SPDX 2.1 identifier].
#deny = []

# Uncomment the following line to change the lint level for licenses considered copyleft [possible values: "deny", "allow" or "warn"].
#copyleft = "warn"

# Uncomment the following line to approve or deny OSI-approved or FSF Free/Libre licenses [possible values: "both", "either", "osi-only", "fsf-only" or "neither"].
#allow-osi-fsf-free = "neither"

# Uncomment the following line to change the confidence threshold. [possible values: any between 0.0 and 1.0].
# The higher the value, the more closely the license text must be to the canonical license text of a valid SPDX license file.
#confidence-threshold = 0.8

[bans]
# This section is considered when running `cargo deny check ban`.
# More documentation about the 'bans' section can be found here:
# https://github.com/EmbarkStudios/cargo-deny#crate-bans-cargo-deny-check-ban

# Uncomment the following line to change what happens when multiple versions of the same crate are encountered [possible values: "deny", "warn" or "allow"].
#multiple-versions = "warn"

# Uncomment the following line to change the highlighting variant used to multiple versions of the same crate when creating
# a dotgraph of your crates dependencies [possible values: "lowest-version", "simplest-path" or "all"].
#highlight = "all"

# Uncomment the following line to allow specific crates.
#allow = []

# Uncomment the following line to deny specific crates.
#deny = []

# Uncomment the following line to skip specific crates.
#skip = []

# Uncomment the following line to skip specific crates (including different versions of the same crate down the dependency
# tree). By default, the depth is infinite. If however desired, the depth can be ajusted.
#skip-tree = []
12 changes: 4 additions & 8 deletions src/cargo-deny/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use serde::Deserialize;
use std::path::{Path, PathBuf};
use structopt::StructOpt;

use crate::common::make_absolute_path;

arg_enum! {
#[derive(Debug, PartialEq)]
pub enum WhichCheck {
Expand Down Expand Up @@ -87,14 +89,8 @@ pub fn cmd(
) -> Result<(), Error> {
let cfg_path = args
.config
.or_else(|| Some("deny.toml".to_owned().into()))
.map(|p| {
if p.is_absolute() {
p
} else {
context_dir.join(p)
}
})
.or_else(|| Some("deny.toml".into()))
.map(|p| make_absolute_path(p, context_dir))
.context("unable to determine config path")?;

let mut files = codespan::Files::new();
Expand Down
9 changes: 9 additions & 0 deletions src/cargo-deny/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::arg_enum;
use std::path::PathBuf;

arg_enum! {
#[derive(Copy, Clone, Debug)]
Expand All @@ -7,3 +8,11 @@ arg_enum! {
Json,
}
}

pub(crate) fn make_absolute_path(path: PathBuf, context_dir: PathBuf) -> PathBuf {
if path.is_absolute() {
path
} else {
context_dir.join(path)
}
}
41 changes: 41 additions & 0 deletions src/cargo-deny/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use anyhow::{ensure, Context, Error};
use std::path::PathBuf;
use structopt::StructOpt;

use crate::common::make_absolute_path;

#[derive(StructOpt, Debug, Clone)]
pub struct Args {
/// The path to the config file. Defaults to <context>/deny.toml
#[structopt(short, long, parse(from_os_str))]
config: Option<PathBuf>,
}

const DENY_TOML: &str = "deny.toml";
const CONTENTS: &[u8] = include_bytes!("../../resources/template.toml");

pub fn cmd(args: Args, context_dir: PathBuf) -> Result<(), Error> {
let cfg_file = args
.config
.clone()
.or_else(|| Some(DENY_TOML.into()))
.map(|path| make_absolute_path(path, context_dir))
.context("unable to determine config path")?;

// make sure the file does not exist yet
ensure!(
std::fs::metadata(&cfg_file).is_err(),
"unable to initialize cargo deny config file ; the provided path already exists"
);
// make sure the path does not terminate in '..'; we need a file name.
ensure!(
&cfg_file.file_name().is_some(),
"unable to create a config file with the given name ; the given file path is not valid"
);

log::info!("saving config file to: {}", &cfg_file.display());

std::fs::write(cfg_file, CONTENTS).context("unable to write config file")?;

Ok(())
}
5 changes: 5 additions & 0 deletions src/cargo-deny/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use structopt::StructOpt;

mod check;
mod common;
mod init;
mod list;

#[derive(StructOpt, Debug)]
Expand All @@ -18,6 +19,9 @@ enum Command {
/// Checks your dependency graph based on the configuration you specify
#[structopt(name = "check")]
Check(check::Args),
/// Initialize an empty deny.toml file in your current crate.
#[structopt(name = "init")]
Init(init::Args),
}

fn parse_level(s: &str) -> Result<log::LevelFilter, Error> {
Expand Down Expand Up @@ -141,6 +145,7 @@ fn real_main() -> Result<(), Error> {
all_crates,
license_store,
),
Command::Init(init) => init::cmd(init, context_dir),
}
}

Expand Down

0 comments on commit 2f349d4

Please sign in to comment.