1- use clap:: Parser ;
21use sqlpage:: {
3- app_config,
2+ app_config:: AppConfig ,
3+ cli,
44 webserver:: { self , Database } ,
55 AppState ,
66} ;
@@ -15,20 +15,11 @@ async fn main() {
1515}
1616
1717async fn start ( ) -> anyhow:: Result < ( ) > {
18- let app_config = app_config :: load_from_cli ( ) ?;
19- let cli = app_config :: Cli :: parse ( ) ;
18+ let cli = cli :: arguments :: parse_cli ( ) ?;
19+ let app_config = AppConfig :: from_cli ( & cli ) ? ;
2020
2121 if let Some ( command) = cli. command {
22- match command {
23- app_config:: Commands :: CreateMigration { migration_name } => {
24- // Pass configuration_directory from app_config
25- create_migration_file (
26- & migration_name,
27- app_config. configuration_directory . to_str ( ) . unwrap ( ) ,
28- ) ?;
29- return Ok ( ( ) ) ;
30- }
31- }
22+ return command. execute ( app_config) . await ;
3223 }
3324
3425 let db = Database :: init ( & app_config) . await ?;
@@ -58,52 +49,3 @@ fn init_logging() {
5849 Err ( e) => log:: error!( "Error loading .env file: {e}" ) ,
5950 }
6051}
61-
62- fn create_migration_file (
63- migration_name : & str ,
64- configuration_directory : & str ,
65- ) -> anyhow:: Result < ( ) > {
66- use chrono:: Utc ;
67- use std:: fs;
68- use std:: path:: Path ;
69-
70- let timestamp = Utc :: now ( ) . format ( "%Y%m%d%H%M%S" ) . to_string ( ) ;
71- let snake_case_name = migration_name
72- . replace ( |c : char | !c. is_alphanumeric ( ) , "_" )
73- . to_lowercase ( ) ;
74- let file_name = format ! ( "{}_{}.sql" , timestamp, snake_case_name) ;
75- let migrations_dir = Path :: new ( configuration_directory) . join ( "migrations" ) ;
76-
77- if !migrations_dir. exists ( ) {
78- fs:: create_dir_all ( & migrations_dir) ?;
79- }
80-
81- let mut unique_file_name = file_name. clone ( ) ;
82- let mut counter = 1 ;
83-
84- while migrations_dir. join ( & unique_file_name) . exists ( ) {
85- unique_file_name = format ! ( "{}_{}_{}.sql" , timestamp, snake_case_name, counter) ;
86- counter += 1 ;
87- }
88-
89- let file_path = migrations_dir. join ( unique_file_name) ;
90- fs:: write ( & file_path, "-- Write your migration here\n " ) ?;
91-
92- // the following code cleans up the display path to show where the migration was created
93- // relative to the current working directory, and then outputs the path to the migration
94- let file_path_canon = file_path. canonicalize ( ) . unwrap_or ( file_path. clone ( ) ) ;
95- let cwd_canon = std:: env:: current_dir ( ) ?
96- . canonicalize ( )
97- . unwrap_or ( std:: env:: current_dir ( ) ?) ;
98- let rel_path = match file_path_canon. strip_prefix ( & cwd_canon) {
99- Ok ( p) => p,
100- Err ( _) => file_path_canon. as_path ( ) ,
101- } ;
102- let mut display_path_str = rel_path. display ( ) . to_string ( ) ;
103- if display_path_str. starts_with ( "\\ \\ ?\\ " ) {
104- display_path_str = display_path_str. trim_start_matches ( "\\ \\ ?\\ " ) . to_string ( ) ;
105- }
106- display_path_str = display_path_str. replace ( '\\' , "/" ) ;
107- println ! ( "Migration file created: {}" , display_path_str) ;
108- Ok ( ( ) )
109- }
0 commit comments