1
1
use std:: fmt:: { self , Debug } ;
2
- use std:: fs:: File ;
2
+ use std:: fs:: { self , File } ;
3
3
4
- use dnrs:: { Config , FileConfig , RuntimeError , run, setup_logger} ;
4
+ use dnrs:: { Config , RuntimeError , run, setup_logger} ;
5
5
use lum_config:: { ConfigPathError , EnvironmentConfigParseError , FileConfigParseError , merge} ;
6
- use lum_log:: { debug, error, log:: SetLoggerError } ;
6
+ use lum_log:: info;
7
+ use lum_log:: { error, log:: SetLoggerError } ;
7
8
use thiserror:: Error ;
8
9
9
10
/*
@@ -57,9 +58,7 @@ enum Error {
57
58
#[ error( "IO error: {0}" ) ]
58
59
Io ( #[ from] std:: io:: Error ) ,
59
60
60
- #[ error(
61
- "Unable to determine config directory. No config directory, home directory, or temp directory available."
62
- ) ]
61
+ #[ error( "Unable to determine config directory" ) ]
63
62
NoConfigDirectory ,
64
63
65
64
#[ error( "Runtime error: {0}" ) ]
@@ -78,31 +77,32 @@ async fn main() -> Result<(), Error> {
78
77
setup_logger ( ) ?;
79
78
80
79
let config_path = dirs:: config_dir ( )
81
- . or_else ( || dirs:: home_dir ( ) )
82
- . or_else ( || Some ( std:: env:: temp_dir ( ) ) )
83
80
. ok_or ( Error :: NoConfigDirectory ) ?
84
81
. join ( APP_NAME )
85
82
. join ( "config.yaml" ) ;
86
83
87
- let file_config: FileConfig = if config_path. exists ( ) {
84
+ let mut loaded_config: Option < Config > = None ;
85
+ if config_path. exists ( ) {
88
86
let file = File :: open ( & config_path) ?;
89
- serde_yaml:: from_reader ( file) ?
90
- } else {
91
- let default_config = FileConfig :: default ( ) ;
92
-
93
- if let Some ( parent) = config_path. parent ( ) {
94
- std:: fs:: create_dir_all ( parent) ?;
95
- }
96
-
97
- let file = File :: create ( & config_path) ?;
98
- serde_yaml:: to_writer ( file, & default_config) ?;
87
+ loaded_config = Some ( serde_yaml:: from_reader ( file) ?) ;
88
+ }
89
+ let config_existed = loaded_config. is_some ( ) ;
99
90
100
- debug ! ( "Created default config file at: {}" , config_path. display( ) ) ;
101
- default_config
91
+ let config = Config :: default ( ) ;
92
+ let config = match loaded_config {
93
+ Some ( loaded_config) => merge ( config, loaded_config) ,
94
+ None => config,
102
95
} ;
103
96
104
- let config = Config :: default ( ) ;
105
- let config = merge ( config, file_config) ;
97
+ if let Some ( parent) = config_path. parent ( ) {
98
+ fs:: create_dir_all ( parent) ?;
99
+ }
100
+ let file = File :: create ( & config_path) ?;
101
+ serde_yaml:: to_writer ( file, & config) ?;
102
+
103
+ if !config_existed {
104
+ info ! ( "Created default config file at: {}" , config_path. display( ) ) ;
105
+ }
106
106
107
107
run ( config) . await ?;
108
108
Ok ( ( ) )
0 commit comments