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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::common::cli::Cli;
use crate::common::dotenv::Dotenv;
use crate::common::types::DatabaseType;
use regex::Regex;
use serde;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DbConnectionConfig {
#[serde(rename = "DB_TYPE")]
pub db_type: DatabaseType,
#[serde(rename = "DB_HOST")]
pub db_host: String,
#[serde(rename = "DB_PORT")]
pub db_port: u16,
#[serde(rename = "DB_USER")]
pub db_user: String,
#[serde(rename = "DB_PASS")]
pub db_pass: Option<String>,
#[serde(rename = "DB_NAME")]
pub db_name: Option<String>,
}
#[derive(Clone, Debug)]
pub struct Config {
cli_args: Cli,
dotenv: Dotenv,
pub connections: HashMap<String, DbConnectionConfig>,
}
fn required_var_msg(key: &str) -> String {
format!(
"{} is not provided neither by an environment variable or CLI argument",
key
)
}
impl Config {
pub fn new(cli_args: Cli) -> Config {
let cli_args = &cli_args;
let dotenv = Dotenv::new();
Config {
dotenv: dotenv.clone(),
cli_args: cli_args.to_owned(),
connections: Self::build_connection_configs(&cli_args, &dotenv),
}
}
fn build_connection_configs(
cli_args: &Cli,
dotenv: &Dotenv,
) -> HashMap<String, DbConnectionConfig> {
let default_config_path = PathBuf::from_str(".sqlxrc.json").unwrap();
let file_config_path = &cli_args.config.clone().unwrap_or(default_config_path);
let file_based_config = fs::read_to_string(&file_config_path);
let mut connections: HashMap<String, DbConnectionConfig> = HashMap::new();
if let Ok(file_based_config) = file_based_config {
connections = serde_json::from_str(&file_based_config).unwrap();
}
connections.insert(
"default".to_string(),
Self::get_default_connection_config(&cli_args, &dotenv, &connections.get("default")),
);
connections
}
fn get_default_connection_config(
cli_args: &Cli,
dotenv: &Dotenv,
default_config: &Option<&DbConnectionConfig>,
) -> DbConnectionConfig {
let db_type = &cli_args
.db_type
.clone()
.or_else(|| dotenv.db_type.clone())
.or_else(|| default_config.map(|x| x.db_type.clone()))
.expect(
r"
Failed to fetch DB type.
Please provide it at least through a CLI arg or an environment variable or through
file based configuration
",
);
let db_host = &cli_args
.db_host
.clone()
.or_else(|| dotenv.db_host.clone())
.or_else(|| default_config.map(|x| x.db_host.clone()))
.expect(
r"
Failed to fetch DB host.
Please provide it at least through a CLI arg or an environment variable or through
file based configuration
",
);
let db_port = &cli_args
.db_port
.or_else(|| dotenv.db_port)
.or_else(|| default_config.map(|x| x.db_port))
.expect(
r"
Failed to fetch DB port.
Please provide it at least through a CLI arg or an environment variable or through
file based configuration
",
);
let db_user = &cli_args
.db_user
.clone()
.or_else(|| dotenv.db_user.clone())
.or_else(|| default_config.map(|x| x.db_user.clone()))
.expect(
r"
Failed to fetch DB user.
Please provide it at least through a CLI arg or an environment variable or through
file based configuration
",
);
let db_pass = &cli_args
.db_pass
.clone()
.or_else(|| dotenv.db_pass.clone())
.or_else(|| default_config.map(|x| x.db_pass.clone()).flatten());
let db_name = &cli_args
.db_name
.clone()
.or_else(|| dotenv.db_name.clone())
.or_else(|| default_config.map(|x| x.db_name.clone()).flatten());
DbConnectionConfig {
db_type: db_type.to_owned().to_owned(),
db_host: db_host.to_owned(),
db_port: db_port.to_owned(),
db_user: db_user.to_owned(),
db_pass: db_pass.to_owned(),
db_name: db_name.to_owned(),
}
}
pub fn get_correct_connection(&self, raw_sql: &str) -> DbConnectionConfig {
let re = Regex::new(r"(/*|//) db: (?P<conn>[\w]+)( */){0,}").unwrap();
let found_matches = re.captures(raw_sql);
if let Some(found_match) = &found_matches {
let detected_conn_name = &found_match[2];
return self.connections.get(detected_conn_name)
.expect(format!("Failed to find a matching connection type - connection name: {detected_conn_name}").as_str())
.clone();
}
self.connections.get("default")
.expect(r"Failed to find the default connection configuration - check your configuration
CLI options: https://jasonshin.github.io/sqlx-ts/user-guide/2.1.cli-options.html
File based config: https://jasonshin.github.io/sqlx-ts/reference-guide/2.configs-file-based.html
").clone()
}
}