1+ mod args;
2+ mod cli;
13mod commands;
24
3- use crate :: commands:: Serve ;
4- use anyhow:: Result ;
5- use clap:: { Parser , Subcommand } ;
65use pyo3:: prelude:: * ;
76use std:: env;
87use std:: process:: ExitCode ;
98
10- #[ derive( Parser ) ]
11- #[ command( name = "djls" ) ]
12- #[ command( version, about, long_about = None ) ]
13- pub struct Cli {
14- #[ command( subcommand) ]
15- command : Command ,
16-
17- #[ command( flatten) ]
18- args : Args ,
19- }
20-
21- #[ derive( Debug , Subcommand ) ]
22- enum Command {
23- /// Start the LSP server
24- Serve ( Serve ) ,
25- }
26-
27- #[ derive( Parser ) ]
28- pub struct Args {
29- #[ command( flatten) ]
30- global : GlobalArgs ,
31- }
32-
33- #[ derive( Parser , Debug , Clone ) ]
34- struct GlobalArgs {
35- /// Do not print any output.
36- #[ arg( global = true , long, short, conflicts_with = "verbose" ) ]
37- pub quiet : bool ,
38-
39- /// Use verbose output.
40- #[ arg( global = true , action = clap:: ArgAction :: Count , long, short, conflicts_with = "quiet" ) ]
41- pub verbose : u8 ,
42- }
9+ pub use cli:: Cli ;
4310
4411#[ pyfunction]
4512fn entrypoint ( _py : Python ) -> PyResult < ( ) > {
@@ -48,62 +15,28 @@ fn entrypoint(_py: Python) -> PyResult<()> {
4815 . chain ( env:: args ( ) . skip ( 2 ) )
4916 . collect ( ) ;
5017
51- let runtime = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
52- let local = tokio:: task:: LocalSet :: new ( ) ;
53- local. block_on ( & runtime, async move {
54- tokio:: select! {
55- // The main CLI program
56- result = main( args) => {
57- match result {
58- Ok ( code) => {
59- if code != ExitCode :: SUCCESS {
60- std:: process:: exit( 1 ) ;
61- }
62- Ok :: <( ) , PyErr >( ( ) )
63- }
64- Err ( e) => {
65- eprintln!( "Error: {}" , e) ;
66- if let Some ( source) = e. source( ) {
67- eprintln!( "Caused by: {}" , source) ;
68- }
69- std:: process:: exit( 1 ) ;
70- }
71- }
72- }
73- // Ctrl+C handling
74- _ = tokio:: signal:: ctrl_c( ) => {
75- println!( "\n Received Ctrl+C, shutting down..." ) ;
76- // Cleanup code here if needed
77- std:: process:: exit( 130 ) ; // Standard Ctrl+C exit code
18+ let runtime = tokio:: runtime:: Builder :: new_multi_thread ( )
19+ . enable_all ( )
20+ . build ( )
21+ . unwrap ( ) ;
22+
23+ let result = runtime. block_on ( cli:: run ( args) ) ;
24+
25+ match result {
26+ Ok ( code) => {
27+ if code != ExitCode :: SUCCESS {
28+ std:: process:: exit ( 1 ) ;
7829 }
79- // SIGTERM handling (Unix only)
80- _ = async {
81- #[ cfg( unix) ]
82- {
83- use tokio:: signal:: unix:: { signal, SignalKind } ;
84- let mut term = signal( SignalKind :: terminate( ) ) . unwrap( ) ;
85- term. recv( ) . await ;
86- }
87- } => {
88- println!( "\n Received termination signal, shutting down..." ) ;
89- std:: process:: exit( 143 ) ; // Standard SIGTERM exit code
30+ Ok ( ( ) )
31+ }
32+ Err ( e) => {
33+ eprintln ! ( "Error: {}" , e) ;
34+ if let Some ( source) = e. source ( ) {
35+ eprintln ! ( "Caused by: {}" , source) ;
9036 }
37+ std:: process:: exit ( 1 ) ;
9138 }
92- } ) ?;
93-
94- Ok ( ( ) )
95- }
96-
97- async fn main ( args : Vec < String > ) -> Result < ExitCode > {
98- let cli = Cli :: try_parse_from ( args) . unwrap_or_else ( |e| {
99- e. exit ( ) ;
100- } ) ;
101-
102- match cli. command {
103- Command :: Serve ( _serve) => djls_server:: serve ( ) . await ?,
10439 }
105-
106- Ok ( ExitCode :: SUCCESS )
10740}
10841
10942#[ pymodule]
0 commit comments