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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// SPDX-FileCopyrightText: 2021-2022 Lynnesbian
// SPDX-License-Identifier: GPL-3.0-or-later

//! Logic for handling the various output formats that fif can output to.

#![allow(missing_copy_implementations)]

use std::ffi::OsStr;
use std::io::{self, Write};
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
use std::path::Path;

use cfg_if::cfg_if;
use snailquote::escape;

use crate::findings::ScanError;
use crate::utils::CLAP_LONG_VERSION;
use crate::Findings;
use crate::String;

/// A macro for creating an array of [`Writable`]s without needing to pepper your code with `into()`s.
/// # Usage
/// ```
/// use crate::fif::writables;
/// use crate::fif::formats::{Writable, smart_write};
/// let mut f = std::io::stdout();
///
/// // Instead of...
/// smart_write(&mut f, &["hello".into(), Writable::Newline]);
/// // ...just use:
/// smart_write(&mut f, writables!["hello", Newline]);
/// ```
#[macro_export]
macro_rules! writables {
	[$($args:tt),+] => {
		&[$(writables!(@do $args),)*]
	};

	(@do Newline) => {
		$crate::formats::Writable::Newline
	};

	(@do $arg:expr) => {
		$arg.into()
	}
}

#[macro_export]
/// Does the same thing as [`writables`], but adds a Newline to the end.
macro_rules! writablesln {
	[$($args:tt),+] => {
		&[$(writables!(@do $args),)* writables!(@do Newline)]
	};
}

#[derive(Debug, PartialEq, Eq)]
pub enum Writable<'a> {
	String(&'a str),
	Path(&'a Path),
	Newline,
}

// the lifetime of a lifetime
impl<'a> From<&'a str> for Writable<'a> {
	fn from(s: &'a str) -> Writable<'a> { Writable::String(s) }
}

impl<'a> From<&'a Path> for Writable<'a> {
	fn from(p: &'a Path) -> Writable<'a> { Writable::Path(p) }
}

impl<'a> From<&'a OsStr> for Writable<'a> {
	fn from(p: &'a OsStr) -> Writable<'a> { Writable::Path(p.as_ref()) }
}

fn generated_by() -> String { format!("Generated by fif {}", CLAP_LONG_VERSION.as_str()).into() }

pub fn smart_write<W: Write>(f: &mut W, writeables: &[Writable]) -> io::Result<()> {
	// ehhhh
	for writeable in writeables {
		match writeable {
			Writable::Newline => {
				cfg_if! {
					if #[cfg(windows)] {
						write!(f, "\r\n")?;
					} else {
						writeln!(f,)?;
					}
				}
			}
			Writable::String(s) => write!(f, "{s}")?,
			Writable::Path(path) => {
				if let Some(path_str) = path.to_str() {
					let escaped = escape(path_str);
					if escaped.as_ref() == path_str {
						// the escaped string is the same as the input - this will occur for inputs like "file.txt" which don't
						// need to be escaped. however, it's Best Practice™ to escape such strings anyway, so we prefix/suffix the
						// escaped string with single quotes.
						write!(f, "'{escaped}'")?;
					} else {
						write!(f, "{escaped}")?;
					}
				} else {
					write!(f, "'")?;
					cfg_if! {
						if #[cfg(windows)] {
							// TODO: implement bonked strings for windows
							// something like:
							// f.write_all(&*path.as_os_str().encode_wide().collect::<Vec<u16>>())?;
							write!(f, "{}", path.as_os_str().to_string_lossy())?;
						} else {
							f.write_all(path.as_os_str().as_bytes())?;
						}
					}
					write!(f, "'")?;
				}
			}
		}
	}
	Ok(())
}

pub trait FormatSteps {
	fn rename<W: Write>(&self, _f: &mut W, _from: &Path, _to: &Path) -> io::Result<()>;
	fn no_known_extension<W: Write>(&self, _f: &mut W, _path: &Path) -> io::Result<()>;
	fn unreadable<W: Write>(&self, _f: &mut W, _path: &Path) -> io::Result<()>;
	fn unknown_type<W: Write>(&self, _f: &mut W, _path: &Path) -> io::Result<()>;
	fn header<W: Write>(&self, _f: &mut W) -> io::Result<()>;
	fn footer<W: Write>(&self, _f: &mut W) -> io::Result<()>;
	fn write_steps<W: Write>(&self, f: &mut W, findings: &[Findings], errors: &[ScanError]) -> io::Result<()> {
		self.header(f)?;

		for error in errors {
			match error {
				// failed to read the file
				ScanError::File(path) => self.unreadable(f, path)?,
				// file was read successfully, but we couldn't determine a MIME type
				ScanError::Mime(path) => self.unknown_type(f, path)?,
			}
		}

		if !errors.is_empty() {
			// add a blank line between the errors and commands
			smart_write(f, writables![Newline])?;
		}

		for finding in findings {
			if let Some(name) = finding.recommended_path() {
				self.rename(f, finding.file.as_path(), &name)?;
			} else {
				self.no_known_extension(f, finding.file.as_path())?;
			}
		}

		self.footer(f)
	}
}

pub trait Format {
	fn write_all<W: Write>(&self, f: &mut W, findings: &[Findings], errors: &[ScanError]) -> io::Result<()>;
}

/// Bourne-Shell compatible script.
pub struct Shell;

impl Format for Shell {
	fn write_all<W: Write>(&self, f: &mut W, findings: &[Findings], errors: &[ScanError]) -> io::Result<()> {
		self.write_steps(f, findings, errors)
	}
}

