mirror of
https://codeberg.org/demostf/cleanup.git
synced 2026-06-03 10:04:10 +02:00
proper config file
This commit is contained in:
parent
6b65ee144c
commit
6cf28c4504
4 changed files with 1211 additions and 470 deletions
1623
Cargo.lock
generated
1623
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
16
Cargo.toml
16
Cargo.toml
|
|
@ -5,8 +5,14 @@ authors = ["Robin Appelman <robin@icewind.nl>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
demostf-client = "0.2"
|
demostf-client = "0.4.6"
|
||||||
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1.49.0", features = ["macros", "rt-multi-thread"] }
|
||||||
chrono = "0.4"
|
time = "0.3.47"
|
||||||
main_error = "0.1"
|
main_error = "0.1.2"
|
||||||
dotenv = "0.15"
|
clap = { version = "4.5.57", features = ["derive"] }
|
||||||
|
tracing = "0.1.44"
|
||||||
|
tracing-subscriber = "0.3.22"
|
||||||
|
toml = "0.9.8"
|
||||||
|
thiserror = "2.0.18"
|
||||||
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
|
secretfile = "0.1.1"
|
||||||
59
src/config.rs
Normal file
59
src/config.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
use demostf_client::ApiClient;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use std::fs::read_to_string;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum ConfigError {
|
||||||
|
#[error("Failed to read config from {path}: {error}")]
|
||||||
|
Read { error: std::io::Error, path: String },
|
||||||
|
#[error("Failed to parse config from {path}: {error}")]
|
||||||
|
Parse {
|
||||||
|
error: toml::de::Error,
|
||||||
|
path: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct Config {
|
||||||
|
pub api: ApiConfig,
|
||||||
|
pub storage: StorageConfig,
|
||||||
|
pub cleanup: CleanupConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn load(path: String) -> Result<Self, ConfigError> {
|
||||||
|
let content = read_to_string(&path).map_err(|error| ConfigError::Read {
|
||||||
|
error,
|
||||||
|
path: path.clone(),
|
||||||
|
})?;
|
||||||
|
toml::from_str(&content).map_err(|error| ConfigError::Parse { error, path })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct ApiConfig {
|
||||||
|
#[serde(default = "default_api_base")]
|
||||||
|
pub url: String,
|
||||||
|
pub key_file: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_api_base() -> String {
|
||||||
|
ApiClient::DEMOS_TF_BASE_URL.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct CleanupConfig {
|
||||||
|
#[serde(default = "default_from_backend")]
|
||||||
|
pub from_backend: String,
|
||||||
|
pub age: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_from_backend() -> String {
|
||||||
|
"freezer".into()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct StorageConfig {
|
||||||
|
pub root: String,
|
||||||
|
}
|
||||||
41
src/main.rs
41
src/main.rs
|
|
@ -1,44 +1,65 @@
|
||||||
use chrono::{Duration, Utc};
|
mod config;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
use clap::Parser;
|
||||||
use demostf_client::{ApiClient, ListOrder, ListParams};
|
use demostf_client::{ApiClient, ListOrder, ListParams};
|
||||||
use main_error::MainError;
|
use main_error::MainError;
|
||||||
use std::fs::remove_file;
|
use std::fs::remove_file;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use time::{Duration, OffsetDateTime};
|
||||||
|
use tracing::{error, info, info_span};
|
||||||
|
|
||||||
|
#[derive(Debug, Parser)]
|
||||||
|
struct Args {
|
||||||
|
/// Config file
|
||||||
|
config: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), MainError> {
|
async fn main() -> Result<(), MainError> {
|
||||||
let key = dotenv::var("DEMOSTF_KEY").expect("DEMOSTF_KEY not set");
|
tracing_subscriber::fmt::init();
|
||||||
let root = PathBuf::from(dotenv::var("DEMOS_ROOT").expect("DEMOS_ROOT not set"));
|
|
||||||
|
|
||||||
let client = ApiClient::new();
|
let args = Args::parse();
|
||||||
|
let config = Config::load(args.config)?;
|
||||||
|
let api_key = load(&config.api.key_file)?;
|
||||||
|
|
||||||
|
let client = ApiClient::with_base_url(config.api.url)?;
|
||||||
|
|
||||||
let demos = client
|
let demos = client
|
||||||
.list(
|
.list(
|
||||||
ListParams::default()
|
ListParams::default()
|
||||||
.with_order(ListOrder::Ascending)
|
.with_order(ListOrder::Ascending)
|
||||||
.with_backend("freezer"),
|
.with_backend(&config.cleanup.from_backend),
|
||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let cutoff_time = Utc::now() - Duration::days(3 * 365);
|
let cutoff_time = OffsetDateTime::now_utc() - Duration::seconds(config.cleanup.age as i64);
|
||||||
|
let root = PathBuf::from(config.storage.root);
|
||||||
|
|
||||||
for demo in demos {
|
for demo in demos {
|
||||||
if demo.time > cutoff_time {
|
if demo.time > cutoff_time {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
let _span = info_span!("processing", id = demo.id).entered();
|
||||||
|
|
||||||
let path = root.join(&demo.path.trim_start_matches('/'));
|
let path = root.join(
|
||||||
|
demo.path
|
||||||
|
.trim_start_matches('/')
|
||||||
|
.trim_start_matches("demos/"),
|
||||||
|
);
|
||||||
|
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
eprintln!("Demo not found: {}", path.to_str().unwrap());
|
error!(path = ?path, "Demo not found");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
info!("removing");
|
||||||
client
|
client
|
||||||
.set_url(demo.id, "deleted", "", "", demo.hash, &key)
|
.set_url(demo.id, "deleted", "", "", demo.hash, &api_key)
|
||||||
.await?;
|
.await?;
|
||||||
remove_file(&path)?;
|
remove_file(&path)?;
|
||||||
println!("{} {}", demo.id, demo.name);
|
info!("{} {}", demo.id, demo.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue