1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#![recursion_limit = "512"]
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use vflight::{fetch, metrics, seed, throttle};
#[derive(Parser)]
#[command(name = "vflight")]
#[command(about = "Share files over the Veilid network")]
struct Cli {
/// Print performance metrics summary after command completes
#[arg(long, global = true)]
metrics: bool,
/// Use insecure local fallback storage (for WSL/testing only)
#[arg(long, global = true)]
insecure_local_fallback: bool,
/// Encrypt chunks with a password (recommended for sensitive files). Falls back to VFLIGHT_PASSWORD env var.
#[arg(long, short = 'p', global = true)]
password: Option<String>,
/// Number of parallel chunk downloads (default: 8)
#[arg(long, default_value = "8", global = true)]
parallel: usize,
/// Disable resumable downloads (start fresh)
#[arg(long, global = true)]
no_resume: bool,
/// Trust cached chunks without re-verification (faster resume)
#[arg(long, global = true)]
trust_cache: bool,
/// Limit transfer speed in KB/s for both seed and fetch (0 = unlimited)
#[arg(long, default_value = "0", global = true)]
throttle: u64,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Seed a file to the network
Seed {
/// Path to the file to seed
file: PathBuf,
/// Compress the file with zstd before chunking
#[arg(long)]
compress: bool,
},
/// Fetch a file from the network
Fetch {
/// DHT key of the file to fetch
dht_key: String,
/// Output directory (defaults to current directory)
#[arg(default_value = ".")]
output_dir: PathBuf,
},
}
#[tokio::main]
async fn main() -> Result<()> {
// Initialize tracing with info as default level
// Can be overridden via RUST_LOG environment variable
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
tracing_subscriber::fmt().with_env_filter(env_filter).init();
let cli = Cli::parse();
let throttler = throttle::Throttler::new(cli.throttle).map(std::sync::Arc::new);
// CLI flag takes precedence; fall back to VFLIGHT_PASSWORD env var
let password = cli
.password
.or_else(|| std::env::var("VFLIGHT_PASSWORD").ok());
let result = match cli.command {
Commands::Seed { file, compress } => {
if !file.exists() {
anyhow::bail!("File not found: {}", file.display());
}
seed::seed_file(
&file,
cli.insecure_local_fallback,
password.as_deref(),
compress,
throttler.clone(),
)
.await
}
Commands::Fetch {
dht_key,
output_dir,
} => {
fetch::fetch_file(
&dht_key,
&output_dir,
cli.insecure_local_fallback,
password.as_deref(),
cli.parallel,
cli.no_resume,
cli.trust_cache,
throttler.clone(),
)
.await
}
};
// Print metrics summary if enabled (even on error)
if cli.metrics {
metrics::global_metrics().print_summary();
}
result
}