]>
Commit | Line | Data |
---|---|---|
d620665f RBR |
1 | use std::env; |
2 | use std::path::PathBuf; | |
3 | ||
4 | pub struct Configuration { | |
5 | // Default Base Directories, default to XDG dirs but can be | |
6 | // configured by users. | |
7 | pub config_directory: PathBuf, | |
8 | pub data_directory: PathBuf, | |
9 | pub output_directory: PathBuf, | |
10 | ||
11 | pub max_posts: u8, | |
12 | ||
13 | // Derived directories: Config | |
14 | pub remote_config: PathBuf, | |
15 | ||
16 | // Derived directories: Data | |
17 | pub posts_directory: PathBuf, | |
18 | pub archive_directory: PathBuf, | |
19 | pub static_directory: PathBuf, | |
20 | pub templates_directory: PathBuf, | |
21 | ||
22 | // Derived directories: Output | |
23 | pub blog_output_directory: PathBuf, | |
24 | pub archive_output_directory: PathBuf | |
25 | } | |
26 | ||
27 | impl Configuration { | |
28 | ||
29 | pub fn new() -> Self { | |
30 | let config_directory = Configuration::directory( | |
31 | "BLOG_CONFIG_DIRECTORY", | |
32 | "XDG_CONFIG_HOME", | |
33 | ".config", | |
34 | "blog" | |
35 | ); | |
36 | let data_directory = Configuration::directory( | |
37 | "BLOG_DATA_DIRECTORY", | |
38 | "XDG_DATA_HOME", | |
39 | ".local/share", | |
40 | "blog" | |
41 | ); | |
42 | let output_directory = Configuration::directory( | |
43 | "BLOG_OUTPUT_DIRECTORY", | |
44 | "XDG_CACHE_HOME", | |
45 | ".cache", | |
46 | "blog" | |
47 | ); | |
48 | ||
49 | let max_posts: u8 = env::var("BLOG_MAX_POSTS") | |
50 | .ok() | |
51 | .and_then(|v| v.parse().ok()) | |
52 | .unwrap_or(3); | |
53 | ||
54 | let remote_config = config_directory.join("blogremote"); | |
55 | ||
56 | let posts_directory = data_directory.join("posts"); | |
57 | let archive_directory = data_directory.join("archive"); | |
58 | let static_directory = data_directory.join("static"); | |
59 | let templates_directory = data_directory.join("templates"); | |
60 | ||
61 | let blog_output_directory = output_directory.join("blog"); | |
62 | let archive_output_directory = output_directory.join("archive"); | |
63 | ||
64 | Configuration { | |
65 | config_directory, | |
66 | data_directory, | |
67 | output_directory, | |
68 | max_posts, | |
69 | remote_config, | |
70 | posts_directory, | |
71 | archive_directory, | |
72 | static_directory, | |
73 | templates_directory, | |
74 | blog_output_directory, | |
75 | archive_output_directory | |
76 | } | |
77 | } | |
78 | ||
79 | fn directory(user_override: &str, default_value: &str, home_fallback: &str, path: &str) -> PathBuf { | |
80 | match env::var(user_override) { | |
81 | Ok(directory) => PathBuf::from(directory), | |
82 | Err(_) => match env::var(default_value) { | |
83 | Ok(directory) => PathBuf::from(directory), | |
84 | Err(_) => match env::var("HOME") { | |
85 | Ok(directory) => PathBuf::from(directory).join(home_fallback), | |
86 | Err(_) => panic!("Could not find required directory, {} or {} should be set and readable.", user_override, default_value), | |
87 | }, | |
88 | }, | |
89 | }.join(path) | |
90 | } | |
91 | } |