aboutsummaryrefslogtreecommitdiff
path: root/bin/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'bin/src/config.rs')
-rw-r--r--bin/src/config.rs92
1 files changed, 86 insertions, 6 deletions
diff --git a/bin/src/config.rs b/bin/src/config.rs
index e0f5cbd..25c2a7f 100644
--- a/bin/src/config.rs
+++ b/bin/src/config.rs
@@ -42,13 +42,28 @@ pub struct Check {
42 /// Supported values: stderr, errfmt, json (on feature flag only) 42 /// Supported values: stderr, errfmt, json (on feature flag only)
43 #[clap(short = 'o', long, default_value_t, parse(try_from_str))] 43 #[clap(short = 'o', long, default_value_t, parse(try_from_str))]
44 pub format: OutFormat, 44 pub format: OutFormat,
45
46 /// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout
47 #[clap(short, long = "stdin")]
48 pub streaming: bool,
45} 49}
46 50
47impl Check { 51impl Check {
48 pub fn vfs(&self) -> Result<ReadOnlyVfs, ConfigErr> { 52 pub fn vfs(&self) -> Result<ReadOnlyVfs, ConfigErr> {
49 let ignore = dirs::build_ignore_set(&self.ignore, &self.target, self.unrestricted)?; 53 if self.streaming {
50 let files = dirs::walk_nix_files(ignore, &self.target)?; 54 use std::io::{self, BufRead};
51 vfs(files.collect::<Vec<_>>()) 55 let src = io::stdin()
56 .lock()
57 .lines()
58 .map(|l| l.unwrap())
59 .collect::<Vec<String>>()
60 .join("\n");
61 Ok(ReadOnlyVfs::singleton("<stdin>", src.as_bytes()))
62 } else {
63 let ignore = dirs::build_ignore_set(&self.ignore, &self.target, self.unrestricted)?;
64 let files = dirs::walk_nix_files(ignore, &self.target)?;
65 vfs(files.collect::<Vec<_>>())
66 }
52 } 67 }
53} 68}
54 69
@@ -69,13 +84,46 @@ pub struct Fix {
69 /// Do not fix files in place, display a diff instead 84 /// Do not fix files in place, display a diff instead
70 #[clap(short, long = "dry-run")] 85 #[clap(short, long = "dry-run")]
71 pub diff_only: bool, 86 pub diff_only: bool,
87
88 /// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout
89 #[clap(short, long = "stdin")]
90 pub streaming: bool,
91}
92
93pub enum FixOut {
94 Diff,
95 Stream,
96 Write,
72} 97}
73 98
74impl Fix { 99impl Fix {
75 pub fn vfs(&self) -> Result<ReadOnlyVfs, ConfigErr> { 100 pub fn vfs(&self) -> Result<ReadOnlyVfs, ConfigErr> {
76 let ignore = dirs::build_ignore_set(&self.ignore, &self.target, self.unrestricted)?; 101 if self.streaming {
77 let files = dirs::walk_nix_files(ignore, &self.target)?; 102 use std::io::{self, BufRead};
78 vfs(files.collect::<Vec<_>>()) 103 let src = io::stdin()
104 .lock()
105 .lines()
106 .map(|l| l.unwrap())
107 .collect::<Vec<String>>()
108 .join("\n");
109 Ok(ReadOnlyVfs::singleton("<stdin>", src.as_bytes()))
110 } else {
111 let ignore = dirs::build_ignore_set(&self.ignore, &self.target, self.unrestricted)?;
112 let files = dirs::walk_nix_files(ignore, &self.target)?;
113 vfs(files.collect::<Vec<_>>())
114 }
115 }
116
117 // i need this ugly helper because clap's data model
118 // does not reflect what i have in mind
119 pub fn out(&self) -> FixOut {
120 if self.diff_only {
121 FixOut::Diff
122 } else if self.streaming {
123 FixOut::Stream
124 } else {
125 FixOut::Write
126 }
79 } 127 }
80} 128}
81 129
@@ -92,6 +140,38 @@ pub struct Single {
92 /// Do not fix files in place, display a diff instead 140 /// Do not fix files in place, display a diff instead
93 #[clap(short, long = "dry-run")] 141 #[clap(short, long = "dry-run")]
94 pub diff_only: bool, 142 pub diff_only: bool,
143
144 /// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout
145 #[clap(short, long = "stdin")]
146 pub streaming: bool,
147}
148
149impl Single {
150 pub fn vfs(&self) -> Result<ReadOnlyVfs, ConfigErr> {
151 if self.streaming {
152 use std::io::{self, BufRead};
153 let src = io::stdin()
154 .lock()
155 .lines()
156 .map(|l| l.unwrap())
157 .collect::<Vec<String>>()
158 .join("\n");
159 Ok(ReadOnlyVfs::singleton("<stdin>", src.as_bytes()))
160 } else {
161 let src = std::fs::read_to_string(self.target.as_ref().unwrap())
162 .map_err(ConfigErr::InvalidPath)?;
163 Ok(ReadOnlyVfs::singleton("<stdin>", src.as_bytes()))
164 }
165 }
166 pub fn out(&self) -> FixOut {
167 if self.diff_only {
168 FixOut::Diff
169 } else if self.streaming {
170 FixOut::Stream
171 } else {
172 FixOut::Write
173 }
174 }
95} 175}
96 176
97#[derive(Clap, Debug)] 177#[derive(Clap, Debug)]