Skip to content

Commit 2f349d4

Browse files
foresterreJake-Shadle
authored andcommitted
Add cargo deny init argument. (#48)
1 parent e1c9347 commit 2f349d4

File tree

5 files changed

+107
-8
lines changed

5 files changed

+107
-8
lines changed

resources/template.toml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
[licenses]
2+
# This section is considered when running `cargo deny check license`
3+
# More documentation for the licenses section can be found here:
4+
# https://github.com/EmbarkStudios/cargo-deny#the-licenses-section
5+
6+
# Uncomment the following line to change the lint level for unlicensed crates [possible values: "deny", "allow" or "warn"].
7+
#unlicensed = "deny"
8+
9+
# Uncomment the following line to explictly allow certain licenses [possible values: any SPDX 2.1 identifier].
10+
#allow = []
11+
12+
# Uncomment the following line to explictly disallow certain licenses [possible values: any SPDX 2.1 identifier].
13+
#deny = []
14+
15+
# Uncomment the following line to change the lint level for licenses considered copyleft [possible values: "deny", "allow" or "warn"].
16+
#copyleft = "warn"
17+
18+
# 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"].
19+
#allow-osi-fsf-free = "neither"
20+
21+
# Uncomment the following line to change the confidence threshold. [possible values: any between 0.0 and 1.0].
22+
# The higher the value, the more closely the license text must be to the canonical license text of a valid SPDX license file.
23+
#confidence-threshold = 0.8
24+
25+
[bans]
26+
# This section is considered when running `cargo deny check ban`.
27+
# More documentation about the 'bans' section can be found here:
28+
# https://github.com/EmbarkStudios/cargo-deny#crate-bans-cargo-deny-check-ban
29+
30+
# Uncomment the following line to change what happens when multiple versions of the same crate are encountered [possible values: "deny", "warn" or "allow"].
31+
#multiple-versions = "warn"
32+
33+
# Uncomment the following line to change the highlighting variant used to multiple versions of the same crate when creating
34+
# a dotgraph of your crates dependencies [possible values: "lowest-version", "simplest-path" or "all"].
35+
#highlight = "all"
36+
37+
# Uncomment the following line to allow specific crates.
38+
#allow = []
39+
40+
# Uncomment the following line to deny specific crates.
41+
#deny = []
42+
43+
# Uncomment the following line to skip specific crates.
44+
#skip = []
45+
46+
# Uncomment the following line to skip specific crates (including different versions of the same crate down the dependency
47+
# tree). By default, the depth is infinite. If however desired, the depth can be ajusted.
48+
#skip-tree = []

src/cargo-deny/check.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use serde::Deserialize;
66
use std::path::{Path, PathBuf};
77
use structopt::StructOpt;
88

9+
use crate::common::make_absolute_path;
10+
911
arg_enum! {
1012
#[derive(Debug, PartialEq)]
1113
pub enum WhichCheck {
@@ -87,14 +89,8 @@ pub fn cmd(
8789
) -> Result<(), Error> {
8890
let cfg_path = args
8991
.config
90-
.or_else(|| Some("deny.toml".to_owned().into()))
91-
.map(|p| {
92-
if p.is_absolute() {
93-
p
94-
} else {
95-
context_dir.join(p)
96-
}
97-
})
92+
.or_else(|| Some("deny.toml".into()))
93+
.map(|p| make_absolute_path(p, context_dir))
9894
.context("unable to determine config path")?;
9995

10096
let mut files = codespan::Files::new();

src/cargo-deny/common.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clap::arg_enum;
2+
use std::path::PathBuf;
23

34
arg_enum! {
45
#[derive(Copy, Clone, Debug)]
@@ -7,3 +8,11 @@ arg_enum! {
78
Json,
89
}
910
}
11+
12+
pub(crate) fn make_absolute_path(path: PathBuf, context_dir: PathBuf) -> PathBuf {
13+
if path.is_absolute() {
14+
path
15+
} else {
16+
context_dir.join(path)
17+
}
18+
}

src/cargo-deny/init.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use anyhow::{ensure, Context, Error};
2+
use std::path::PathBuf;
3+
use structopt::StructOpt;
4+
5+
use crate::common::make_absolute_path;
6+
7+
#[derive(StructOpt, Debug, Clone)]
8+
pub struct Args {
9+
/// The path to the config file. Defaults to <context>/deny.toml
10+
#[structopt(short, long, parse(from_os_str))]
11+
config: Option<PathBuf>,
12+
}
13+
14+
const DENY_TOML: &str = "deny.toml";
15+
const CONTENTS: &[u8] = include_bytes!("../../resources/template.toml");
16+
17+
pub fn cmd(args: Args, context_dir: PathBuf) -> Result<(), Error> {
18+
let cfg_file = args
19+
.config
20+
.clone()
21+
.or_else(|| Some(DENY_TOML.into()))
22+
.map(|path| make_absolute_path(path, context_dir))
23+
.context("unable to determine config path")?;
24+
25+
// make sure the file does not exist yet
26+
ensure!(
27+
std::fs::metadata(&cfg_file).is_err(),
28+
"unable to initialize cargo deny config file ; the provided path already exists"
29+
);
30+
// make sure the path does not terminate in '..'; we need a file name.
31+
ensure!(
32+
&cfg_file.file_name().is_some(),
33+
"unable to create a config file with the given name ; the given file path is not valid"
34+
);
35+
36+
log::info!("saving config file to: {}", &cfg_file.display());
37+
38+
std::fs::write(cfg_file, CONTENTS).context("unable to write config file")?;
39+
40+
Ok(())
41+
}

src/cargo-deny/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use structopt::StructOpt;
88

99
mod check;
1010
mod common;
11+
mod init;
1112
mod list;
1213

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

2327
fn parse_level(s: &str) -> Result<log::LevelFilter, Error> {
@@ -141,6 +145,7 @@ fn real_main() -> Result<(), Error> {
141145
all_crates,
142146
license_store,
143147
),
148+
Command::Init(init) => init::cmd(init, context_dir),
144149
}
145150
}
146151

0 commit comments

Comments
 (0)