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

Set token via qlty auth login --token <TOKEN> #1407

Merged
merged 3 commits into from
Jan 7, 2025
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
35 changes: 32 additions & 3 deletions qlty-cli/src/commands/auth/login.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
use crate::{Arguments, CommandError, CommandSuccess};
use anyhow::Result;
use anyhow::{Context, Result};
use clap::Args;
use qlty_cloud::load_or_retrieve_auth_token;
use console::style;
use dialoguer::Input;
use qlty_cloud::{load_or_retrieve_auth_token, store_auth_token};

#[derive(Args, Debug)]
pub struct Login {}
pub struct Login {
/// Provide a CLI token manually or use "-" to read from standard input (see https://qlty.sh/user/settings/cli)
Copy link
Member

Choose a reason for hiding this comment

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

This help string may be a bit long and cause wrapping. I generally try to keep these to a length of ~60-70 max

Copy link
Member Author

Choose a reason for hiding this comment

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

The wrapping is handled by clap:

Options:
      --token <TOKEN>     Provide a CLI token manually or use "-" to read from standard input (see
                          https://qlty.sh/user/settings/cli)

Copy link
Member

Choose a reason for hiding this comment

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

Ah lovely, didn't realize that. Thanks

#[arg(long)]
pub token: Option<String>,
}

impl Login {
pub fn execute(&self, _args: &Arguments) -> Result<CommandSuccess, CommandError> {
if let Some(mut token) = self.token.clone() {
if token == "" || token == "-" {
eprintln!(
"Generate a token from {} and paste it here.",
style("https://qlty.sh/user/settings/cli")
.underlined()
.green()
);
token = Input::<String>::new()
.with_prompt("Token")
.interact_text()
.map(|line| line.trim().to_string())
.with_context(|| "Invalid input")?;
}

if !token.starts_with("qltyp_") || token.len() < 32 {
return Err(CommandError::new("Token is invalid"));
}

store_auth_token(&token)?;
eprintln!("{}", style("Token saved successfully.").green());
return CommandSuccess::ok();
}
load_or_retrieve_auth_token()?;
CommandSuccess::ok()
}
Expand Down
19 changes: 13 additions & 6 deletions qlty-cloud/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ use crate::Client;
use anyhow::Result;
use auth_flow::{launch_login_server, AppState};
use console::style;
use credentials::{delete_token, read_token};
use std::{thread, time::Duration};
use credentials::read_token;
pub use credentials::{delete_token as clear_auth_token, write_token as store_auth_token};
use std::{env, thread, time::Duration};
use tracing::{info, warn};

const TOKEN_ENV_VAR: &str = "QLTY_TOKEN";

pub fn load_or_retrieve_auth_token() -> Result<String> {
if let Ok(token) = env::var(TOKEN_ENV_VAR) {
let token = token.trim().to_string();
if !token.is_empty() {
// bypass validation when env var is set since this is an intentional override of credential lookup
return Ok(token);
}
}

let mut has_token = false;
let auth_token = match read_token() {
Ok(token) => {
Expand All @@ -32,10 +43,6 @@ pub fn load_or_retrieve_auth_token() -> Result<String> {
}
}

pub fn clear_auth_token() -> Result<()> {
delete_token()
}

fn validate_auth_token(auth_token: &String) -> Result<()> {
Client::new(None, Some(auth_token.into()))
.get("/user")
Expand Down
2 changes: 1 addition & 1 deletion qlty-cloud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod export;
pub mod format;

use anyhow::Result;
pub use auth::{clear_auth_token, load_or_retrieve_auth_token};
pub use auth::{clear_auth_token, load_or_retrieve_auth_token, store_auth_token};
use qlty_config::version::QLTY_VERSION;
use ureq::Request;

Expand Down
Loading