Skip to content

Commit 35e2361

Browse files
committed
Added a global config file
1 parent 1117320 commit 35e2361

File tree

5 files changed

+110
-25
lines changed

5 files changed

+110
-25
lines changed

src/app/business_logic/request/export.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ impl App<'_> {
294294
};
295295

296296
if request.settings.use_config_proxy.as_bool() {
297-
if let Some(proxy) = &self.config.proxy {
297+
if let Some(proxy) = &self.config.get_proxy() {
298298
let mut proxy_output = String::from("--proxy ");
299299

300300
if let Some(http_proxy) = &proxy.http_proxy {
@@ -354,7 +354,7 @@ impl App<'_> {
354354
/* Proxy */
355355

356356
if request.settings.use_config_proxy.as_bool() {
357-
if let Some(proxy) = &self.config.proxy {
357+
if let Some(proxy) = &self.config.get_proxy() {
358358
let mut proxy_output = String::from("\n 'proxy' => [");
359359

360360
if let Some(http_proxy) = &proxy.http_proxy {
@@ -574,7 +574,7 @@ impl App<'_> {
574574

575575
/* Proxy */
576576
if request.settings.use_config_proxy.as_bool() {
577-
if let Some(proxy) = &self.config.proxy {
577+
if let Some(proxy) = &self.config.get_proxy() {
578578
output += ",\n proxy: {";
579579

580580
if let Some(http_proxy) = &proxy.http_proxy {
@@ -754,7 +754,7 @@ impl App<'_> {
754754
output += " let status = response.status();\n";
755755
output += " let body = response.text().await?;\n\n";
756756

757-
if request.settings.use_config_proxy.as_bool() && self.config.proxy.is_some() {
757+
if request.settings.use_config_proxy.as_bool() && self.config.get_proxy().is_some() {
758758
output += " // Use reqwest::ClientBuilder with reqwest::Proxy\n";
759759
output += " // https://docs.rs/reqwest/latest/reqwest/struct.Proxy.html\n\n";
760760
}

src/app/business_logic/request/send.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl App<'_> {
7373
/* PROXY */
7474

7575
if request.settings.use_config_proxy.as_bool() {
76-
match &self.config.proxy {
76+
match &self.config.get_proxy() {
7777
None => {}
7878
Some(proxy) => {
7979
match &proxy.http_proxy {

src/app/files/config.rs

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub struct Proxy {
5454

5555
impl Config {
5656
pub fn is_syntax_highlighting_disabled(&self) -> bool {
57-
return self.disable_syntax_highlighting.unwrap_or(false)
57+
self.disable_syntax_highlighting.unwrap_or(false)
5858
}
5959

6060
pub fn should_save_requests_response(&self) -> bool {
@@ -68,19 +68,19 @@ impl Config {
6868
}
6969

7070
pub fn is_cors_disabled(&self) -> bool {
71-
return self.disable_cors.unwrap_or(false)
71+
self.disable_cors.unwrap_or(false)
7272
}
7373

7474
pub fn is_image_preview_disabled(&self) -> bool {
75-
return self.disable_images_preview.unwrap_or(false)
75+
self.disable_images_preview.unwrap_or(false)
7676
}
7777

7878
pub fn is_graphical_protocol_disabled(&self) -> bool {
79-
return self.disable_graphical_protocol.unwrap_or(false)
79+
self.disable_graphical_protocol.unwrap_or(false)
8080
}
8181

8282
pub fn should_wrap_body(&self) -> bool {
83-
return self.wrap_responses.unwrap_or(false)
83+
self.wrap_responses.unwrap_or(false)
8484
}
8585

8686
pub fn get_preferred_collection_file_format(&self) -> CollectionFileFormat {
@@ -89,13 +89,17 @@ impl Config {
8989
Some(file_format) => file_format.clone()
9090
}
9191
}
92+
93+
pub fn get_proxy(&self) -> &Option<Proxy> {
94+
&self.proxy
95+
}
9296
}
9397

9498
impl App<'_> {
9599
pub fn parse_config_file(&mut self, path_buf: &PathBuf) {
96100
let mut file_content = String::new();
97101

98-
trace!("Trying to open or create \"atac.toml\" config file");
102+
trace!("Trying to open \"atac.toml\" config file");
99103

100104
let mut config_file = OpenOptions::new()
101105
.read(true)
@@ -116,4 +120,60 @@ impl App<'_> {
116120

117121
trace!("Config file parsed!");
118122
}
123+
124+
pub fn parse_global_config_file(&mut self, path_buf: &PathBuf) {
125+
let mut file_content = String::new();
126+
127+
trace!("Trying to open \"{}\" global config file", path_buf.display());
128+
129+
let mut global_config_file = OpenOptions::new()
130+
.read(true)
131+
.open(path_buf.clone())
132+
.expect("\tCould not open global config file");
133+
134+
global_config_file.read_to_string(&mut file_content).expect("\tCould not read global config file");
135+
136+
let global_config: Config = match toml::from_str(&file_content) {
137+
Ok(config) => config,
138+
Err(e) => panic_error(format!("Could not parse config file\n\t{e}"))
139+
};
140+
141+
// Replace an attribute if it is not set
142+
143+
if self.config.disable_syntax_highlighting.is_none() {
144+
self.config.disable_syntax_highlighting = global_config.disable_syntax_highlighting;
145+
}
146+
147+
if self.config.save_requests_response.is_none() {
148+
self.config.save_requests_response = global_config.save_requests_response;
149+
}
150+
151+
if self.config.disable_cors.is_none() {
152+
self.config.disable_cors = global_config.disable_cors;
153+
}
154+
155+
if self.config.disable_images_preview.is_none() {
156+
self.config.disable_images_preview = global_config.disable_images_preview;
157+
}
158+
159+
if self.config.disable_graphical_protocol.is_none() {
160+
self.config.disable_graphical_protocol = global_config.disable_graphical_protocol;
161+
}
162+
163+
if self.config.wrap_responses.is_none() {
164+
self.config.wrap_responses = global_config.wrap_responses;
165+
}
166+
167+
if self.config.preferred_collection_file_format.is_none() {
168+
self.config.preferred_collection_file_format = global_config.preferred_collection_file_format;
169+
}
170+
171+
if self.config.proxy.is_none() {
172+
self.config.proxy = global_config.proxy;
173+
}
174+
175+
self.config.set_should_skip_requests_response();
176+
177+
trace!("Global config file parsed!");
178+
}
119179
}

src/app/startup/startup.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ impl<'a> App<'a> {
111111
}
112112
}
113113

114+
// Check if the global config file exists
115+
if let Some(config_directory) = &ARGS.config_directory {
116+
let global_config_file_path = config_directory.join("global.toml");
117+
118+
if global_config_file_path.exists() {
119+
self.parse_global_config_file(&global_config_file_path);
120+
}
121+
}
122+
114123
// Ensures that legacy collections and requests gets save as their new version
115124
if ARGS.should_save {
116125
for index in 0..self.collections.len() {

src/cli/args.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ pub enum Command {
122122
lazy_static! {
123123
pub static ref ARGS: GlobalArgs = {
124124
let args = Args::parse();
125-
125+
126+
let config_directory = get_app_config_dir();
127+
126128
let (directory, should_parse_directory) = match &args.command {
127129
// CLI
128130
Some(command) => match command.clone() {
@@ -131,14 +133,15 @@ lazy_static! {
131133
// Commands that use no dir at all
132134
Command::Try(_) => (None, false),
133135
// Commands that use the app dir
134-
_ => (Some(choose_app_directory(args.directory)), true)
136+
_ => (Some(choose_app_directory(args.directory, &config_directory)), true)
135137
},
136138
// TUI
137-
None => (Some(choose_app_directory(args.directory)), true)
139+
None => (Some(choose_app_directory(args.directory, &config_directory)), true)
138140
};
139141

140142
GlobalArgs {
141143
directory,
144+
config_directory,
142145
command: args.command,
143146
collection_filter: args.filter,
144147
should_run_tui: args.tui,
@@ -150,7 +153,27 @@ lazy_static! {
150153
};
151154
}
152155

153-
fn choose_app_directory(path_buf: Option<PathBuf>) -> PathBuf {
156+
fn get_app_config_dir() -> Option<PathBuf> {
157+
let project_directory = ProjectDirs::from("com", "Julien-cpsn", "ATAC");
158+
159+
let config_directory = match project_directory {
160+
Some(project_directory) => {
161+
let config_directory = project_directory.config_dir().to_path_buf();
162+
163+
// Create the config dir if it does not exist
164+
if !config_directory.exists() {
165+
fs::create_dir_all(&config_directory).expect(&format!("Could not recursively create folder \"{}\"", config_directory.display()));
166+
}
167+
168+
Some(config_directory)
169+
},
170+
None => None
171+
};
172+
173+
return config_directory;
174+
}
175+
176+
fn choose_app_directory(path_buf: Option<PathBuf>, config_directory: &Option<PathBuf>) -> PathBuf {
154177
match path_buf {
155178
// If a directory was provided with the CLI argument
156179
Some(directory) => expand_tilde(directory),
@@ -161,16 +184,8 @@ fn choose_app_directory(path_buf: Option<PathBuf>) -> PathBuf {
161184
Ok(env_directory) => expand_tilde(PathBuf::from(env_directory)),
162185

163186
// No ATAC_MAIN_DIR env variable
164-
Err(_) => match ProjectDirs::from("com", "Julien-cpsn", "ATAC") {
165-
Some(project_dir) => {
166-
let config_dir = project_dir.config_dir();
167-
168-
if !config_dir.exists() {
169-
fs::create_dir_all(config_dir).expect(&format!("Could not recursively create folder \"{}\"", config_dir.display()));
170-
}
171-
172-
config_dir.to_path_buf()
173-
},
187+
Err(_) => match config_directory {
188+
Some(config_directory) => config_directory.clone(),
174189
None => panic_error("No directory provided, provide one either with `--directory <dir>` or via the environment variable `ATAC_MAIN_DIR`")
175190
}
176191
}
@@ -180,6 +195,7 @@ fn choose_app_directory(path_buf: Option<PathBuf>) -> PathBuf {
180195
#[derive(Debug)]
181196
pub struct GlobalArgs {
182197
pub directory: Option<PathBuf>,
198+
pub config_directory: Option<PathBuf>,
183199
pub command: Option<Command>,
184200
pub collection_filter: Option<Regex>,
185201
pub should_run_tui: bool,

0 commit comments

Comments
 (0)