-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathsetup.rs
93 lines (82 loc) · 3.1 KB
/
setup.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
//! Submodule for handling setup commands
//!
//! Setting up refinery is fairly straight forward. We
//! create a configuration with default values, a migrations
//! directory and also an initial migration (for the db type
//! chosen) that contains metadata that refinery will use
//! for future migrations.
//!
//! This process can be adjusted by user flags. All of this
//! is mirrored in the refinery configuration file stored
//! in `refinery.toml` in the crate root.
//!
//! When running the setup *again* when a configuration file
//! is already present, the fields from it can be used to
//! override otherwise default values
use anyhow::{anyhow, Result};
use refinery_core::config::{Config, ConfigDbType};
use std::fs::File;
use std::io::{self, Write};
/// Do everything that the module docs promise. And more ✨
pub fn handle_setup() -> Result<()> {
let cfg = get_config_from_input()?;
let s = toml::to_string(&cfg)?;
let mut file = File::create("./refinery.toml").unwrap();
file.write_all(s.as_bytes()).ok();
Ok(())
}
fn get_config_from_input() -> Result<Config> {
println!("Select database 1) Mysql 2) Postgresql 3) Sqlite 4) Mssql: ");
print!("Enter a number: ");
io::stdout().flush()?;
let mut db_type = String::new();
io::stdin().read_line(&mut db_type)?;
let db_type = match db_type.trim() {
"1" => ConfigDbType::Mysql,
"2" => ConfigDbType::Postgres,
"3" => ConfigDbType::Sqlite,
"4" => ConfigDbType::Mssql,
_ => return Err(anyhow!("invalid option")),
};
let mut config = Config::new(db_type);
if config.db_type() == ConfigDbType::Sqlite {
cfg_if::cfg_if! {
if #[cfg(feature = "sqlite")] {
print!("Enter database path: ");
io::stdout().flush()?;
let mut db_path = String::new();
io::stdin().read_line(&mut db_path)?;
config = config.set_db_path(db_path.trim());
return Ok(config);
} else {
panic!("tried to migrate async from config for a sqlite database, but sqlite feature was not enabled!");
}
}
}
print!("Enter database host: ");
io::stdout().flush()?;
let mut db_host = String::new();
io::stdin().read_line(&mut db_host)?;
config = config.set_db_host(db_host.trim());
print!("Enter database port: ");
io::stdout().flush()?;
let mut db_port = String::new();
io::stdin().read_line(&mut db_port)?;
config = config.set_db_port(db_port.trim());
print!("Enter database username: ");
io::stdout().flush()?;
let mut db_user = String::new();
io::stdin().read_line(&mut db_user)?;
config = config.set_db_user(db_user.trim());
print!("Enter database password: ");
io::stdout().flush()?;
let mut db_pass = String::new();
io::stdin().read_line(&mut db_pass)?;
config = config.set_db_pass(db_pass.trim());
print!("Enter database name: ");
io::stdout().flush()?;
let mut db_name = String::new();
io::stdin().read_line(&mut db_name)?;
config = config.set_db_name(db_name.trim());
Ok(config)
}