Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Fix some clippy warnings #793

Merged
merged 4 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/commands/kv/bulk/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
10 changes: 4 additions & 6 deletions src/commands/kv/bulk/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyValuePair> = 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!(
Expand Down Expand Up @@ -62,7 +60,7 @@ pub fn call_api(
client: &impl ApiClient,
target: &Target,
namespace_id: &str,
pairs: &Vec<KeyValuePair>,
pairs: &[KeyValuePair],
) -> Result<ApiSuccess<()>, ApiFailure> {
client.request(&WriteBulk {
account_identifier: &target.account_id,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/kv/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
45 changes: 23 additions & 22 deletions src/commands/kv/key/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub expiration_ttl: Option<String>,
}

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);
Expand All @@ -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() {
Expand Down
29 changes: 17 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down