-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rs
145 lines (121 loc) · 3.59 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::{
fs,
io::{self, Write},
path::PathBuf,
};
#[derive(Deserialize, Serialize)]
pub struct Postgres {
#[serde(default)]
pub host: String,
#[serde(default)]
pub password: String,
#[serde(default)]
pub port: i32,
#[serde(default)]
pub username: String,
#[serde(default)]
pub database: String,
}
impl Postgres {
pub fn connection_string(&self) -> String {
format!(
"host={} user={} password={} port={} dbname={}",
self.host, self.username, self.password, self.port, self.database
)
}
}
#[derive(Deserialize, Serialize)]
pub struct Migrations {
#[serde(default)]
pub dir_path: String,
}
#[derive(Deserialize, Serialize)]
pub struct Config {
#[serde(default)]
pub migrations: Migrations,
#[serde(default)]
pub postgres: Postgres,
}
impl Config {
pub fn from_file(path: PathBuf) -> Result<Config> {
let contents = fs::read_to_string(&path).with_context(|| {
format!(
"Failed to find seagull.toml at {}. Run `seagull init` to create it.",
&path.display()
)
})?;
let config: Config = toml::from_str(&contents)?;
Ok(config)
}
pub fn from_input() -> Result<Config> {
let mut config = Config::default();
print!("What's your database host? (localhost) ");
io::stdout().flush()?;
let mut host = String::new();
io::stdin().read_line(&mut host)?;
print!("What's your database username? (postgres) ");
io::stdout().flush()?;
let mut username = String::new();
io::stdin().read_line(&mut username)?;
print!("What's your database password? ");
io::stdout().flush()?;
let mut password = String::new();
io::stdin().read_line(&mut password)?;
print!("What's your database port? (5432) ");
io::stdout().flush()?;
let mut port_string = String::new();
io::stdin().read_line(&mut port_string)?;
if !port_string.trim().is_empty() {
let port: i32 = port_string
.trim()
.parse()
.expect("Please enter a positive port number");
config.postgres.port = port;
}
print!("What's your database name? (postgres) ");
io::stdout().flush()?;
let mut database = String::new();
io::stdin().read_line(&mut database)?;
if !host.trim().is_empty() {
config.postgres.host = host.trim().to_string();
};
if !username.trim().is_empty() {
config.postgres.username = username.trim().to_string();
}
if !password.trim().is_empty() {
config.postgres.password = password.trim().to_string();
}
if !database.trim().is_empty() {
config.postgres.database = database.trim().to_string()
}
Ok(config)
}
}
impl Default for Config {
fn default() -> Self {
Config {
migrations: Migrations::default(),
postgres: Postgres::default(),
}
}
}
impl Default for Postgres {
fn default() -> Self {
Postgres {
host: String::from("localhost"),
username: String::from("postgres"),
password: String::from("postgres"),
port: 5432,
database: String::from("postgres"),
}
}
}
impl Default for Migrations {
fn default() -> Self {
Migrations {
dir_path: String::from("migrations"),
}
}
}