Browse Source

Add option --list-paths

Johannes Hofmann 7 years ago
parent
commit
d3c51824af
3 changed files with 74 additions and 12 deletions
  1. 4
    0
      src/args.rs
  2. 61
    11
      src/config.rs
  3. 9
    1
      src/main.rs

+ 4
- 0
src/args.rs View File

13
             .value_name("FILE")
13
             .value_name("FILE")
14
             .help("Set a custom config file")
14
             .help("Set a custom config file")
15
             .takes_value(true))
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
         .arg(Arg::with_name("tile-sources")
20
         .arg(Arg::with_name("tile-sources")
17
             .short("t")
21
             .short("t")
18
             .long("tile-sources")
22
             .long("tile-sources")

+ 61
- 11
src/config.rs View File

18
 
18
 
19
 #[derive(Debug)]
19
 #[derive(Debug)]
20
 pub struct Config {
20
 pub struct Config {
21
+    config_file_path: Option<PathBuf>,
22
+    tile_sources_file_path: Option<PathBuf>,
21
     tile_cache_dir: PathBuf,
23
     tile_cache_dir: PathBuf,
22
     sources: Vec<(String, TileSource)>,
24
     sources: Vec<(String, TileSource)>,
23
     pbf_path: Option<PathBuf>,
25
     pbf_path: Option<PathBuf>,
94
                 &config_file,
96
                 &config_file,
95
                 DEFAULT_CONFIG.as_bytes()
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
                 &sources_file,
129
                 &sources_file,
123
                 DEFAULT_TILE_SOURCES.as_bytes()
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
                 let pbf_path = {
168
                 let pbf_path = {
157
                     match table.get("pbf_file") {
169
                     match table.get("pbf_file") {
158
                         Some(&Value::String(ref pbf_file)) => {
170
                         Some(&Value::String(ref pbf_file)) => {
159
-                            match config_path {
171
+                            match config_path.as_ref() {
160
                                 Some(config_path) => {
172
                                 Some(config_path) => {
161
                                     let p = config_path.as_ref().parent()
173
                                     let p = config_path.as_ref().parent()
162
                                         .ok_or_else(|| "root path is not a valid config file.")?;
174
                                         .ok_or_else(|| "root path is not a valid config file.")?;
211
 
223
 
212
                 Ok(
224
                 Ok(
213
                     Config {
225
                     Config {
226
+                        config_file_path: config_path.map(|p| PathBuf::from(p.as_ref())),
227
+                        tile_sources_file_path: None,
214
                         tile_cache_dir,
228
                         tile_cache_dir,
215
                         sources: vec![],
229
                         sources: vec![],
216
                         pbf_path,
230
                         pbf_path,
236
         Config::from_toml_str(&content, Some(path))
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
         match toml_str.parse::<Value>() {
260
         match toml_str.parse::<Value>() {
241
             Ok(Value::Table(ref table)) => {
261
             Ok(Value::Table(ref table)) => {
242
                 let sources_array = table.get("tile_sources")
262
                 let sources_array = table.get("tile_sources")
312
                         )?,
332
                         )?,
313
                     ));
333
                     ));
314
                 }
334
                 }
335
+
336
+                self.tile_sources_file_path = file_path.map(|p| PathBuf::from(p.as_ref()));
315
                 Ok(())
337
                 Ok(())
316
             },
338
             },
317
             Ok(_) => Err("TOML file has invalid structure. Expected a Table as the top-level element.".to_string()),
339
             Ok(_) => Err("TOML file has invalid structure. Expected a Table as the top-level element.".to_string()),
320
     }
342
     }
321
 
343
 
322
     fn add_tile_sources_from_file<P: AsRef<Path>>(&mut self, path: P) -> Result<(), String> {
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
         let mut content = String::new();
347
         let mut content = String::new();
326
         file.read_to_string(&mut content).map_err(|e| format!("{}", e))?;
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
     pub fn tile_sources(&self) -> &[(String, TileSource)] {
381
     pub fn tile_sources(&self) -> &[(String, TileSource)] {
408
     #[test]
458
     #[test]
409
     fn default_config() {
459
     fn default_config() {
410
         let mut config = Config::from_toml_str::<&str>(DEFAULT_CONFIG, None).unwrap();
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 View File

213
 }
213
 }
214
 
214
 
215
 fn run() -> Result<(), Box<Error>> {
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
     let mut sources = TileSources::new(config.tile_sources())
226
     let mut sources = TileSources::new(config.tile_sources())
219
         .ok_or_else(|| "no tile sources provided.")?;
227
         .ok_or_else(|| "no tile sources provided.")?;