From bbe26ccf3eaf79e18949c96e6f90523a325f97ca Mon Sep 17 00:00:00 2001 From: Brady Ouren Date: Wed, 25 Oct 2023 03:59:45 -0700 Subject: [PATCH] feat(sdk): allow global ~/.clarinet/Settings.toml (#1208) * add global Settings.toml option * rustfmt * fix merge * code review: use warns and nest err conditions into global defaults --- Cargo.lock | 1 + components/clarinet-cli/Cargo.toml | 1 + components/clarinet-cli/src/frontend/cli.rs | 64 ++++++++++++++++++--- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a3a11d0f..c0633f32f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -662,6 +662,7 @@ dependencies = [ "crossbeam-channel", "crossterm 0.22.1", "ctrlc", + "dirs", "encoding_rs", "futures", "fwdansi", diff --git a/components/clarinet-cli/Cargo.toml b/components/clarinet-cli/Cargo.toml index a9b1bead5..b6fb734f2 100644 --- a/components/clarinet-cli/Cargo.toml +++ b/components/clarinet-cli/Cargo.toml @@ -37,6 +37,7 @@ lazy_static = "1.4.0" atty = "0.2.14" termcolor = "1.1.2" regex = "1.7" +dirs = { version = "4.0.0" } libc = "0.2.86" encoding_rs = "0.8.31" percent-encoding = "2.1.0" diff --git a/components/clarinet-cli/src/frontend/cli.rs b/components/clarinet-cli/src/frontend/cli.rs index 16133a08d..2345e5646 100644 --- a/components/clarinet-cli/src/frontend/cli.rs +++ b/components/clarinet-cli/src/frontend/cli.rs @@ -550,6 +550,11 @@ struct Check { pub use_computed_deployment_plan: bool, } +#[derive(Serialize, Deserialize)] +struct GlobalSettings { + disable_hints: bool, + enable_telemetry: Option, +} #[derive(Parser, PartialEq, Clone, Debug)] struct Completions { /// Specify which shell to generation completions script for @@ -579,7 +584,40 @@ pub fn main() { } }; + let mut global_settings = String::new(); + let global_settings_default = GlobalSettings { + disable_hints: false, + enable_telemetry: None, + }; + // This is backwards compatible with ENV var setting as well as the new ~/.clarinet/Settings.toml let hints_enabled = env::var("CLARINET_DISABLE_HINTS") != Ok("1".into()); + let home_dir = dirs::home_dir(); + let mpath: Option = home_dir.map(|home_dir| home_dir.join(".clarinet/Settings.toml")); + let settings_file = "~/.clarinet/Settings.toml"; + let global_settings: GlobalSettings = match mpath { + Some(path) => { + if path.exists() { + let mut file = File::open(&path).expect("Unable to open the file"); + let result = file.read_to_string(&mut global_settings); + match result { + Ok(_) => match toml::from_str(&global_settings) { + Ok(res) => res, + Err(_) => { + println!("{}{}", format_warn!("unable to parse "), settings_file); + global_settings_default + } + }, + Err(_) => { + println!("{}{}", format_warn!("unable to read file "), settings_file); + global_settings_default + } + } + } else { + global_settings_default + } + } + None => global_settings_default, + }; match opts.command { Command::New(project_opts) => { @@ -598,15 +636,23 @@ pub fn main() { if project_opts.disable_telemetry { false } else { - println!("{}", yellow!("Send usage data to Hiro.")); - println!("{}", yellow!("Help Hiro improve its products and services by automatically sending diagnostics and usage data.")); - println!("{}", yellow!("Only high level usage information, and no information identifying you or your project are collected.")); - // TODO(lgalabru): once we have a privacy policy available, add a link - // println!("{}", yellow!("Visit http://hiro.so/clarinet-privacy for details.")); - println!("{}", yellow!("Enable [Y/n]?")); - let mut buffer = String::new(); - std::io::stdin().read_line(&mut buffer).unwrap(); - !buffer.starts_with("n") + let enabled = match global_settings.enable_telemetry { + Some(true) => true, + _ => env::var("CLARINET_TELEMETRY") == Ok("1".into()), + }; + if enabled { + true + } else { + println!("{}", yellow!("Send usage data to Hiro.")); + println!("{}", yellow!("Help Hiro improve its products and services by automatically sending diagnostics and usage data.")); + println!("{}", yellow!("Only high level usage information, and no information identifying you or your project are collected.")); + // TODO(lgalabru): once we have a privacy policy available, add a link + // println!("{}", yellow!("Visit http://hiro.so/clarinet-privacy for details.")); + println!("{}", yellow!("Enable [Y/n]?")); + let mut buffer = String::new(); + std::io::stdin().read_line(&mut buffer).unwrap(); + !buffer.starts_with("n") + } } } else { false