Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/pm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ indicatif = "0.17.8"
libc = "0.2"
libdeflater = "1.25"
once_cell = "1.19"
open = "5"
owo-colors = { workspace = true }
petgraph = "0.6"
rayon = { workspace = true }
Expand Down
23 changes: 23 additions & 0 deletions crates/pm/src/cmd/login.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use anyhow::Result;
use colored::Colorize;

use crate::service::auth;
use crate::util::user_config::get_registry;

pub async fn login() -> Result<()> {
let registry = get_registry();

println!("Login to {}", registry.cyan());

let token = auth::web_login(&registry, |url| {
println!("Login at: {}", url);
if let Err(e) = open::that(url) {
tracing::warn!("Failed to open browser: {e}");
}
})
.await?;

auth::save_token(&registry, token).await?;
println!("{}", "Logged in successfully.".green());
Ok(())
}
15 changes: 15 additions & 0 deletions crates/pm/src/cmd/logout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use anyhow::Result;
use colored::Colorize;

use crate::service::auth;
use crate::util::user_config::get_registry;

pub async fn logout() -> Result<()> {
let registry = get_registry();
let token = auth::require_token(&registry).await?;

auth::logout(&registry, &token).await?;

println!("Logged out from {}.", registry.green());
Ok(())
}
3 changes: 3 additions & 0 deletions crates/pm/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ pub mod init;
pub mod install;
pub mod link;
pub mod list;
pub mod login;
pub mod logout;
pub mod ping;
pub mod rebuild;
pub mod run;
pub mod update;
pub mod view;
pub mod whoami;
13 changes: 13 additions & 0 deletions crates/pm/src/cmd/whoami.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use anyhow::Result;

use crate::service::auth;
use crate::util::user_config::get_registry;

pub async fn whoami() -> Result<()> {
let registry = get_registry();
let token = auth::require_token(&registry).await?;

let username = auth::whoami(&registry, &token).await?;
println!("{}", username);
Ok(())
}
12 changes: 12 additions & 0 deletions crates/pm/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ pub mod cmd {
pub const PING_ALIAS: &str = "pg";
pub const PING_ABOUT: &str = "Ping npm registry";

pub const LOGIN_NAME: &str = "login";
pub const LOGIN_ALIAS: &str = "lg";
pub const LOGIN_ABOUT: &str = "Login to npm registry";

pub const WHOAMI_NAME: &str = "whoami";
pub const WHOAMI_ALIAS: &str = "who";
pub const WHOAMI_ABOUT: &str = "Display npm username";

pub const LOGOUT_NAME: &str = "logout";
pub const LOGOUT_ALIAS: &str = "lo";
pub const LOGOUT_ABOUT: &str = "Logout from npm registry";

pub const COMPLETIONS_NAME: &str = "completions";
pub const COMPLETIONS_ABOUT: &str = "Generate shell completion scripts\n\nAdd to your shell config:\n bash: echo 'eval \"$(utoo completions bash)\"' >> ~/.bashrc\n zsh: echo 'eval \"$(utoo completions zsh)\"' >> ~/.zshrc\n fish: utoo completions fish > ~/.config/fish/completions/utoo.fish";
}
27 changes: 23 additions & 4 deletions crates/pm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ use crate::constants::cmd::{
CLEAN_ABOUT, CLEAN_ALIAS, CLEAN_NAME, COMPLETIONS_ABOUT, COMPLETIONS_NAME, CONFIG_ABOUT,
CONFIG_ALIAS, CONFIG_NAME, DEPS_ABOUT, DEPS_ALIAS, DEPS_NAME, EXECUTE_ABOUT, EXECUTE_ALIAS,
EXECUTE_NAME, INIT_ABOUT, INIT_ALIAS, INIT_NAME, INSTALL_ABOUT, INSTALL_ALIAS, INSTALL_NAME,
LINK_ABOUT, LINK_ALIAS, LINK_NAME, LIST_ALIAS, LIST_NAME, PING_ABOUT, PING_ALIAS, PING_NAME,
REBUILD_ABOUT, REBUILD_ALIAS, REBUILD_NAME, RUN_ALIAS, RUN_NAME, UNINSTALL_ABOUT,
UNINSTALL_ALIAS, UNINSTALL_NAME, UPDATE_ABOUT, UPDATE_ALIAS, UPDATE_NAME, VIEW_ABOUT,
VIEW_ALIAS, VIEW_ALIAS_INFO, VIEW_ALIAS_SHOW, VIEW_NAME,
LINK_ABOUT, LINK_ALIAS, LINK_NAME, LIST_ALIAS, LIST_NAME, LOGIN_ABOUT, LOGIN_ALIAS, LOGIN_NAME,
LOGOUT_ABOUT, LOGOUT_ALIAS, LOGOUT_NAME, PING_ABOUT, PING_ALIAS, PING_NAME, REBUILD_ABOUT,
REBUILD_ALIAS, REBUILD_NAME, RUN_ALIAS, RUN_NAME, UNINSTALL_ABOUT, UNINSTALL_ALIAS,
UNINSTALL_NAME, UPDATE_ABOUT, UPDATE_ALIAS, UPDATE_NAME, VIEW_ABOUT, VIEW_ALIAS,
VIEW_ALIAS_INFO, VIEW_ALIAS_SHOW, VIEW_NAME, WHOAMI_ABOUT, WHOAMI_ALIAS, WHOAMI_NAME,
};
use crate::constants::{APP_ABOUT, APP_NAME, APP_VERSION};
use crate::helper::workspace::update_cwd_to_root;
Expand Down Expand Up @@ -265,6 +266,15 @@ enum Commands {
registry: Option<String>,
},

#[command(name = LOGIN_NAME, alias = LOGIN_ALIAS, about = LOGIN_ABOUT)]
Login,

#[command(name = WHOAMI_NAME, alias = WHOAMI_ALIAS, about = WHOAMI_ABOUT)]
Whoami,

#[command(name = LOGOUT_NAME, alias = LOGOUT_ALIAS, about = LOGOUT_ABOUT)]
Logout,

#[command(name = CONFIG_NAME, alias = CONFIG_ALIAS, about = CONFIG_ABOUT)]
Config {
#[command(subcommand)]
Expand Down Expand Up @@ -544,6 +554,15 @@ async fn async_main() -> Result<()> {
Some(Commands::Ping { registry }) => {
cmd::ping::ping(registry.as_deref()).await?;
}
Some(Commands::Login) => {
cmd::login::login().await?;
}
Some(Commands::Whoami) => {
cmd::whoami::whoami().await?;
}
Some(Commands::Logout) => {
cmd::logout::logout().await?;
}
Some(Commands::Config { command }) => match command {
ConfigCommands::Set { key, value, global } => {
handle_config_set(key, value, global).await?;
Expand Down
Loading