1use sec::Secret;
4use std::fs::File;
5use std::io::Read;
6use std::path::{Path, PathBuf};
7
8use failure::{Error, ResultExt};
9use toml;
10
11#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13pub struct Config {
14 pub general: General,
16 pub github: Option<GithubConfig>,
18 pub gitlab: Option<GitLabConfig>,
20}
21
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
24#[serde(rename_all = "kebab-case")]
25pub struct General {
26 pub dest_dir: PathBuf,
28}
29
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32#[serde(rename_all = "kebab-case")]
33pub struct GithubConfig {
34 pub api_key: Secret<String>,
40 #[serde(default = "always_true")]
42 pub starred: bool,
43 #[serde(default = "always_true")]
45 pub owned: bool,
46}
47
48#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
50#[serde(rename_all = "kebab-case")]
51#[allow(deprecated)]
52pub struct GitLabConfig {
53 pub api_key: Secret<String>,
58 #[serde(default = "default_gitlab_url")]
60 pub host: String,
61 #[serde(default = "always_false")]
64 pub organisations: bool,
65 #[serde(default = "always_true")]
67 pub owned: bool,
68}
69
70fn always_true() -> bool {
71 true
72}
73
74fn always_false() -> bool {
75 false
76}
77
78fn default_gitlab_url() -> String {
79 String::from("https://gitlab.com/")
80}
81
82impl Config {
83 pub fn from_file<P: AsRef<Path>>(file: P) -> Result<Config, Error> {
85 let file = file.as_ref();
86 debug!("Reading config from {}", file.display());
87
88 let mut buffer = String::new();
89 File::open(file)
90 .with_context(|_| format!("Unable to open {}", file.display()))?
91 .read_to_string(&mut buffer)
92 .context("Reading config file failed")?;
93
94 Config::from_str(&buffer)
95 }
96
97 pub fn from_str(src: &str) -> Result<Config, Error> {
99 toml::from_str(src)
100 .context("Parsing config file failed")
101 .map_err(Error::from)
102 }
103
104 pub fn example() -> Config {
106 Config {
107 general: General {
108 dest_dir: PathBuf::from("/srv"),
109 },
110 github: Some(GithubConfig {
111 api_key: String::from("your API key").into(),
112 owned: true,
113 starred: false,
114 }),
115 gitlab: Some(GitLabConfig {
116 api_key: String::from("your API key").into(),
117 host: String::from("gitlab.com"),
118 organisations: true,
119 owned: true,
120 }),
121 }
122 }
123
124 pub fn as_toml(&self) -> String {
126 match toml::to_string_pretty(self) {
127 Ok(s) => s,
128 Err(e) => {
129 panic!("Serializing a Config should never fail. {}", e);
130 }
131 }
132 }
133}