impl FormatSteps for Shell {
	fn rename<W: Write>(&self, f: &mut W, from: &Path, to: &Path) -> io::Result<()> {
		smart_write(f, writablesln!("mv -v -i -- ", from, "\t", to))
	}

	fn no_known_extension<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
		smart_write(
			f,
			writablesln!["cat <<- '???'", Newline, "No known extension for ", path, Newline, "???"],
		)
	}

	fn unreadable<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
		smart_write(f, writablesln!["# Failed to read", path])
	}

	fn unknown_type<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
		smart_write(f, writablesln!["# Failed to detect MIME type for ", path])
	}

	fn header<W: Write>(&self, f: &mut W) -> io::Result<()> {
		smart_write(f, writablesln!["#!/usr/bin/env sh", Newline, "# ", (generated_by().as_str())])?;

		if let Ok(working_directory) = std::env::current_dir() {
			smart_write(f, writablesln!["# Run from ", (working_directory.as_path())])?;
		}
		write!(f, "# Happy with these changes? Run `fif --fix` from the same directory!")?;

		smart_write(f, writablesln![Newline, "set -e", Newline])
	}

	fn footer<W: Write>(&self, f: &mut W) -> io::Result<()> { smart_write(f, writablesln![Newline, "echo 'Done.'"]) }
}

// PowerShell is a noun, not a type
#[allow(clippy::doc_markdown)]
/// PowerShell script.
pub struct PowerShell;

impl Format for PowerShell {
	fn write_all<W: Write>(&self, f: &mut W, findings: &[Findings], errors: &[ScanError]) -> io::Result<()> {
		self.write_steps(f, findings, errors)
	}
}

impl FormatSteps for PowerShell {
	fn rename<W: Write>(&self, f: &mut W, from: &Path, to: &Path) -> io::Result<()> {
		// unfortunately there doesn't seem to be an equivalent of sh's `mv -i` -- passing the '-Confirm' flag will prompt
		// the user to confirm every single rename, and using Move-Item -Force will always overwrite without prompting.
		// there doesn't seem to be a way to rename the file, prompting only if the target already exists.
		smart_write(
			f,
			writablesln!["Rename-Item -Verbose -Path ", from, " -NewName ", (to.file_name().unwrap())],
		)
	}

	fn no_known_extension<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
		smart_write(
			f,
			writablesln!["Write-Output @'", Newline, "No known extension for ", path, Newline, "'@"],
		)
	}

	fn unreadable<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
		smart_write(
			f,
			writablesln!["Write-Output @'", Newline, "Failed to read ", path, Newline, "'@"],
		)
	}

	fn unknown_type<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
		smart_write(f, writablesln!["<# Failed to detect MIME type for ", path, " #>"])
	}

	fn header<W: Write>(&self, f: &mut W) -> io::Result<()> {
		smart_write(
			f,
			writablesln!["#!/usr/bin/env pwsh", Newline, "<# ", (generated_by().as_str()), " #>"],
		)?;

		if let Ok(working_directory) = std::env::current_dir() {
			smart_write(f, writablesln!["<# Run from ", (working_directory.as_path()), " #>"])?;
		}
		write!(f, "<# Happy with these changes? Run `fif --fix` from the same directory! #>")?;

		smart_write(f, writables![Newline])
	}

	fn footer<W: Write>(&self, f: &mut W) -> io::Result<()> {
		smart_write(f, writablesln![Newline, "Write-Output 'Done!'"])
	}
}

pub struct Text;
impl Format for Text {
	fn write_all<W: Write>(&self, f: &mut W, findings: &[Findings], errors: &[ScanError]) -> io::Result<()> {
		self.write_steps(f, findings, errors)
	}
}

impl FormatSteps for Text {
	fn rename<W: Write>(&self, f: &mut W, from: &Path, to: &Path) -> io::Result<()> {
		smart_write(f, writablesln![from, " should be renamed to ", to])
	}

	fn no_known_extension<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
		smart_write(f, writablesln!["No known extension for ", path])
	}

	fn unreadable<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
		smart_write(f, writablesln!["Encountered IO error while accessing ", path])
	}

	fn unknown_type<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
		smart_write(f, writablesln!["Couldn't determine type for ", path])
	}

	fn header<W: Write>(&self, f: &mut W) -> io::Result<()> {
		smart_write(f, writablesln![(generated_by().as_str()), Newline])?;
		if let Ok(working_directory) = std::env::current_dir() {
			smart_write(f, writablesln!["Run from ", (working_directory.as_path())])?;
		}
		write!(f, "Happy with these changes? Run `fif --fix` from the same directory!")
	}

	fn footer<W: Write>(&self, f: &mut W) -> io::Result<()> {
		smart_write(
			f,
			// writablesln![Newline, "Processed ", (entries.len().to_string().as_str()), " files"],
			&[Writable::Newline],
		)
	}
}

#[cfg(feature = "json")]
pub struct Json;

#[cfg(feature = "json")]
impl Format for Json {
	fn write_all<W: Write>(&self, f: &mut W, findings: &[Findings], errors: &[ScanError]) -> io::Result<()> {
		#[derive(serde::Serialize)]
		struct SerdeEntries<'a> {
			errors: &'a [ScanError<'a>],
			findings: &'a [Findings],
		}

		let result = serde_json::to_writer_pretty(f, &SerdeEntries { errors, findings });

		if let Err(err) = result {
			log::error!("Error while serialising: {}", err);
			return Err(err.into());
		}

		Ok(())
	}
}