-
Notifications
You must be signed in to change notification settings - Fork 63
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
Disallow overwrite_key and force_update in Config File #218
base: master
Are you sure you want to change the base?
Conversation
save the file maybe 🤔 |
Done. Actually, I thought it does it automatically but saw now I have not enabled the feature. Showing now. |
.gitignore
Outdated
.idea | ||
.DS_Store |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't add this change to the PR, these are entirely unrelated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I raise a pr, it gets attached automatically and pr gets two commits. So, rebased it and squashed it. What Should I do to make the commit ignore in this pr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete it, amend and rebase so it is a single commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete it, amend and rebase so it is a single commit
okay.
teos/src/config.rs
Outdated
@@ -203,6 +203,12 @@ impl Config { | |||
self.tor_support |= options.tor_support; | |||
self.debug |= options.debug; | |||
self.deps_debug |= options.deps_debug; | |||
if self.overwrite_key { | |||
panic!("The 'overwrite_key' parameter cannot be set in the configuration file. Please pass it as a command-line argument if necessary."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't panic. Make the method return a Result
and check it on main, if it's an error, eprintln!("{e}")
and std::process::exit(1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't panic. Make the method return a
Result
and check it on main, if it's an error,eprintln!("{e}")
andstd::process::exit(1)
if path_with_options have to updated with result, I have to add Result<(), String>
in the function to make the error message work. But the problem is I have to change all the code path function to return a result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should do such sanity checks in config::from_file
instead of patch_with_options
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should do such sanity checks in
config::from_file
instead ofpatch_with_options
.
config::from_file
is in main.rs where I don't see any changes can be made regarding this issue. In config.rs, from_file function can be modified to return a Result<T, String>
instead of T
.
I made changes only in the main.rs file because modifying the config.rs file's from_file function would have resulted in errors. The reason for this is that when we removed the generic type T and added Config directly, it caused errors related to the opt variable being linked with overwrite_key and force_update. Since opt is specific to the command line options, it cannot be directly used in the from_file function, which is responsible for loading the configuration from a file. Therefore, I decided to handle the sanity checks in the main.rs file instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should have these checks inside config.rs
.
What you could do is modify the from_file
function like so:
pub fn from_file<T: Default + serde::de::DeserializeOwned>(path: PathBuf) -> Result<T, String> {
match std::fs::read(path) {
Ok(file_content) => match toml::from_slice::<T>(&file_content) {
Err(e) => {
Err(format!("Couldn't parse config file: {e}"))
}
Ok(config) => {
// do the sanity checks here.
if config.overwrite_key {
return Err(format!("Overwrite key isn't allowed in the config file."))
}
Ok(config)
}
},
Err(_) => Ok(T::default()),
}
}
This won't work right away, you will need to adapt some stuff in main.rs
and probably cli.rs
because it uses the same parsing function.
teos/src/main.rs
Outdated
if conf.overwrite_key { | ||
eprintln!("The 'overwrite_key' parameter cannot be set in the configuration file. Please pass it as a command-line argument if necessary."); | ||
std::process::exit(1); | ||
} | ||
if conf.force_update { | ||
eprintln!("The 'force_update' parameter cannot be set in the configuration file. Please pass it as a command-line argument if necessary."); | ||
std::process::exit(1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't belong here.
Got it. |
I've tried implementing the changes you've suggested. Specifically, I updated the from_file function to include checks for overwrite_key. However, I'm encountering an issue. The from_file function is a generic function, which takes any type T that implements I am thinking of removing the T and make it config. |
I think a good solution is to serialize the config as a serde object and disallowing the fields we want disallowed. @anipaul2 |
Yes sounds like a reasonable approach. |
…d force_update fields from serialization. By using the #[serde(skip_serializing)] attribute, these fields will not be included when the Config struct is serialized.
@sr-gi @mariocynicys I have first made the changes in cli.rs, main.rs, and config.rs to disallow overwrite_key and force_update but only skipping the serializing in the flags part does this. Let me know if this approach is wrong but it worked on my end. |
I don't think what you are doing is right. This is only preventing Coinf from serializing those two items. You could prevent it from deserializing it, which will prevent us from reading that, but I don't think that'll be correct either. Giving this a closer look, I'm starting to lean toward two different solutions: Either reworking the Config logic to specify where the data comes from, potentially using an enum, which will help towards #204 (comment), or doing this either with a custom deserializer or in I think @anipaul2 has a point in that |
Agreed. Even though these two parameter should just default to None when queried in the cli config. But doesn't make a lot of sense querying it in the first place.
Not sure how the enum would help regarding the comment you linked. I'm thinking now of copying this function over to cli_config and not make it generic anymore. Could this help the issue you linked? |
This is what I'm thinking about give or take. However, I haven't been able to make the deserialization of |
@sr-gi Fixed the compilation issues in this branch. Acking the approach :). |
@sr-gi @mariocynicys how to approach this issue now? any ideas? I am thinking of making a change in |
I think I'd make more sense to fix this one by reworking the config as I was trying to do here: master...sr-gi:rust-teos:config-rework I haven't had much time lately to finish it, so no worries about this for now, it's not critical, or you can pick up when I left it if you like |
okay. |
fixes #217