Sfoglia il codice sorgente

Add option --list-paths

Johannes Hofmann 7 anni fa
parent
commit
d3c51824af
3 ha cambiato i file con 74 aggiunte e 12 eliminazioni
  1. 4
    0
      src/args.rs
  2. 61
    11
      src/config.rs
  3. 9
    1
      src/main.rs

+ 4
- 0
src/args.rs Vedi File

@@ -13,6 +13,10 @@ pub fn parse<'a>() -> clap::ArgMatches<'a> {
13 13
             .value_name("FILE")
14 14
             .help("Set a custom config file")
15 15
             .takes_value(true))
16
+        .arg(Arg::with_name("list-paths")
17
+            .long("list-paths")
18
+            .help("Print paths of configuration files and directories \
19
+                and exit the program."))
16 20
         .arg(Arg::with_name("tile-sources")
17 21
             .short("t")
18 22
             .long("tile-sources")

+ 61
- 11
src/config.rs Vedi File

@@ -18,6 +18,8 @@ lazy_static! {
18 18
 
19 19
 #[derive(Debug)]
20 20
 pub struct Config {
21
+    config_file_path: Option<PathBuf>,
22
+    tile_sources_file_path: Option<PathBuf>,
21 23
     tile_cache_dir: PathBuf,
22 24
     sources: Vec<(String, TileSource)>,
23 25
     pbf_path: Option<PathBuf>,
@@ -94,11 +96,16 @@ impl Config {
94 96
                 &config_file,
95 97
                 DEFAULT_CONFIG.as_bytes()
96 98
             ) {
97
-                Err(err) => warn!("{}", err),
98
-                Ok(()) => info!("create default config file {:?}", config_file),
99
+                Err(err) => {
100
+                    warn!("{}", err);
101
+                    Config::from_toml_str::<&str>(DEFAULT_CONFIG, None)
102
+                },
103
+                Ok(()) => {
104
+                    info!("create default config file {:?}", config_file);
105
+                    Config::from_toml_str(DEFAULT_CONFIG, Some(config_file))
106
+                },
99 107
             }
100 108
 
101
-            Config::from_toml_str::<&str>(DEFAULT_CONFIG, None)
102 109
         }
103 110
     }
104 111
 
@@ -122,11 +129,16 @@ impl Config {
122 129
                 &sources_file,
123 130
                 DEFAULT_TILE_SOURCES.as_bytes()
124 131
             ) {
125
-                Err(err) => warn!("{}", err),
126
-                Ok(()) => info!("create default tile sources file {:?}", sources_file),
132
+                Err(err) => {
133
+                    warn!("{}", err);
134
+                    self.add_tile_sources_from_str::<&str>(DEFAULT_TILE_SOURCES, None)
135
+                },
136
+                Ok(()) => {
137
+                    info!("create default tile sources file {:?}", sources_file);
138
+                    self.add_tile_sources_from_str(DEFAULT_TILE_SOURCES, Some(sources_file))
139
+                },
127 140
             }
128 141
 
129
-            self.add_tile_sources_from_str(DEFAULT_TILE_SOURCES)
130 142
         }
131 143
     }
132 144
 
@@ -156,7 +168,7 @@ impl Config {
156 168
                 let pbf_path = {
157 169
                     match table.get("pbf_file") {
158 170
                         Some(&Value::String(ref pbf_file)) => {
159
-                            match config_path {
171
+                            match config_path.as_ref() {
160 172
                                 Some(config_path) => {
161 173
                                     let p = config_path.as_ref().parent()
162 174
                                         .ok_or_else(|| "root path is not a valid config file.")?;
@@ -211,6 +223,8 @@ impl Config {
211 223
 
212 224
                 Ok(
213 225
                     Config {
226
+                        config_file_path: config_path.map(|p| PathBuf::from(p.as_ref())),
227
+                        tile_sources_file_path: None,
214 228
                         tile_cache_dir,
215 229
                         sources: vec![],
216 230
                         pbf_path,
@@ -236,7 +250,13 @@ impl Config {
236 250
         Config::from_toml_str(&content, Some(path))
237 251
     }
238 252
 
239
-    fn add_tile_sources_from_str(&mut self, toml_str: &str) -> Result<(), String> {
253
+    fn add_tile_sources_from_str<P>(
254
+        &mut self,
255
+        toml_str: &str,
256
+        file_path: Option<P>
257
+        ) -> Result<(), String>
258
+        where P: AsRef<Path>
259
+    {
240 260
         match toml_str.parse::<Value>() {
241 261
             Ok(Value::Table(ref table)) => {
242 262
                 let sources_array = table.get("tile_sources")
@@ -312,6 +332,8 @@ impl Config {
312 332
                         )?,
313 333
                     ));
314 334
                 }
335
+
336
+                self.tile_sources_file_path = file_path.map(|p| PathBuf::from(p.as_ref()));
315 337
                 Ok(())
316 338
             },
317 339
             Ok(_) => Err("TOML file has invalid structure. Expected a Table as the top-level element.".to_string()),
@@ -320,12 +342,40 @@ impl Config {
320 342
     }
321 343
 
322 344
     fn add_tile_sources_from_file<P: AsRef<Path>>(&mut self, path: P) -> Result<(), String> {
323
-        let mut file = File::open(path).map_err(|e| format!("{}", e))?;
345
+        let mut file = File::open(&path).map_err(|e| format!("{}", e))?;
324 346
 
325 347
         let mut content = String::new();
326 348
         file.read_to_string(&mut content).map_err(|e| format!("{}", e))?;
327 349
 
328
-        self.add_tile_sources_from_str(&content)
350
+        self.add_tile_sources_from_str(&content, Some(path))
351
+    }
352
+
353
+    pub fn list_paths(&self) {
354
+        let config = match self.config_file_path.as_ref() {
355
+            Some(path) => format!("{:?}", path),
356
+            None => "<None>".to_string(),
357
+        };
358
+
359
+        let sources = match self.tile_sources_file_path.as_ref() {
360
+            Some(path) => format!("{:?}", path),
361
+            None => "<None>".to_string(),
362
+        };
363
+
364
+        let pbf = match self.pbf_path.as_ref() {
365
+            Some(path) => format!("{:?}", path),
366
+            None => "<None>".to_string(),
367
+        };
368
+
369
+        println!("\
370
+            main configuration file: {}\n\
371
+            tile sources file:       {}\n\
372
+            tile cache directory:    {:?}\n\
373
+            OSM PBF file:            {}",
374
+            config,
375
+            sources,
376
+            self.tile_cache_dir,
377
+            pbf,
378
+        );
329 379
     }
330 380
 
331 381
     pub fn tile_sources(&self) -> &[(String, TileSource)] {
@@ -408,6 +458,6 @@ mod tests {
408 458
     #[test]
409 459
     fn default_config() {
410 460
         let mut config = Config::from_toml_str::<&str>(DEFAULT_CONFIG, None).unwrap();
411
-        config.add_tile_sources_from_str(DEFAULT_TILE_SOURCES).unwrap();
461
+        config.add_tile_sources_from_str(DEFAULT_TILE_SOURCES, None).unwrap();
412 462
     }
413 463
 }

+ 9
- 1
src/main.rs Vedi File

@@ -213,7 +213,15 @@ fn dur_to_sec(dur: Duration) -> f64 {
213 213
 }
214 214
 
215 215
 fn run() -> Result<(), Box<Error>> {
216
-    let config = config::Config::from_arg_matches(&args::parse())?;
216
+    let config = {
217
+        let arg_matches = args::parse();
218
+        let config = config::Config::from_arg_matches(&arg_matches)?;
219
+        if arg_matches.is_present("list-paths") {
220
+            config.list_paths();
221
+            return Ok(());
222
+        }
223
+        config
224
+    };
217 225
 
218 226
     let mut sources = TileSources::new(config.tile_sources())
219 227
         .ok_or_else(|| "no tile sources provided.")?;