Skip to content
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

Merged
merged 15 commits into from
Jan 10, 2024
Merged

Conversation

marcua
Copy link
Owner

@marcua marcua commented Jan 6, 2024

Resolves #251

@marcua marcua changed the title Save auth token to local config [WIP] Save auth token to local config Jan 6, 2024
src/bin/ayb.rs Outdated
} else {
home_dir()
.expect("can't determine home directory")
.join(".ayb.json")
Copy link
Contributor

@sofiaritz sofiaritz Jan 7, 2024

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.

Copy link
Owner Author

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! :)

}

impl ClientConfig {
fn default_config() -> ClientConfig {
Copy link
Contributor

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you and done!

@marcua marcua changed the title [WIP] Save auth token to local config Save auth token to local config Jan 8, 2024
base_url: url.to_string(),
api_token: token,
};
if let Some(matches) = matches.subcommand_matches("create_database") {
Copy link
Owner Author

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;
Copy link
Owner Author

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

@marcua marcua mentioned this pull request Jan 8, 2024
Copy link
Contributor

@sofiaritz sofiaritz left a 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 {
Copy link
Contributor

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?

Copy link
Owner Author

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.

Copy link
Contributor

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 :)


pub fn from_file(file_path: &PathBuf) -> Result<ClientConfig, std::io::Error> {
if file_path.exists() {
let file = File::open(file_path)?;
Copy link
Contributor

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

Copy link
Owner Author

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 :).

return Ok(serde_json::from_reader(&mut reader)?);
}

Ok(ClientConfig::new())
Copy link
Contributor

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

.parent()
.expect("unable to determine parent of ayb configuration directory"),
)?;
let file = File::create(file_path)?;
Copy link
Contributor

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Thank you!

Copy link
Contributor

@sofiaritz sofiaritz left a 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
Copy link
Contributor

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@marcua
Copy link
Owner Author

marcua commented Jan 9, 2024

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: ayb clean, I haven't seen it in too many CLIs, but we can look out for use cases for it and add it if we encounter them. What do you think?

@marcua marcua requested a review from sofiaritz January 9, 2024 00:24
Copy link
Contributor

@sofiaritz sofiaritz left a 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.

@marcua marcua changed the title Save auth token to local config Save API tokens and default server URL locally Jan 9, 2024
@marcua
Copy link
Owner Author

marcua commented Jan 10, 2024

Thank you so much @sofiaritz! :)

I like the ayb client clean idea. A nice uninstaller for when you no longer want it! We can keep our eyes out for people who ask for it. Thank you again!

@marcua marcua merged commit dcb7974 into main Jan 10, 2024
@marcua marcua deleted the client-save-tokens branch January 10, 2024 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make CLI save authentication tokens on registration/login
2 participants