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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use anyhow::Result;
use clap::{Parser, Subcommand};
use log::{error, info};
use std::path::PathBuf;
use xtool::{config, disk, file, http, serial, tftp};
#[derive(Parser)]
#[command(name = "xtool")]
#[command(version, about = "Amazing Tools", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Start a TFTP server
Tftpd {
/// IP address to listen on
#[arg(short, long, default_value = "0.0.0.0")]
ip: String,
/// Port to listen on
#[arg(short, long, default_value = "69")]
port: u16,
/// Root directory for TFTP files
#[arg(value_name = "PATH")]
path: PathBuf,
/// Enable read-only mode
#[arg(short, long)]
read_only: bool,
/// Use single port mode (useful for NAT environments)
#[arg(short, long)]
single_port: bool,
},
/// TFTP client - download or upload files
Tftpc {
#[command(subcommand)]
action: tftp::client::TftpcAction,
},
/// File transfer - upload and download files
File {
#[command(subcommand)]
action: file::FileAction,
},
/// Serial port tools - specify port to monitor, or use 'list' command
Serial {
/// Serial port name (e.g., COM1 or /dev/ttyUSB0). If not provided, will try to use config.
#[arg(value_name = "UART")]
uart: Option<String>,
/// Baud rate
#[arg(short, long)]
baud: Option<u32>,
#[command(subcommand)]
subcommand: Option<serial::SerialSubcommand>,
},
/// Generate configuration file (.xtool.toml) in current directory
Genconfig {
/// Force overwrite existing configuration file
#[arg(long)]
force: bool,
},
/// Start a HTTP static file server
Http {
/// Port to listen on
#[arg(short, long, default_value = "80")]
port: u16,
/// Root directory to serve
#[arg(short = 'd', long, default_value = ".")]
path: PathBuf,
},
/// Disk image utilities
Disk(disk::DiskCli),
}
fn main() -> Result<()> {
// Initialize logger, default info level, display file line number and time
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
.format(|buf, record| {
use std::io::Write;
let level_style = buf.default_level_style(record.level());
writeln!(
buf,
"[{} {level_style}{}{level_style:#} {}:{}] {level_style}{}{level_style:#}",
chrono::Local::now().format("%H:%M:%S"),
record.level(),
record.target(),
record.line().unwrap_or(0),
record.args()
)
})
.init();
let cli = Cli::parse();
// Try to load configuration file
let config_path = ".xtool.toml";
let app_config = if std::path::Path::new(config_path).exists() {
match config::AppConfig::load_from_file(config_path) {
Ok(cfg) => {
let abs_path = std::fs::canonicalize(config_path)
.unwrap_or_else(|_| std::path::PathBuf::from(config_path));
info!("Using configuration file: {}", abs_path.display());
Some(cfg)
}
Err(e) => {
error!("Failed to load configuration file: {}, using defaults", e);
None
}
}
} else {
None
};
match cli.command {
Commands::Tftpd {
ip,
port,
path,
read_only,
single_port,
} => {
tftp::server::run_with_config(
ip,
port,
path,
read_only,
single_port,
app_config.as_ref().and_then(|c| c.tftpd.clone()),
)?;
}
Commands::Tftpc { action } => {
// Client configuration merging is handled inside client::run_with_config
tftp::client::run_with_config(
action,
app_config.as_ref().and_then(|c| c.tftpc.as_ref()),
)?;
}
Commands::File { action } => {
file::run(action)?;
}
Commands::Serial {
uart,
baud,
subcommand,
} => {
serial::run(
subcommand,
uart,
baud,
app_config.as_ref().and_then(|c| c.serial.clone()),
)?;
}
Commands::Genconfig { force } => {
if let Err(e) = config::AppConfig::generate_config_file(force) {
error!("Error: {}", e);
std::process::exit(1);
}
}
Commands::Http { port, path } => {
http::run(port, path)?;
}
Commands::Disk(cmd) => {
disk::run(cmd)?;
}
}
Ok(())
}