Commit a509450 1 parent 8c21073 commit a509450 Copy full SHA for a509450
File tree 5 files changed +83
-8
lines changed
5 files changed +83
-8
lines changed Original file line number Diff line number Diff line change @@ -32,8 +32,7 @@ fn auth_file() -> PathBuf {
32
32
}
33
33
34
34
pub fn authenticate_from_cache ( ) -> Option < Authentication > {
35
- match std:: fs:: File :: open ( auth_file ( ) ) {
36
- Ok ( f) => serde_json:: from_reader ( & f) . ok ( ) ,
37
- Err ( _) => None ,
38
- }
35
+ std:: fs:: File :: open ( auth_file ( ) )
36
+ . ok ( )
37
+ . and_then ( |f| serde_json:: from_reader ( f) . ok ( ) )
39
38
}
Original file line number Diff line number Diff line change
1
+ use std:: io:: ErrorKind ;
2
+
3
+ use anyhow:: Context ;
4
+ use clap:: Args ;
5
+
6
+ use crate :: config;
7
+
8
+ /// Clear cache, log
9
+ #[ derive( Args , Debug ) ]
10
+ pub struct ClearCommand { }
11
+
12
+ impl ClearCommand {
13
+ pub async fn run ( self ) {
14
+ let exit_code = if let Err ( err) = self . clear ( ) . await {
15
+ tracing:: error!( "{err}" ) ;
16
+ 1
17
+ } else {
18
+ 0
19
+ } ;
20
+
21
+ std:: process:: exit ( exit_code) ;
22
+ }
23
+
24
+ async fn clear ( self ) -> anyhow:: Result < ( ) > {
25
+ // remove cache
26
+ let cache_dir = config:: cache_dir ( ) ;
27
+ match std:: fs:: remove_dir_all ( cache_dir) {
28
+ Ok ( _) => {
29
+ tracing:: info!( "Clear {}" , cache_dir. display( ) ) ;
30
+ }
31
+ Err ( err) => match err. kind ( ) {
32
+ ErrorKind :: NotFound => { }
33
+ _ => {
34
+ return Err ( anyhow:: Error :: from ( err) )
35
+ . with_context ( || format ! ( "path: {}" , cache_dir. display( ) ) )
36
+ }
37
+ } ,
38
+ }
39
+
40
+ // remove log
41
+ let log_file = config:: log_path ( ) ;
42
+ match std:: fs:: remove_file ( & log_file) {
43
+ Ok ( _) => {
44
+ tracing:: info!( "Clear {}" , log_file. display( ) ) ;
45
+ }
46
+ Err ( err) => match err. kind ( ) {
47
+ ErrorKind :: NotFound => { }
48
+ _ => {
49
+ return Err ( anyhow:: Error :: from ( err) )
50
+ . with_context ( || format ! ( "path: {}" , log_file. display( ) ) )
51
+ }
52
+ } ,
53
+ }
54
+
55
+ Ok ( ( ) )
56
+ }
57
+ }
Original file line number Diff line number Diff line change 1
1
use std:: path:: PathBuf ;
2
2
3
- use clap:: Parser ;
3
+ use clap:: { Parser , Subcommand } ;
4
4
use url:: Url ;
5
5
6
6
use crate :: config;
7
7
8
+ mod clear;
9
+
8
10
#[ derive( Parser , Debug ) ]
9
11
#[ command( version, propagate_version = true , about = "xxx" ) ]
10
12
pub struct Args {
@@ -14,6 +16,13 @@ pub struct Args {
14
16
/// Log file path
15
17
#[ arg( long, default_value = config:: log_path( ) . into_os_string( ) ) ]
16
18
pub log : PathBuf ,
19
+ #[ command( subcommand) ]
20
+ pub command : Option < Command > ,
21
+ }
22
+
23
+ #[ derive( Subcommand , Debug ) ]
24
+ pub enum Command {
25
+ Clear ( clear:: ClearCommand ) ,
17
26
}
18
27
19
28
pub fn parse ( ) -> Args {
Original file line number Diff line number Diff line change 1
1
#![ allow( clippy:: new_without_default) ]
2
2
3
3
pub mod application;
4
- pub mod args;
5
4
pub mod auth;
5
+ pub mod cli;
6
6
pub mod client;
7
7
pub mod command;
8
8
pub mod config;
Original file line number Diff line number Diff line change @@ -3,8 +3,8 @@ use std::path::PathBuf;
3
3
use crossterm:: event:: EventStream ;
4
4
use syndterm:: {
5
5
application:: Application ,
6
- args:: { self , Args } ,
7
6
auth,
7
+ cli:: { self , Args } ,
8
8
client:: Client ,
9
9
terminal:: Terminal ,
10
10
} ;
@@ -49,10 +49,20 @@ fn init_tracing(log_path: PathBuf) -> anyhow::Result<WorkerGuard> {
49
49
50
50
#[ tokio:: main]
51
51
async fn main ( ) {
52
- let Args { endpoint, log } = args:: parse ( ) ;
52
+ let Args {
53
+ endpoint,
54
+ log,
55
+ command,
56
+ } = cli:: parse ( ) ;
53
57
54
58
let _guard = init_tracing ( log) . unwrap ( ) ;
55
59
60
+ #[ allow( clippy:: single_match) ]
61
+ match command {
62
+ Some ( cli:: Command :: Clear ( clear) ) => clear. run ( ) . await ,
63
+ None => { }
64
+ }
65
+
56
66
let mut app = {
57
67
let terminal = Terminal :: new ( ) . expect ( "Failed to construct terminal" ) ;
58
68
let client = Client :: new ( endpoint) . expect ( "Failed to construct client" ) ;
You can’t perform that action at this time.
0 commit comments