11use anyhow:: Result ;
2- use clang_tools_manager:: { ClangTool , RequestedVersion } ;
2+ use clang_tools_manager:: {
3+ ClangTool , RequestedVersion ,
4+ logger:: { CLI_HELP_STYLE , try_init_logger} ,
5+ } ;
36use clap:: Parser ;
47
58use 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- } ;
13-
14- struct SimpleLogger ;
15-
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- }
26- }
27- }
28-
29- impl Log for SimpleLogger {
30- fn enabled ( & self , metadata : & Metadata ) -> bool {
31- metadata. level ( ) <= log:: max_level ( )
32- }
33-
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_tools_manager" ) || 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" ) ;
69- }
70- }
71-
72- fn flush ( & self ) { }
73- }
74-
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- }
96- }
979
9810#[ derive( clap:: Parser , Debug ) ]
11+ #[ command( styles( CLI_HELP_STYLE ) ) ]
9912pub struct CliOptions {
10013 /// The desired version of clang to install.
10114 #[ arg(
@@ -122,12 +35,10 @@ pub struct CliOptions {
12235
12336 /// The clang tool to install.
12437 #[ arg(
125- short,
126- long,
127- value_delimiter = ' ' ,
128- default_value = "clang-format clang-tidy"
38+ num_args = 0 ..,
39+ default_values_t = vec![ ClangTool :: ClangFormat , ClangTool :: ClangTidy ] ,
12940 ) ]
130- pub tool : Option < Vec < ClangTool > > ,
41+ pub tool : Vec < ClangTool > ,
13142
13243 /// The directory where the clang tools should be installed.
13344 #[ arg( short, long) ]
@@ -162,14 +73,11 @@ pub struct CliOptions {
16273
16374#[ tokio:: main]
16475async fn main ( ) -> Result < ( ) > {
165- logging :: initialize_logger ( ) ;
76+ try_init_logger ( ) ;
16677 let options = CliOptions :: parse ( ) ;
16778 if options. verbose {
16879 log:: set_max_level ( log:: LevelFilter :: Debug ) ;
16980 }
170- let tool = options
171- . tool
172- . expect ( "--tool should have a default value: [clang-format, clang-tidy]" ) ;
17381 match options. version . unwrap_or ( RequestedVersion :: default ( ) ) {
17482 RequestedVersion :: NoValue => {
17583 log:: info!(
@@ -179,7 +87,7 @@ async fn main() -> Result<()> {
17987 }
18088 req_ver => {
18189 let mut map_tools = HashMap :: new ( ) ;
182- for t in tool {
90+ for t in options . tool {
18391 if let Some ( version) = req_ver
18492 . eval_tool (
18593 & t,
0 commit comments