-
Notifications
You must be signed in to change notification settings - Fork 98
Remove unwraps (for commands) #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
e707db6
8692f46
25278f9
cf19d0c
73a9ecd
5c7fae7
8266a11
ff3ed9a
2ea9ff6
13ae601
cfae9e4
9371097
0b5543c
96bb152
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,11 +54,13 @@ where | |
|
|
||
| let canister_name = args.value_of("canister_name").unwrap(); | ||
| let canister_info = CanisterInfo::load(config, canister_name)?; | ||
| // Read the config. | ||
| let canister_id = canister_info.get_canister_id().ok_or_else(|| { | ||
| DfxError::CannotFindBuildOutputForCanister(canister_info.get_name().to_owned()) | ||
| })?; | ||
|
|
||
| let method_name = args.value_of("method_name").unwrap(); | ||
| let method_name = args.value_of("method_name").ok_or_else(|| { | ||
| DfxError::InvalidArgument("method name argument provided invalid".to_string()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
eftychis marked this conversation as resolved.
Outdated
|
||
| })?; | ||
| let arguments: Option<&str> = args.value_of("argument"); | ||
| let arg_type: Option<&str> = args.value_of("type"); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,11 +48,13 @@ where | |
|
|
||
| let canister_name = args.value_of("canister_name").unwrap(); | ||
| let canister_info = CanisterInfo::load(config, canister_name)?; | ||
| // Read the config. | ||
| let canister_id = canister_info.get_canister_id().ok_or_else(|| { | ||
| DfxError::CannotFindBuildOutputForCanister(canister_info.get_name().to_owned()) | ||
| })?; | ||
|
|
||
| let method_name = args.value_of("method_name").unwrap(); | ||
| let method_name = args | ||
| .value_of("method_name") | ||
| .ok_or_else(|| DfxError::InvalidArgument("method name".to_string()))?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should at least be consistent in the error messages. The |
||
| let arguments: Option<&str> = args.value_of("argument"); | ||
| let arg_type: Option<&str> = args.value_of("type"); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,8 +25,14 @@ pub fn exec<T>(env: &T, args: &ArgMatches<'_>) -> DfxResult | |
| where | ||
| T: ClientEnv, | ||
| { | ||
| let request_id = RequestId::from_str(&args.value_of("request_id").unwrap()[2..]) | ||
| .map_err(|e| DfxError::InvalidArgument(format!("Invalid request ID: {:?}", e)))?; // FIXME Default formatter for RequestIdFromStringError | ||
| let request_id = RequestId::from_str( | ||
| &args.value_of("request_id").ok_or_else(|| { | ||
| DfxError::InvalidArgument( | ||
| "failed to retrieve request_id -- required argument".to_string(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ) | ||
| })?[2..], | ||
| ) | ||
| .map_err(|e| DfxError::InvalidArgument(format!("invalid request ID: {:?}", e)))?; // FIXME Default formatter for RequestIdFromStringError | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like it's a different error, not |
||
| let request_status = request_status(env.get_client(), request_id); | ||
| let mut runtime = Runtime::new().expect("Unable to create a runtime"); | ||
| match runtime.block_on(request_status) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,9 @@ pub fn exec<T: ProjectConfigEnv>(env: &T, args: &ArgMatches<'_>) -> DfxResult { | |
| .ok_or(DfxError::CommandMustBeRunInAProject)? | ||
| .clone(); | ||
|
|
||
| let config_path = args.value_of("config_path").unwrap(); | ||
| let config_path = args | ||
| .value_of("config_path") | ||
| .ok_or_else(|| DfxError::InvalidArgument("config path".to_string()))?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Snake case. |
||
|
|
||
| // We replace `.` with `/` so the user can use `path.value.field` instead of forcing him | ||
| // to use `path/value/field`. Since none of our keys have slashes or tildes in them it | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,7 +113,10 @@ where | |
| T: BinaryCacheEnv + PlatformEnv + VersionEnv, | ||
| { | ||
| let dry_run = args.is_present(DRY_RUN); | ||
| let project_name = Path::new(args.value_of(PROJECT_NAME).unwrap()); | ||
| let project_name_path = args | ||
| .value_of(PROJECT_NAME) | ||
| .ok_or_else(|| DfxError::InvalidArgument("project path".to_string()))?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Snake case. |
||
| let project_name = Path::new(project_name_path); | ||
|
|
||
| if project_name.exists() { | ||
| return Err(DfxError::ProjectExists); | ||
|
|
@@ -127,6 +130,10 @@ where | |
| } | ||
|
|
||
| let mut new_project_files = assets::new_project_files()?; | ||
| let project_name_str = project_name | ||
| .to_str() | ||
| .ok_or_else(|| DfxError::InvalidArgument("project name".to_string()))?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Snake case. |
||
|
|
||
| for file in new_project_files.entries()? { | ||
| let mut file = file?; | ||
|
|
||
|
|
@@ -135,18 +142,21 @@ where | |
| } | ||
|
|
||
| let mut s = String::new(); | ||
| file.read_to_string(&mut s).unwrap(); | ||
| file.read_to_string(&mut s) | ||
| .or_else(|e| Err(DfxError::Io(e)))?; | ||
|
|
||
| // Perform replacements. | ||
| let s = s.replace("{project_name}", project_name.to_str().unwrap()); | ||
| let s = s.replace("{project_name}", project_name_str); | ||
| let s = s.replace("{dfx_version}", dfx_version); | ||
|
|
||
| // Perform path replacements. | ||
| let p = PathBuf::from( | ||
| project_name | ||
| .join(file.header().path()?) | ||
| .to_str() | ||
| .unwrap() | ||
| .ok_or_else(|| { | ||
|
hansl marked this conversation as resolved.
Outdated
eftychis marked this conversation as resolved.
Outdated
|
||
| DfxError::InvalidArgument("project name path or file header".to_string()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should never happen. Maybe we need an |
||
| })? | ||
|
eftychis marked this conversation as resolved.
Outdated
|
||
| .replace("__dot__", ".") | ||
| .as_str(), | ||
| ); | ||
|
|
@@ -192,7 +202,7 @@ where | |
| include_str!("../../assets/welcome.txt"), | ||
| dfx_version, | ||
| assets::dfinity_logo(), | ||
| project_name.to_str().unwrap() | ||
| project_name_str | ||
| ); | ||
|
|
||
| Ok(()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,11 +218,23 @@ impl Config { | |
| &self.config | ||
| } | ||
|
|
||
| pub fn get_project_root(&self) -> &Path { | ||
| // a configuration path contains a file name specifically. As | ||
| // such we should be returning at least root as parent. If | ||
| // this is invariance is broken, we must fail. | ||
| self.path.parent().expect( | ||
| "An incorrect configuration path was set with no parent, i.e. did not include root", | ||
| ) | ||
| } | ||
|
|
||
| pub fn save(&self) -> DfxResult { | ||
| std::fs::write( | ||
| &self.path, | ||
| serde_json::to_string_pretty(&self.json).unwrap(), | ||
| )?; | ||
| let json_pretty = serde_json::to_string_pretty(&self.json).or_else(|e| { | ||
| Err(DfxError::InvalidData(format!( | ||
| "Failed to serialize -- {}", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| e | ||
| ))) | ||
| })?; | ||
| std::fs::write(&self.path, json_pretty)?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.