From 2572ea30f1ec2972659a82b7071da702f876fb39 Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Mon, 21 Oct 2019 15:06:01 -0500 Subject: [PATCH 1/3] Fix some clippy warnings --- src/commands/kv/bulk/delete.rs | 7 +++--- src/commands/kv/bulk/put.rs | 10 +++----- src/commands/kv/key/mod.rs | 2 +- src/commands/kv/key/put.rs | 45 +++++++++++++++++----------------- src/main.rs | 29 +++++++++++++--------- 5 files changed, 48 insertions(+), 45 deletions(-) diff --git a/src/commands/kv/bulk/delete.rs b/src/commands/kv/bulk/delete.rs index fbee0c8a6..9138dd9b1 100644 --- a/src/commands/kv/bulk/delete.rs +++ b/src/commands/kv/bulk/delete.rs @@ -38,10 +38,9 @@ pub fn delete( Ok(file_type) if file_type.is_file() => { let data = fs::read_to_string(filename)?; let keys_vec = serde_json::from_str(&data); - if keys_vec.is_err() { - failure::bail!("Failed to decode JSON. Please make sure to follow the format, [{\"key\": \"test_key\", \"value\": \"test_value\"}, ...]") - } else { - Ok(keys_vec.unwrap()) + match keys_vec { + Ok(keys_vec) => Ok(keys_vec), + Err(_) => failure::bail!("Failed to decode JSON. Please make sure to follow the format, [{\"key\": \"test_key\", \"value\": \"test_value\"}, ...]") } } Ok(_) => failure::bail!("{} should be a JSON file, but is not", filename.display()), diff --git a/src/commands/kv/bulk/put.rs b/src/commands/kv/bulk/put.rs index a1fcbb30d..a4cf91d27 100644 --- a/src/commands/kv/bulk/put.rs +++ b/src/commands/kv/bulk/put.rs @@ -27,11 +27,9 @@ pub fn put( Ok(file_type) if file_type.is_file() => { let data = fs::read_to_string(filename)?; let data_vec = serde_json::from_str(&data); - if data_vec.is_err() { - Err(failure::format_err!("Failed to decode JSON. Please make sure to follow the format, [{{\"key\": \"test_key\", \"value\": \"test_value\"}}, ...]")) - } else { - let data_vec: Vec = data_vec.unwrap(); - Ok(data_vec) + match data_vec { + Ok(data_vec) => Ok(data_vec), + Err(_) => Err(failure::format_err!("Failed to decode JSON. Please make sure to follow the format, [{{\"key\": \"test_key\", \"value\": \"test_value\"}}, ...]")) } } Ok(_) => Err(failure::format_err!( @@ -62,7 +60,7 @@ pub fn call_api( client: &impl ApiClient, target: &Target, namespace_id: &str, - pairs: &Vec, + pairs: &[KeyValuePair], ) -> Result, ApiFailure> { client.request(&WriteBulk { account_identifier: &target.account_id, diff --git a/src/commands/kv/key/mod.rs b/src/commands/kv/key/mod.rs index ce710e1a8..bd9468e45 100644 --- a/src/commands/kv/key/mod.rs +++ b/src/commands/kv/key/mod.rs @@ -8,4 +8,4 @@ pub use delete::delete; pub use get::get; pub use key_list::KeyList; pub use list::list; -pub use put::put; +pub use put::{put, KVMetaData}; diff --git a/src/commands/kv/key/put.rs b/src/commands/kv/key/put.rs index 41499d99a..3502a8966 100644 --- a/src/commands/kv/key/put.rs +++ b/src/commands/kv/key/put.rs @@ -14,32 +14,32 @@ use crate::settings::global_user::GlobalUser; use crate::settings::target::Target; use crate::terminal::message; -pub fn put( - target: &Target, - user: &GlobalUser, - id: &str, - key: &str, - value: &str, - is_file: bool, - expiration: Option<&str>, - expiration_ttl: Option<&str>, -) -> Result<(), failure::Error> { +pub struct KVMetaData { + pub namespace_id: String, + pub key: String, + pub value: String, + pub is_file: bool, + pub expiration: Option, + pub expiration_ttl: Option, +} + +pub fn put(target: &Target, user: &GlobalUser, data: KVMetaData) -> Result<(), failure::Error> { kv::validate_target(target)?; let api_endpoint = format!( "https://api.cloudflare.com/client/v4/accounts/{}/storage/kv/namespaces/{}/values/{}", target.account_id, - id, - kv::url_encode_key(key) + &data.namespace_id, + kv::url_encode_key(&data.key) ); // Add expiration and expiration_ttl query options as necessary. let mut query_params: Vec<(&str, &str)> = vec![]; - if let Some(exp) = expiration { + if let Some(exp) = &data.expiration { query_params.push(("expiration", exp)) }; - if let Some(ttl) = expiration_ttl { + if let Some(ttl) = &data.expiration_ttl { query_params.push(("expiration_ttl", ttl)) }; let url = Url::parse_with_params(&api_endpoint, query_params); @@ -50,20 +50,21 @@ pub fn put( // If is_file is true, overwrite value to be the contents of the given // filename in the 'value' arg. - let mut res = if is_file { - match &metadata(value) { + let mut res = if data.is_file { + match &metadata(&data.value) { Ok(file_type) if file_type.is_file() => { - let file = fs::File::open(value)?; + let file = fs::File::open(&data.value)?; client.put(&url_into_str).body(file).send()? } - Ok(file_type) if file_type.is_dir() => { - failure::bail!("--path argument takes a file, {} is a directory", value) - } - Ok(_) => failure::bail!("--path argument takes a file, {} is a symlink", value), // last remaining value is symlink + Ok(file_type) if file_type.is_dir() => failure::bail!( + "--path argument takes a file, {} is a directory", + data.value + ), + Ok(_) => failure::bail!("--path argument takes a file, {} is a symlink", data.value), // last remaining value is symlink Err(e) => failure::bail!("{}", e), } } else { - client.put(&url_into_str).body(value.to_string()).send()? + client.put(&url_into_str).body(data.value).send()? }; if res.status().is_success() { diff --git a/src/main.rs b/src/main.rs index cebba197a..5ca2ac0a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ mod settings; mod terminal; mod util; +use crate::commands::kv::key::KVMetaData; use crate::settings::target::TargetType; use exitfailure::ExitFailure; use terminal::emoji; @@ -555,23 +556,27 @@ fn run() -> Result<(), failure::Error> { commands::kv::key::get(&target, &user, &namespace_id, key)? } ("put", Some(put_key_matches)) => { - let key = put_key_matches.value_of("key").unwrap(); + let key = put_key_matches.value_of("key").unwrap().to_string(); // If is_file is true, overwrite value to be the contents of the given // filename in the 'value' arg. - let value = put_key_matches.value_of("value").unwrap(); - let expiration = put_key_matches.value_of("expiration"); - let ttl = put_key_matches.value_of("expiration-ttl"); - commands::kv::key::put( - &target, - &user, - &namespace_id, + let value = put_key_matches.value_of("value").unwrap().to_string(); + let is_file = put_key_matches.is_present("path"); + let expiration = put_key_matches + .value_of("expiration") + .map(|e| e.to_string()); + let expiration_ttl = put_key_matches + .value_of("expiration-ttl") + .map(|t| t.to_string()); + let kv_metadata = KVMetaData { + namespace_id, key, - &value, - put_key_matches.is_present("path"), + value, + is_file, expiration, - ttl, - )? + expiration_ttl, + }; + commands::kv::key::put(&target, &user, kv_metadata)? } ("delete", Some(delete_key_matches)) => { let key = delete_key_matches.value_of("key").unwrap(); From aa3c0d9bfe303a4e45eabcae0d2066776de7bb49 Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Mon, 21 Oct 2019 15:29:04 -0500 Subject: [PATCH 2/3] Fix a few more clippy nits --- src/commands/build/wranglerjs/mod.rs | 2 +- src/commands/kv/bucket/upload.rs | 4 ++-- src/commands/publish/upload_form/mod.rs | 2 +- src/main.rs | 4 ++-- src/settings/target/mod.rs | 4 ++-- tests/config.rs | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/commands/build/wranglerjs/mod.rs b/src/commands/build/wranglerjs/mod.rs index 8ef40fa1b..92226589b 100644 --- a/src/commands/build/wranglerjs/mod.rs +++ b/src/commands/build/wranglerjs/mod.rs @@ -43,7 +43,7 @@ pub fn run_build(target: &Target) -> Result<(), failure::Error> { let status = command.status()?; if status.success() { - let output = fs::read_to_string(temp_file.clone()).expect("could not retrieve ouput"); + let output = fs::read_to_string(temp_file).expect("could not retrieve output"); let wranglerjs_output: WranglerjsOutput = serde_json::from_str(&output).expect("could not parse wranglerjs output"); diff --git a/src/commands/kv/bucket/upload.rs b/src/commands/kv/bucket/upload.rs index a489f1314..14f0221c1 100644 --- a/src/commands/kv/bucket/upload.rs +++ b/src/commands/kv/bucket/upload.rs @@ -165,7 +165,7 @@ mod tests { // Old values found on remote let mut exclude_keys = HashSet::new(); exclude_keys.insert(key_a_old.clone()); - exclude_keys.insert(key_b_old.clone()); + exclude_keys.insert(key_b_old); // local files (with b updated) to upload let pairs_to_upload = vec![ @@ -206,7 +206,7 @@ mod tests { // todo(gabbi): Implement PartialEq for KeyValuePair in cloudflare-rs. assert!(pair.key == actual[idx].key); assert!(pair.value == actual[idx].value); - idx = idx + 1; + idx += 1; } } } diff --git a/src/commands/publish/upload_form/mod.rs b/src/commands/publish/upload_form/mod.rs index aa899aecc..e8434478f 100644 --- a/src/commands/publish/upload_form/mod.rs +++ b/src/commands/publish/upload_form/mod.rs @@ -37,7 +37,7 @@ pub fn build_script_and_upload_form( build_generated_dir()?; concat_js(&name)?; - let path = format!("./pkg/{}_bg.wasm", name).to_string(); + let path = format!("./pkg/{}_bg.wasm", name); let binding = "wasm".to_string(); let wasm_module = WasmModule::new(path, binding)?; diff --git a/src/main.rs b/src/main.rs index 5ca2ac0a7..f221f4643 100644 --- a/src/main.rs +++ b/src/main.rs @@ -545,7 +545,7 @@ fn run() -> Result<(), failure::Error> { .unwrap() // clap configs ensure that if "binding" isn't present,"namespace-id" must be. .to_string(), }; - (target, namespace_id.to_string()) + (target, namespace_id) } None => unreachable!(), // this is unreachable because all kv:key commands have required arguments. }; @@ -608,7 +608,7 @@ fn run() -> Result<(), failure::Error> { .unwrap() // clap configs ensure that if "binding" isn't present,"namespace-id" must be. .to_string(), }; - (target, namespace_id.to_string()) + (target, namespace_id) } None => unreachable!(), // this is unreachable because all kv:key commands have required arguments. }; diff --git a/src/settings/target/mod.rs b/src/settings/target/mod.rs index a293c97e5..ad699e08d 100644 --- a/src/settings/target/mod.rs +++ b/src/settings/target/mod.rs @@ -151,9 +151,9 @@ impl Manifest { account_id: String::new(), env: None, kv_namespaces: None, - name: name.clone(), + name, private: None, - target_type: target_type.clone(), + target_type, route: Some(String::new()), routes: None, webpack_config: None, diff --git a/tests/config.rs b/tests/config.rs index c002fd3fe..58a4f710f 100644 --- a/tests/config.rs +++ b/tests/config.rs @@ -34,7 +34,7 @@ fn generate_config_with(eol: &str) { let config_file = fake_home_dir.join("config").join("default.toml"); let config = fs::read_to_string(&config_file) - .expect(&format!("could not read config at {:?}", &config_file)); + .unwrap_or_else(|_| panic!("could not read config at {:?}", &config_file)); assert_eq!( config, r#"email = "email@example.com" From 82846485c82789804b7fc40787e67cee23e1b290 Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Mon, 21 Oct 2019 16:22:51 -0500 Subject: [PATCH 3/3] Remove duplicate semicolon --- src/commands/kv/bucket/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/kv/bucket/mod.rs b/src/commands/kv/bucket/mod.rs index afbeede31..e22ff3080 100644 --- a/src/commands/kv/bucket/mod.rs +++ b/src/commands/kv/bucket/mod.rs @@ -435,7 +435,7 @@ mod tests { let actual_path_with_hash = generate_path_with_hash(&path, hashed_value.to_owned()).unwrap(); - let expected_path_with_hash = format!("path/to/asset.{}", hashed_value);; + let expected_path_with_hash = format!("path/to/asset.{}", hashed_value); assert_eq!(actual_path_with_hash, expected_path_with_hash); }