Commit 3a6d2d2 1 parent feefcf9 commit 3a6d2d2 Copy full SHA for 3a6d2d2
File tree 3 files changed +46
-10
lines changed
qlty-cli/src/commands/auth
3 files changed +46
-10
lines changed Original file line number Diff line number Diff line change 1
1
use crate :: { Arguments , CommandError , CommandSuccess } ;
2
- use anyhow:: Result ;
2
+ use anyhow:: { Context , Result } ;
3
3
use clap:: Args ;
4
- use qlty_cloud:: load_or_retrieve_auth_token;
4
+ use console:: style;
5
+ use dialoguer:: Input ;
6
+ use qlty_cloud:: { load_or_retrieve_auth_token, store_auth_token} ;
5
7
6
8
#[ derive( Args , Debug ) ]
7
- pub struct Login { }
9
+ pub struct Login {
10
+ /// Provide a CLI token manually or use "-" to read from standard input (see https://qlty.sh/user/settings/cli)
11
+ #[ arg( long) ]
12
+ pub token : Option < String > ,
13
+ }
8
14
9
15
impl Login {
10
16
pub fn execute ( & self , _args : & Arguments ) -> Result < CommandSuccess , CommandError > {
17
+ if let Some ( mut token) = self . token . clone ( ) {
18
+ if token == "" || token == "-" {
19
+ eprintln ! (
20
+ "Generate a token from {} and paste it here." ,
21
+ style( "https://qlty.sh/user/settings/cli" )
22
+ . underlined( )
23
+ . green( )
24
+ ) ;
25
+ token = Input :: < String > :: new ( )
26
+ . with_prompt ( "Token" )
27
+ . interact_text ( )
28
+ . map ( |line| line. trim ( ) . to_string ( ) )
29
+ . with_context ( || "Invalid input" ) ?;
30
+ }
31
+
32
+ if !token. starts_with ( "qltyp_" ) || token. len ( ) < 32 {
33
+ return Err ( CommandError :: new ( "Token is invalid" ) ) ;
34
+ }
35
+
36
+ store_auth_token ( & token) ?;
37
+ eprintln ! ( "{}" , style( "Token saved successfully." ) . green( ) ) ;
38
+ return CommandSuccess :: ok ( ) ;
39
+ }
11
40
load_or_retrieve_auth_token ( ) ?;
12
41
CommandSuccess :: ok ( )
13
42
}
Original file line number Diff line number Diff line change @@ -5,11 +5,22 @@ use crate::Client;
5
5
use anyhow:: Result ;
6
6
use auth_flow:: { launch_login_server, AppState } ;
7
7
use console:: style;
8
- use credentials:: { delete_token, read_token} ;
9
- use std:: { thread, time:: Duration } ;
8
+ use credentials:: read_token;
9
+ pub use credentials:: { delete_token as clear_auth_token, write_token as store_auth_token} ;
10
+ use std:: { env, thread, time:: Duration } ;
10
11
use tracing:: { info, warn} ;
11
12
13
+ const TOKEN_ENV_VAR : & str = "QLTY_TOKEN" ;
14
+
12
15
pub fn load_or_retrieve_auth_token ( ) -> Result < String > {
16
+ if let Ok ( token) = env:: var ( TOKEN_ENV_VAR ) {
17
+ let token = token. trim ( ) . to_string ( ) ;
18
+ if !token. is_empty ( ) {
19
+ // bypass validation when env var is set since this is an intentional override of credential lookup
20
+ return Ok ( token) ;
21
+ }
22
+ }
23
+
13
24
let mut has_token = false ;
14
25
let auth_token = match read_token ( ) {
15
26
Ok ( token) => {
@@ -32,10 +43,6 @@ pub fn load_or_retrieve_auth_token() -> Result<String> {
32
43
}
33
44
}
34
45
35
- pub fn clear_auth_token ( ) -> Result < ( ) > {
36
- delete_token ( )
37
- }
38
-
39
46
fn validate_auth_token ( auth_token : & String ) -> Result < ( ) > {
40
47
Client :: new ( None , Some ( auth_token. into ( ) ) )
41
48
. get ( "/user" )
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ pub mod export;
3
3
pub mod format;
4
4
5
5
use anyhow:: Result ;
6
- pub use auth:: { clear_auth_token, load_or_retrieve_auth_token} ;
6
+ pub use auth:: { clear_auth_token, load_or_retrieve_auth_token, store_auth_token } ;
7
7
use qlty_config:: version:: QLTY_VERSION ;
8
8
use ureq:: Request ;
9
9
You can’t perform that action at this time.
0 commit comments