11extern crate log;
22
3- use clap:: { Arg , Command } ;
3+ use clap:: { Parser , Subcommand } ;
44use cursive:: { Cursive , CursiveExt , event:: Key , views:: Dialog } ;
5+ use std:: path:: PathBuf ;
56
67use jkconfig:: {
78 data:: AppData ,
@@ -11,35 +12,69 @@ use jkconfig::{
1112// mod menu_view;
1213// use menu_view::MenuView;
1314
15+ /// 命令行参数结构体
16+ #[ derive( Parser ) ]
17+ #[ command( name = "jkconfig" ) ]
18+ #[ command( author = "周睿 <[email protected] >" ) ] 19+ #[ command( about = "配置编辑器" , long_about = None ) ]
20+ struct Cli {
21+ /// config file path
22+ #[ arg( short = 'c' , long = "config" , default_value = ".config.toml" ) ]
23+ config : PathBuf ,
24+
25+ /// schema file path, default is config file name with '-schema.json' suffix
26+ #[ arg( short = 's' , long = "schema" ) ]
27+ schema : Option < PathBuf > ,
28+
29+ /// 子命令
30+ #[ command( subcommand) ]
31+ command : Option < Commands > ,
32+ }
33+
34+ /// 子命令枚举
35+ #[ derive( Subcommand ) ]
36+ enum Commands {
37+ /// TUI (default)
38+ Tui ,
39+ /// Web UI mode
40+ Web {
41+ /// server port
42+ #[ arg( short = 'p' , long = "port" , default_value = "3000" ) ]
43+ port : u16 ,
44+ } ,
45+ }
46+
1447/// 主函数
1548fn main ( ) -> anyhow:: Result < ( ) > {
1649 // 解析命令行参数
17- let matches = Command :: new ( "jkconfig" )
18- 19- . arg (
20- Arg :: new ( "config" )
21- . long ( "config" )
22- . short ( 'c' )
23- . value_name ( "FILE" )
24- . help ( "指定初始配置文件路径" )
25- . default_value ( ".project.toml" ) ,
26- )
27- . arg (
28- Arg :: new ( "schema" )
29- . long ( "schema" )
30- . short ( 's' )
31- . value_name ( "FILE" )
32- . help ( "指定schema文件路径(默认基于配置文件名推导)" ) ,
33- )
34- . get_matches ( ) ;
50+ let cli = Cli :: parse ( ) ;
3551
3652 // 提取命令行参数
37- let config_file = matches. get_one :: < String > ( "config" ) . map ( |s| s. as_str ( ) ) ;
38- let schema_file = matches. get_one :: < String > ( "schema" ) . map ( |s| s. as_str ( ) ) ;
53+ let config_path = cli. config . to_string_lossy ( ) . to_string ( ) ;
54+ let schema_path = cli. schema . as_ref ( ) . map ( |p| p. to_string_lossy ( ) . to_string ( ) ) ;
55+
56+ let config_file = Some ( config_path. as_str ( ) ) ;
57+ let schema_file = schema_path. as_deref ( ) ;
3958
4059 // 初始化AppData
4160 let app_data = AppData :: new ( config_file, schema_file) ?;
4261
62+ // 根据子命令决定运行模式
63+ match cli. command {
64+ Some ( Commands :: Web { port } ) => {
65+ tokio:: runtime:: Runtime :: new ( ) ?. block_on ( jkconfig:: web:: run_server ( app_data, port) ) ?;
66+ }
67+ Some ( Commands :: Tui ) | None => {
68+ // 运行TUI界面(默认行为)
69+ run_tui ( app_data) ?;
70+ }
71+ }
72+
73+ Ok ( ( ) )
74+ }
75+
76+ /// 运行TUI界面
77+ fn run_tui ( app_data : AppData ) -> anyhow:: Result < ( ) > {
4378 let title = app_data. root . title . clone ( ) ;
4479 let fields = app_data. root . menu ( ) . fields ( ) ;
4580
0 commit comments