Skip to content

Commit 95d6217

Browse files
committed
[rust] Improve management of the configuration key for cache path
1 parent 13d6954 commit 95d6217

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

rust/src/config.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,28 @@ impl StringKey<'_> {
170170
pub fn get_value(&self) -> String {
171171
let config = get_config().unwrap_or_default();
172172
let keys = self.0.to_owned();
173+
let default_value = self.1.to_owned();
174+
173175
let mut result;
176+
let mut first_key = "";
174177
for key in keys {
178+
if first_key.is_empty() {
179+
first_key = key;
180+
}
175181
if config.contains_key(key) {
176182
result = config[key].as_str().unwrap().to_string()
177183
} else {
178184
result = env::var(get_env_name(key)).unwrap_or_default()
179185
}
180186
if !result.is_empty() {
181-
if key.eq(CACHE_PATH_KEY) {
182-
write_cache_path(result.clone());
183-
}
184-
return result;
187+
// The configuration key for the cache path ("cache-path") is special because
188+
// the rest of the configuration values depend on this value (since the
189+
// configuration file is stored in the cache path). Therefore, this value needs
190+
// to be discovered in the first place and stored globally (on CACHE_PATH)
191+
return check_cache_path(key, result, default_value);
185192
}
186193
}
187-
self.1.to_owned()
194+
default_value
188195
}
189196
}
190197

@@ -239,6 +246,22 @@ fn concat(prefix: &str, suffix: &str) -> String {
239246
version_label
240247
}
241248

249+
fn check_cache_path(key: &str, value_in_config_or_env: String, default_value: String) -> String {
250+
let return_value = if key.eq(CACHE_PATH_KEY) {
251+
if default_value.is_empty() {
252+
value_in_config_or_env
253+
} else {
254+
default_value
255+
}
256+
} else {
257+
value_in_config_or_env
258+
};
259+
if key.eq(CACHE_PATH_KEY) {
260+
write_cache_path(return_value.clone());
261+
}
262+
return_value
263+
}
264+
242265
fn write_cache_path(cache_path: String) {
243266
CACHE_PATH.with(|value| {
244267
*value.borrow_mut() = cache_path;

rust/src/files.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub fn unzip(
206206
final_path,
207207
single_file,
208208
&compressed_path.to_path_buf(),
209-
&log,
209+
log,
210210
)?;
211211

212212
Ok(())
@@ -251,7 +251,7 @@ pub fn copy_folder_content(
251251
destination_path,
252252
single_file.clone(),
253253
avoid_path,
254-
&log,
254+
log,
255255
)?;
256256
}
257257
}

rust/src/logger.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use crate::config::BooleanKey;
1819
use crate::metadata::now_unix_timestamp;
1920
use env_logger::fmt::Color;
2021
use env_logger::Target::Stdout;
@@ -70,6 +71,12 @@ pub struct JsonOutput {
7071
}
7172

7273
impl Logger {
74+
pub fn default() -> Self {
75+
let debug = BooleanKey("debug", false).get_value();
76+
let trace = BooleanKey("trace", false).get_value();
77+
Logger::create("", debug, trace)
78+
}
79+
7380
pub fn create(output: &str, debug: bool, trace: bool) -> Self {
7481
let output_type;
7582
if output.eq_ignore_ascii_case("json") {

rust/src/main.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use exitcode::DATAERR;
2323
use exitcode::OK;
2424
use exitcode::UNAVAILABLE;
2525
use selenium_manager::config::{BooleanKey, StringKey, CACHE_PATH_KEY};
26-
use selenium_manager::files::{default_cache_folder, path_buf_to_string};
2726
use selenium_manager::grid::GridManager;
2827
use selenium_manager::logger::{Logger, BROWSER_PATH, DRIVER_PATH};
2928
use selenium_manager::REQUEST_TIMEOUT_SEC;
@@ -129,13 +128,9 @@ struct Cli {
129128

130129
fn main() {
131130
let cli = Cli::parse();
131+
let cache_path =
132+
StringKey(vec![CACHE_PATH_KEY], &cli.cache_path.unwrap_or_default()).get_value();
132133

133-
let default_cache_path = path_buf_to_string(default_cache_folder());
134-
let cache_path = StringKey(
135-
vec![CACHE_PATH_KEY],
136-
&cli.cache_path.unwrap_or(default_cache_path),
137-
)
138-
.get_value();
139134
let debug = cli.debug || BooleanKey("debug", false).get_value();
140135
let trace = cli.trace || BooleanKey("trace", false).get_value();
141136
let log = Logger::create(&cli.output, debug, trace);

0 commit comments

Comments
 (0)