Skip to content

Commit

Permalink
Add a whoami command to display the logged in user
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Engel committed Oct 19, 2017
1 parent 2f77974 commit 0e8afdf
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/bin/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ macro_rules! each_subcommand{
$mac!(update);
$mac!(verify_project);
$mac!(version);
$mac!(whoami);
$mac!(yank);
}
}
Expand Down
51 changes: 51 additions & 0 deletions src/bin/whoami.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use cargo::ops;
use cargo::util::{CliResult, Config};

#[derive(Deserialize)]
pub struct Options {
flag_index: Option<String>,
flag_verbose: u32,
flag_quiet: Option<bool>,
flag_color: Option<String>,
flag_frozen: bool,
flag_locked: bool,
#[serde(rename = "flag_Z")]
flag_z: Vec<String>,
}

pub const USAGE: &'static str = "
Check if an api token exists locally and who it belongs to
Usage:
cargo whoami [options] [<token>]
Options:
-h, --help Print this message
--index INDEX Registry index to search in
-v, --verbose ... Use verbose output (-vv very verbose/build.rs output)
-q, --quiet No output printed to stdout
--color WHEN Coloring: auto, always, never
--frozen Require Cargo.lock and cache are up to date
--locked Require Cargo.lock is up to date
-Z FLAG ... Unstable (nightly-only) flags to Cargo
";

pub fn execute(options: Options, config: &mut Config) -> CliResult {
config.configure(
options.flag_verbose,
options.flag_quiet,
&options.flag_color,
options.flag_frozen,
options.flag_locked,
&options.flag_z,
)?;

let Options {
flag_index: index,
..
} = options;

ops::registry_whoami(config, index)?;
Ok(())
}
2 changes: 1 addition & 1 deletion src/cargo/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use self::lockfile::{load_pkg_lockfile, write_pkg_lockfile};
pub use self::cargo_test::{run_tests, run_benches, TestOptions};
pub use self::cargo_package::{package, PackageOpts};
pub use self::registry::{publish, registry_configuration, RegistryConfig};
pub use self::registry::{registry_login, search, http_proxy_exists, http_handle};
pub use self::registry::{registry_login, registry_whoami, search, http_proxy_exists, http_handle};
pub use self::registry::{modify_owners, yank, OwnersOptions, PublishOpts};
pub use self::cargo_fetch::fetch;
pub use self::cargo_pkgid::pkgid;
Expand Down
12 changes: 12 additions & 0 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,18 @@ pub fn registry_login(config: &Config, token: String) -> CargoResult<()> {
config::save_credentials(config, token)
}

pub fn registry_whoami(config: &Config, index: Option<String>) -> CargoResult<()> {
let RegistryConfig { token, .. } = registry_configuration(config)?;
let (mut registry, _) = registry(config, token.clone(), index)?;

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

Ok(())
}

pub struct OwnersOptions {
pub krate: Option<String>,
pub token: Option<String>,
Expand Down
10 changes: 10 additions & 0 deletions src/crates-io/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub struct Warnings {
}

#[derive(Deserialize)] struct R { ok: bool }
#[derive(Deserialize)] struct MeResponse { user: Option<User> }
#[derive(Deserialize)] struct OwnerResponse { ok: bool, msg: String }
#[derive(Deserialize)] struct ApiErrorList { errors: Vec<ApiError> }
#[derive(Deserialize)] struct ApiError { detail: String }
Expand Down Expand Up @@ -256,6 +257,15 @@ impl Registry {
Ok(())
}

pub fn whoami(&mut self) -> Result<User> {
let body = self.get(String::from("/me"))?;

match serde_json::from_str::<MeResponse>(&body)?.user {
Some(user) => Ok(user),
_ => Err(Error::from_kind(ErrorKind::TokenMissing))
}
}

fn put(&mut self, path: String, b: &[u8]) -> Result<String> {
self.handle.put(true)?;
self.req(path, Some(b), Auth::Authorized)
Expand Down

0 comments on commit 0e8afdf

Please sign in to comment.