1use serde::{Deserialize, Serialize};
16use std::collections::HashMap;
17use std::path::Path;
18use std::string::String;
19
20use std::fmt;
21use tokio::{fs, io};
22
23#[derive(Serialize, Deserialize, Clone)]
24pub struct GitConfig {
25 pub host: String,
26 pub port: u16,
27 pub username: String,
28 pub private_key: String,
29 pub repository: String,
30 pub branch: String,
31}
32
33#[derive(Serialize, Deserialize, Clone)]
34pub struct SshConfig {
35 pub host: String,
36 pub port: u16,
37 pub username: String,
38 pub private_key: String,
39}
40
41#[derive(Serialize, Deserialize)]
42pub struct AwsConfig {
43 pub region: String,
44 pub endpoint: Option<String>,
45 pub access_key: String,
46 pub secret_key: String,
47 pub force_path_style: Option<bool>,
48}
49
50#[derive(Serialize, Deserialize)]
51pub struct GCloudConfig {
52 pub service_account_path: String,
53}
54
55#[derive(Serialize, Deserialize)]
56pub struct PostgreSqlConfig {
57 pub username: String,
58 pub db_name: String,
59 pub host: Option<String>,
60 pub port: Option<u16>,
61}
62
63#[derive(Serialize, Deserialize)]
64pub struct DockerConfig {
65 pub container_name: String,
66 pub command: String,
67}
68
69#[derive(Serialize, Deserialize)]
70pub struct FoldersConfig {
71 pub pattern: String,
72}
73
74#[derive(Serialize, Deserialize, Clone)]
75pub struct BackupConfig {
76 pub what: String,
77 pub r#where: String,
78 pub when: String,
79 pub remote_path: String,
80 pub compress: bool,
81 pub keep_last: Option<u32>,
82}
83
84#[derive(Serialize, Deserialize)]
85pub struct LocalhostConfig {
86 pub path: String,
87}
88
89#[derive(Serialize, Deserialize)]
90pub struct Config {
91 pub aws: Option<HashMap<String, AwsConfig>>,
93 pub gcloud: Option<HashMap<String, GCloudConfig>>,
94 pub ssh: Option<HashMap<String, SshConfig>>,
95 pub git: Option<HashMap<String, GitConfig>>,
96 pub localhost: Option<HashMap<String, LocalhostConfig>>,
97 pub folders: Option<HashMap<String, FoldersConfig>>,
99 pub postgres: Option<HashMap<String, PostgreSqlConfig>>,
100 pub docker: Option<HashMap<String, DockerConfig>>,
101 pub backup: HashMap<String, BackupConfig>,
103}
104
105#[derive(Debug)]
106pub enum Error {
107 Open(io::Error),
108 Parse(toml::de::Error),
109}
110
111impl std::error::Error for Error {}
112impl fmt::Display for Error {
113 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114 match self {
115 Error::Open(error) => write!(f, "Could not open/read config: {}", error),
116 Error::Parse(error) => write!(f, "Failed to parse config: {}", error),
117 }
118 }
119}
120
121impl From<io::Error> for Error {
122 fn from(error: io::Error) -> Self {
123 Error::Open(error)
124 }
125}
126
127impl From<toml::de::Error> for Error {
128 fn from(error: toml::de::Error) -> Self {
129 Error::Parse(error)
130 }
131}
132
133impl Config {
134 pub async fn new(path: &Path) -> Result<Config, Error> {
135 let txt = fs::read_to_string(path).await?;
136 let config: Config = toml::from_str(&txt)?;
137 Ok(config)
138 }
139}