-
Notifications
You must be signed in to change notification settings - Fork 4
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
Save API tokens and default server URL locally #259
Conversation
…ult url if one isn't provided at runtime.
… for URLs and tokens
src/bin/ayb.rs
Outdated
} else { | ||
home_dir() | ||
.expect("can't determine home directory") | ||
.join(".ayb.json") |
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.
The standard in Linux is to create config files inside $HOME/.config/$APP_NAME
(or whatever $XDG_CONFIG_HOME is set to).
In this case that would be $HOME/.config/ayb/ayb.json
.
The crate dirs
does this for you with the dirs::config_dir
function :)
There's also directories
, which is higher level, and fully compliant with the standard of all the different OSes.
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.
Thank you! I switched over to directories
. Hurray for standardizing across operating systems! :)
src/client/config.rs
Outdated
} | ||
|
||
impl ClientConfig { | ||
fn default_config() -> ClientConfig { |
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.
As you may already know, perfect use case for Default
:p
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.
Thank you and done!
…ault_urls from .ayb.json, and test set_default_url
…tion in documentation
base_url: url.to_string(), | ||
api_token: token, | ||
}; | ||
if let Some(matches) = matches.subcommand_matches("create_database") { |
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.
The changes below are just unindenting by one level since we removed a layer of if
to get the URL above.
@@ -0,0 +1,2 @@ | |||
pub mod config; |
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.
Starting the move toward pulling things out of http
and into server
and client
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.
I really like how you implemented this, great work! :)
Just some suggestions
} | ||
|
||
impl ClientConfig { | ||
pub fn new() -> ClientConfig { |
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.
Should new
receive the version as a param or should we leave it as is and change it in the future if needed?
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.
Good question! My intuition is no, since the version is a function of the schema/format of the encoded JSON, and should stay close to it. I'm adding it here now in case we ever need to change the schema/format, so that we can detect an older version of the configuration file and migrate it. The caller shouldn't have any knowledge of this internal detail of the configuration file. For example, you wouldn't want a caller to say that the configuration format is a v2 format when it is in fact v1. Let me know if that makes sense.
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.
I think you're right, I'll mark this as resolved :)
src/client/config.rs
Outdated
|
||
pub fn from_file(file_path: &PathBuf) -> Result<ClientConfig, std::io::Error> { | ||
if file_path.exists() { | ||
let file = File::open(file_path)?; |
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 could use fs::read_to_string()
here to simplify this function a bit
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.
Done! I in initially had the buffered reader/writers for performance reasons, but the configuration file is so tiny that I suspect it will hardly matter :).
src/client/config.rs
Outdated
return Ok(serde_json::from_reader(&mut reader)?); | ||
} | ||
|
||
Ok(ClientConfig::new()) |
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 should call default
instead of new
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.
Done!
src/client/config.rs
Outdated
.parent() | ||
.expect("unable to determine parent of ayb configuration directory"), | ||
)?; | ||
let file = File::create(file_path)?; |
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 could use fs::write
to simplify this function
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.
Done! Thank you!
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.
I forgot one comment :p
Also, would a command like ayb client clean
to delete the config file be useful?
README.md
Outdated
@@ -116,6 +112,13 @@ $ ayb client profile marcua | |||
Adam Marcus | | | | http://marcua.net | |||
``` | |||
|
|||
Note that the command line also saved a configuration file (on Linux, | |||
to a place like `$HOME/.config/ayb/ayb.json`) for your convenience so |
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.
Maybe adding three bullet points for Linux, MacOS and Windows would be useful for some people
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.
Done!
… code review feedback
Thank you so much for the review @sofiaritz ! That simplified and clarified things! Let me know if my response on passing a version makes sense. Re: |
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.
Great implementation! I think we can leave the ayb client clean
command for another day :)
The idea of the ayb client clean
command is to leave the file system like it was before ayb
was installed and used. That way someone who wants to remove ayb
from their system can do so without leaving anything behind.
Thank you so much @sofiaritz! :) I like the |
Resolves #251