Skip to content

Commit a509450

Browse files
committed
feat: add clear command
1 parent 8c21073 commit a509450

File tree

5 files changed

+83
-8
lines changed

5 files changed

+83
-8
lines changed

syndterm/src/auth/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ fn auth_file() -> PathBuf {
3232
}
3333

3434
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())
3938
}

syndterm/src/cli/clear.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

syndterm/src/args.rs syndterm/src/cli/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::path::PathBuf;
22

3-
use clap::Parser;
3+
use clap::{Parser, Subcommand};
44
use url::Url;
55

66
use crate::config;
77

8+
mod clear;
9+
810
#[derive(Parser, Debug)]
911
#[command(version, propagate_version = true, about = "xxx")]
1012
pub struct Args {
@@ -14,6 +16,13 @@ pub struct Args {
1416
/// Log file path
1517
#[arg(long, default_value = config::log_path().into_os_string())]
1618
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),
1726
}
1827

1928
pub fn parse() -> Args {

syndterm/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![allow(clippy::new_without_default)]
22

33
pub mod application;
4-
pub mod args;
54
pub mod auth;
5+
pub mod cli;
66
pub mod client;
77
pub mod command;
88
pub mod config;

syndterm/src/main.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::path::PathBuf;
33
use crossterm::event::EventStream;
44
use syndterm::{
55
application::Application,
6-
args::{self, Args},
76
auth,
7+
cli::{self, Args},
88
client::Client,
99
terminal::Terminal,
1010
};
@@ -49,10 +49,20 @@ fn init_tracing(log_path: PathBuf) -> anyhow::Result<WorkerGuard> {
4949

5050
#[tokio::main]
5151
async fn main() {
52-
let Args { endpoint, log } = args::parse();
52+
let Args {
53+
endpoint,
54+
log,
55+
command,
56+
} = cli::parse();
5357

5458
let _guard = init_tracing(log).unwrap();
5559

60+
#[allow(clippy::single_match)]
61+
match command {
62+
Some(cli::Command::Clear(clear)) => clear.run().await,
63+
None => {}
64+
}
65+
5666
let mut app = {
5767
let terminal = Terminal::new().expect("Failed to construct terminal");
5868
let client = Client::new(endpoint).expect("Failed to construct client");

0 commit comments

Comments
 (0)