feat: Initialize UPFS project with login and file upload functionality
- Add Cargo.toml with dependencies for reqwest, serde, tokio, and clap. - Implement login functionality to retrieve authentication token. - Create a module for handling file uploads with multipart support. - Set up command-line interface for file upload operations. - Include error handling for file existence and HTTP requests.
This commit is contained in:
83
src/main.rs
Normal file
83
src/main.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
mod login;
|
||||
mod update;
|
||||
|
||||
use clap::Parser;
|
||||
use login::login_and_get_token;
|
||||
use update::upload_file;
|
||||
use std::process;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "upfs")]
|
||||
#[command(about = "Upload file to UPFS server")]
|
||||
struct Cli {
|
||||
/// File path to upload
|
||||
#[arg(short, long)]
|
||||
file: String,
|
||||
|
||||
/// Remote file path on server
|
||||
#[arg(short, long)]
|
||||
remote_path: String,
|
||||
|
||||
/// Username for authentication
|
||||
#[arg(short, long, default_value = "admin")]
|
||||
username: String,
|
||||
|
||||
/// Password for authentication
|
||||
#[arg(short, long)]
|
||||
password: Option<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let cli = Cli::parse();
|
||||
|
||||
// 检查文件是否存在
|
||||
if !std::path::Path::new(&cli.file).exists() {
|
||||
eprintln!("错误: 文件 '{}' 不存在", cli.file);
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
// 获取密码(如果没有提供则询问)
|
||||
let password = match cli.password {
|
||||
Some(pwd) => pwd,
|
||||
None => {
|
||||
println!("请输入密码:");
|
||||
let mut input = String::new();
|
||||
std::io::stdin().read_line(&mut input).expect("读取密码失败");
|
||||
input.trim().to_string()
|
||||
}
|
||||
};
|
||||
|
||||
println!("正在登录服务器...");
|
||||
|
||||
// 登录获取token
|
||||
let token = match login_and_get_token(cli.username, password).await {
|
||||
Ok(token) => {
|
||||
println!("登录成功!");
|
||||
token
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("登录失败: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
println!("正在上传文件: {} 到远程路径: {}", cli.file, cli.remote_path);
|
||||
|
||||
// 上传文件
|
||||
match upload_file(token, &cli.file, &cli.remote_path).await {
|
||||
Ok((true, response)) => {
|
||||
println!("✅ 文件上传成功!");
|
||||
println!("服务器响应: {}", response);
|
||||
}
|
||||
Ok((false, response)) => {
|
||||
println!("❌ 文件上传失败!");
|
||||
println!("服务器响应: {}", response);
|
||||
process::exit(1);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("上传过程中发生错误: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user