@@ -15,7 +15,7 @@ enum FormattingStatus {
1515 Unchanged ,
1616}
1717
18- #[ derive( Parser ) ]
18+ #[ derive( Clone , Parser ) ]
1919#[ command( author, version, about, long_about = None ) ]
2020struct Cli {
2121 /// Check for formatting errors without writing to the file
@@ -28,10 +28,10 @@ struct Cli {
2828
2929 /// The file to format
3030 #[ arg( index = 1 , value_name = "FILE" ) ]
31- file_path : PathBuf ,
31+ file_path : Option < PathBuf > ,
3232}
3333
34- fn format_fs ( cli : & Cli , config : & Config ) -> bool {
34+ fn format_fs ( cli : & Cli , config : & Config , dir_path : & Path ) -> bool {
3535 let mut emitter = create_emitter ( false ) ;
3636 let mut has_errors = false ;
3737
@@ -40,7 +40,7 @@ fn format_fs(cli: &Cli, config: &Config) -> bool {
4040 types. add ( "devicetree" , "*.keymap" ) . unwrap ( ) ;
4141 types. select ( "devicetree" ) ;
4242
43- for result in WalkBuilder :: new ( & cli . file_path )
43+ for result in WalkBuilder :: new ( dir_path )
4444 . types ( types. build ( ) . unwrap ( ) )
4545 . add_custom_ignore_filename ( ".dtsfmtignore" )
4646 . standard_filters ( false )
@@ -64,14 +64,14 @@ fn format_fs(cli: &Cli, config: &Config) -> bool {
6464 has_errors
6565}
6666
67- fn format_stdin ( cli : & Cli , config : & Config ) -> bool {
67+ fn format_stdin ( cli : & Cli , config : & Config , dir_path : & Path ) -> bool {
6868 let mut emitter = create_emitter ( true ) ;
6969 let mut buffer = String :: new ( ) ;
7070 io:: stdin ( ) . read_to_string ( & mut buffer) . expect ( "Failed to read stdin" ) ;
7171
7272 // If the file is ignored, we need to print the original content unchanged
7373 // since we still need to return content when running in stdin mode.
74- let status = if is_ignored ( & cli . file_path ) {
74+ let status = if is_ignored ( dir_path ) {
7575 print_original ( cli, & mut emitter, & buffer)
7676 } else {
7777 format ( PathBuf :: from ( "stdin" ) , buffer, & mut emitter, config, cli. check )
@@ -82,12 +82,24 @@ fn format_stdin(cli: &Cli, config: &Config) -> bool {
8282
8383fn main ( ) {
8484 let cli = Cli :: parse ( ) ;
85- let config = Config :: parse ( & cli. file_path . to_path_buf ( ) ) ;
85+
86+ let cfg_path = match & cli. file_path {
87+ Some ( path) => path. clone ( ) ,
88+ None => std:: env:: current_dir ( ) . expect ( "Couldn't read CWD" ) ,
89+ } ;
90+ let config = Config :: parse ( & cfg_path) ;
91+
92+ // If no path was specified (likely with --stdin) then default to the
93+ // current working directory.
94+ let dir_path = match & cli. file_path {
95+ Some ( path) => path. clone ( ) ,
96+ None => std:: env:: current_dir ( ) . expect ( "Couldn't read CWD" ) ,
97+ } ;
8698
8799 let has_errors = if cli. stdin {
88- format_stdin ( & cli, & config)
100+ format_stdin ( & cli, & config, & dir_path )
89101 } else {
90- format_fs ( & cli, & config)
102+ format_fs ( & cli, & config, & dir_path )
91103 } ;
92104
93105 if cli. check && has_errors {
0 commit comments