|
1 | 1 | use anyhow::Result; |
2 | | -use clang_installer::{CliOptions, RequestedVersion}; |
| 2 | +use clang_installer::{ClangTool, RequestedVersion}; |
3 | 3 | use clap::Parser; |
4 | | -use colored::{Colorize, control::set_override}; |
5 | | -use log::{Level, LevelFilter, Log, Metadata, Record}; |
6 | 4 |
|
7 | | -use std::{ |
8 | | - collections::HashMap, |
9 | | - env, |
10 | | - io::{Write, stdout}, |
11 | | -}; |
| 5 | +use std::{collections::HashMap, path::PathBuf, str::FromStr}; |
| 6 | +mod logging { |
| 7 | + use colored::{Colorize, control::set_override}; |
| 8 | + use log::{Level, LevelFilter, Log, Metadata, Record}; |
| 9 | + use std::{ |
| 10 | + env, |
| 11 | + io::{Write, stdout}, |
| 12 | + }; |
12 | 13 |
|
13 | | -struct SimpleLogger; |
| 14 | + struct SimpleLogger; |
14 | 15 |
|
15 | | -impl SimpleLogger { |
16 | | - fn level_color(level: &Level) -> String { |
17 | | - let name = format!("{:>5}", level.as_str().to_uppercase()); |
18 | | - match level { |
19 | | - Level::Error => name.red().bold().to_string(), |
20 | | - Level::Warn => name.yellow().bold().to_string(), |
21 | | - Level::Info => name.green().bold().to_string(), |
22 | | - Level::Debug => name.blue().bold().to_string(), |
23 | | - Level::Trace => name.magenta().bold().to_string(), |
| 16 | + impl SimpleLogger { |
| 17 | + fn level_color(level: &Level) -> String { |
| 18 | + let name = format!("{:>5}", level.as_str().to_uppercase()); |
| 19 | + match level { |
| 20 | + Level::Error => name.red().bold().to_string(), |
| 21 | + Level::Warn => name.yellow().bold().to_string(), |
| 22 | + Level::Info => name.green().bold().to_string(), |
| 23 | + Level::Debug => name.blue().bold().to_string(), |
| 24 | + Level::Trace => name.magenta().bold().to_string(), |
| 25 | + } |
24 | 26 | } |
25 | 27 | } |
26 | | -} |
27 | 28 |
|
28 | | -impl Log for SimpleLogger { |
29 | | - fn enabled(&self, metadata: &Metadata) -> bool { |
30 | | - metadata.level() <= log::max_level() |
31 | | - } |
| 29 | + impl Log for SimpleLogger { |
| 30 | + fn enabled(&self, metadata: &Metadata) -> bool { |
| 31 | + metadata.level() <= log::max_level() |
| 32 | + } |
32 | 33 |
|
33 | | - fn log(&self, record: &Record) { |
34 | | - let mut stdout = stdout().lock(); |
35 | | - if record.target() == "CI_LOG_GROUPING" { |
36 | | - // this log is meant to manipulate a CI workflow's log grouping |
37 | | - writeln!(stdout, "{}", record.args()).expect("Failed to write log command to stdout"); |
38 | | - stdout |
39 | | - .flush() |
40 | | - .expect("Failed to flush log command in stdout"); |
41 | | - } else if self.enabled(record.metadata()) { |
42 | | - let module = record.module_path(); |
43 | | - if module |
44 | | - .is_none_or(|v| v.starts_with("clang_installer") || v.starts_with("clang_tools")) |
45 | | - { |
46 | | - writeln!( |
47 | | - stdout, |
48 | | - "[{}]: {}", |
49 | | - Self::level_color(&record.level()), |
50 | | - record.args() |
51 | | - ) |
52 | | - .expect("Failed to write log message to stdout"); |
53 | | - } else if let Some(module) = module { |
54 | | - writeln!( |
55 | | - stdout, |
56 | | - "[{}]{{{}:{}}}: {}", |
57 | | - Self::level_color(&record.level()), |
58 | | - module, |
59 | | - record.line().unwrap_or_default(), |
60 | | - record.args() |
61 | | - ) |
62 | | - .expect("Failed to write detailed log message to stdout"); |
| 34 | + fn log(&self, record: &Record) { |
| 35 | + let mut stdout = stdout().lock(); |
| 36 | + if record.target() == "CI_LOG_GROUPING" { |
| 37 | + // this log is meant to manipulate a CI workflow's log grouping |
| 38 | + writeln!(stdout, "{}", record.args()) |
| 39 | + .expect("Failed to write log command to stdout"); |
| 40 | + stdout |
| 41 | + .flush() |
| 42 | + .expect("Failed to flush log command in stdout"); |
| 43 | + } else if self.enabled(record.metadata()) { |
| 44 | + let module = record.module_path(); |
| 45 | + if module.is_none_or(|v| { |
| 46 | + v.starts_with("clang_installer") || v.starts_with("clang_tools") |
| 47 | + }) { |
| 48 | + writeln!( |
| 49 | + stdout, |
| 50 | + "[{}]: {}", |
| 51 | + Self::level_color(&record.level()), |
| 52 | + record.args() |
| 53 | + ) |
| 54 | + .expect("Failed to write log message to stdout"); |
| 55 | + } else if let Some(module) = module { |
| 56 | + writeln!( |
| 57 | + stdout, |
| 58 | + "[{}]{{{}:{}}}: {}", |
| 59 | + Self::level_color(&record.level()), |
| 60 | + module, |
| 61 | + record.line().unwrap_or_default(), |
| 62 | + record.args() |
| 63 | + ) |
| 64 | + .expect("Failed to write detailed log message to stdout"); |
| 65 | + } |
| 66 | + stdout |
| 67 | + .flush() |
| 68 | + .expect("Failed to flush log message in stdout"); |
63 | 69 | } |
64 | | - stdout |
65 | | - .flush() |
66 | | - .expect("Failed to flush log message in stdout"); |
67 | 70 | } |
| 71 | + |
| 72 | + fn flush(&self) {} |
68 | 73 | } |
69 | 74 |
|
70 | | - fn flush(&self) {} |
| 75 | + /// A function to initialize the private `LOGGER`. |
| 76 | + /// |
| 77 | + /// The logging level defaults to [`LevelFilter::Info`]. |
| 78 | + /// This logs a debug message about [`SetLoggerError`](struct@log::SetLoggerError) |
| 79 | + /// if the `LOGGER` is already initialized. |
| 80 | + pub fn initialize_logger() { |
| 81 | + let logger: SimpleLogger = SimpleLogger; |
| 82 | + if env::var("CPP_LINTER_COLOR") |
| 83 | + .as_deref() |
| 84 | + .is_ok_and(|v| matches!(v, "on" | "1" | "true")) |
| 85 | + { |
| 86 | + set_override(true); |
| 87 | + } |
| 88 | + if let Err(e) = |
| 89 | + log::set_boxed_logger(Box::new(logger)).map(|()| log::set_max_level(LevelFilter::Info)) |
| 90 | + { |
| 91 | + // logger singleton already instantiated. |
| 92 | + // we'll just use whatever the current config is. |
| 93 | + log::debug!("{e:?}"); |
| 94 | + } |
| 95 | + } |
71 | 96 | } |
72 | 97 |
|
73 | | -/// A function to initialize the private `LOGGER`. |
74 | | -/// |
75 | | -/// The logging level defaults to [`LevelFilter::Info`]. |
76 | | -/// This logs a debug message about [`SetLoggerError`](struct@log::SetLoggerError) |
77 | | -/// if the `LOGGER` is already initialized. |
78 | | -pub fn initialize_logger() { |
79 | | - let logger: SimpleLogger = SimpleLogger; |
80 | | - if env::var("CPP_LINTER_COLOR") |
81 | | - .as_deref() |
82 | | - .is_ok_and(|v| matches!(v, "on" | "1" | "true")) |
83 | | - { |
84 | | - set_override(true); |
85 | | - } |
86 | | - if let Err(e) = |
87 | | - log::set_boxed_logger(Box::new(logger)).map(|()| log::set_max_level(LevelFilter::Info)) |
88 | | - { |
89 | | - // logger singleton already instantiated. |
90 | | - // we'll just use whatever the current config is. |
91 | | - log::debug!("{e:?}"); |
92 | | - } |
| 98 | +#[derive(clap::Parser, Debug)] |
| 99 | +pub struct CliOptions { |
| 100 | + /// The desired version of clang to install. |
| 101 | + #[arg( |
| 102 | + short, |
| 103 | + long, |
| 104 | + default_missing_value = "CPP-LINTER-VERSION", |
| 105 | + num_args = 0..=1, |
| 106 | + value_parser = RequestedVersion::from_str, |
| 107 | + default_value = "", |
| 108 | + )] |
| 109 | + pub version: Option<RequestedVersion>, |
| 110 | + /// The clang tool to install. |
| 111 | + #[arg( |
| 112 | + short, |
| 113 | + long, |
| 114 | + value_delimiter = ' ', |
| 115 | + default_value = "clang-format clang-tidy" |
| 116 | + )] |
| 117 | + pub tool: Option<Vec<ClangTool>>, |
| 118 | + /// The directory where the clang tools should be installed. |
| 119 | + #[arg(short, long)] |
| 120 | + pub directory: Option<PathBuf>, |
| 121 | + /// Force overwriting symlink to the installed binary. |
| 122 | + /// |
| 123 | + /// This will only overwrite an existing symlink. |
| 124 | + #[arg(short, long)] |
| 125 | + pub force: bool, |
93 | 126 | } |
94 | 127 |
|
95 | 128 | #[tokio::main] |
96 | 129 | async fn main() -> Result<()> { |
97 | | - initialize_logger(); |
| 130 | + logging::initialize_logger(); |
98 | 131 | let options = CliOptions::parse(); |
99 | 132 | log::debug!("{:?}", options); |
100 | 133 |
|
|
0 commit comments