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

Add a whoami command to display the logged in user #4625

Closed
wants to merge 1 commit into from

Conversation

mike-engel
Copy link

This adds a new command to cargo called whoami, which will display the login name of the user that belongs to the saved credential.

I realize I did this without making an issue first to discuss it—sorry about that. I'm also aware that cargo has a built in plugin system, but a whoami command felt like it should be in cargo itself. If y'all feel differently, feel free to close this PR and I'll create a cargo plugin.

I also attempted to write some integration tests, but wasn't able to successfully. It seems like most commands are covered by tests but I might have been searching wrong. If tests are desired, let me know and I can take another stab at it.

Finally, if there is anything I should change, add, remove, etc, please let me know and I'll be happy to do that!

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@alexcrichton
Copy link
Member

Thanks for the PR! This sounds like a good idea to me, so I'm fine adding it. I think it's ok to skip tests as this does indeed seem like it'd be hard to test, but I imagine you've verified it locally :)

@rust-lang/cargo, thoughts on this new subcommand?

@mike-engel
Copy link
Author

@alexcrichton Sweet! I did test it locally, with both scenarios. I saw there were some tests that mocked network responses like the search tests, but I couldn't get those to work for the /api/v1/me route for some reason.

let (mut registry, _) = registry(config, token.clone(), index)?;

match registry.whoami() {
Ok(user) => config.shell().status("Whoami", format!("Currently logged in as `{}`", user.login))?,
Copy link
Contributor

@lukaslueg lukaslueg Oct 26, 2017

Choose a reason for hiding this comment

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

I wonder if this wording is optimal. "Currently logged in as", given to a web-accustomed audience, sounds like the status of a temporary session. We could also be more clear about where this user is actually used.

FWIW "Crates will be published/yanked by user `{}`"

Copy link
Author

Choose a reason for hiding this comment

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

@lukaslueg I get where you're coming from, and normally I'd agree. Two things are hanging me up though. The first is that users already establish local credentials by running cargo login (normally), so there's already some precedent there. Additionally, while publish/yank are probably the two most common commands, there's certainly more to do that requires authentication.

If you wanted to move away from logged in, maybe Authenticated as {} or Operating as {}?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest mirroring what whoami or id -un does and just print out the username, with no further formatting. This has the added benefit that the command can be used in scripts more easily, without having to do a awk-pass. That is

$ cargo whoami
leeroyjenkins

and

$ cargo whoami
[some TokenMissing-message to stderr]
$ echo $?
1

Copy link
Author

Choose a reason for hiding this comment

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

I thought about that as well while implementing this. The main reason I didn't is because it doesn't match how the rest of cargo generally outputs info to STDOUT

Copy link
Contributor

Choose a reason for hiding this comment

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

#4646 comes with a small change in config.shell() to determine if stderr is actually human-readable. One can extend that to stdout and output an unformatted string if stdout is hooked up. If wonder if this is surprising behavior, though

@withoutboats
Copy link
Contributor

withoutboats commented Oct 31, 2017

We discussed this PR during the cargo team meeting today. While we don't have anything particular against including a whoami subcommand, we'd like to adopt a more incremental approach to introducing new subcommands, analogous to the way the libs nursery works today.

That is, we'd strongly prefer all new subcommands to be either:

  • An essential component of a larger feature which has had an RFC accepted.
  • Provided through the plugin system at first, and integrated into cargo after their popularity/utility has been demonstrated.

We also discussed improving the discoverability of plugins by having cargo suggest that you install the plugin if you use a command that exists as a plugin, e.g:

$ cargo whoami
error: no such subcommand: `whoami`

info: plugin `cargo-whoami` exists; you can install it with `cargo install cargo-whoami`

@joshtriplett opened an issue to discuss this discoverability feature at #4682.

Thanks for the PR though! If you feel up to it, please do publish a cargo-whoami as a plugin and in the future we'll revisit upstreaming it into cargo proper.

@mike-engel
Copy link
Author

Thanks @withoutboats for the breakdown. Once I find some time, I'll definitely work on making this a plugin.

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.

5 participants