1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::common::*;
use clap::Arg;
use std::env;
use std::fs;
use std::fs::File;
use std::io::{BufRead, BufReader, Error};
use std::path::PathBuf;
mod checks;
mod common;
fn new_app<'a, 'b>() -> clap::App<'a, 'b> {
clap::App::new(env!("CARGO_PKG_NAME"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.author(env!("CARGO_PKG_AUTHORS"))
.version(env!("CARGO_PKG_VERSION"))
.version_short("v")
.arg(
Arg::with_name("include")
.short("i")
.long("include")
.value_name("FILE_NAME")
.help("Includes files to check")
.multiple(true)
.takes_value(true),
)
.arg(
Arg::with_name("exclude")
.short("e")
.long("exclude")
.value_name("FILE_NAME")
.help("Excludes files from check")
.multiple(true)
.takes_value(true),
)
.arg(
Arg::with_name("path")
.short("p")
.long("path")
.value_name("DIRECTORY_PATH")
.help("Specify the path of the directory where to run dotenv-linter")
.multiple(false)
.takes_value(true),
)
}
#[derive(Debug)]
pub struct DotenvLinter<'a> {
args: clap::ArgMatches<'a>,
}
pub fn new<'a>() -> DotenvLinter<'a> {
DotenvLinter {
args: new_app().get_matches(),
}
}
impl<'a> DotenvLinter<'a> {
pub fn run(&self) -> Result<Vec<Warning>, Error> {
let dir_path = match self.args.value_of("path") {
Some(path) => PathBuf::from(path),
None => env::current_dir()?,
};
let files = self.dotenv_files(dir_path)?;
let mut warnings: Vec<Warning> = Vec::new();
for file in files {
let f = File::open(&file.path)?;
let reader = BufReader::new(f);
let mut lines: Vec<LineEntry> = Vec::new();
for (index, line) in reader.lines().enumerate() {
let number = index + 1;
let raw_string = line?;
lines.push(LineEntry {
file_name: file.file_name.clone(),
number,
raw_string,
})
}
let result = checks::run(lines);
warnings.extend(result);
}
Ok(warnings)
}
#[allow(clippy::redundant_closure)]
fn dotenv_files(&self, dir_path: PathBuf) -> Result<Vec<FileEntry>, Error> {
let entries = dir_path.read_dir()?;
let mut paths: Vec<FileEntry> = entries
.filter_map(|e| e.ok())
.filter_map(|e| FileEntry::from(e.path()))
.filter(|f| f.is_env_file())
.collect();
if let Some(included) = self.args.values_of("include") {
included
.filter_map(|f| fs::canonicalize(f).ok())
.filter_map(|p| FileEntry::from(p))
.for_each(|f| paths.push(f));
}
if let Some(excluded) = self.args.values_of("exclude") {
let excluded_paths: Vec<PathBuf> =
excluded.filter_map(|f| fs::canonicalize(f).ok()).collect();
paths.retain(|f| !excluded_paths.contains(&f.path));
}
paths.sort();
paths.dedup();
Ok(paths)
}
}
#[cfg(test)]
mod tests {
use super::*;
mod args {
use super::*;
mod path {
use super::*;
use std::io::ErrorKind;
#[test]
fn file_not_found() {
let linter = DotenvLinter {
args: new_app().get_matches_from(vec!["dotenv-linter", "-p", "/foo"]),
};
let result = linter.run();
assert!(result.is_err());
assert_eq!(result.unwrap_err().kind(), ErrorKind::NotFound);
}
}
}
}