Uploading all available versions

This commit is contained in:
2025-08-08 23:13:28 +03:00
parent af84517ce3
commit e1881782fe
+187 -80
View File
@@ -1,19 +1,20 @@
use clap::{Args, Parser, Subcommand};
use clap::{Parser, Subcommand};
use dirs_next::config_dir;
use reqwest::{Client, multipart};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::fs;
use std::fs::File;
use std::io::{stdout, BufReader, BufWriter, Read, Write};
use std::io::{BufReader, BufWriter, Read, Write, stdout};
use std::path::{Path, PathBuf};
use std::process::exit;
use std::str::FromStr;
use std::string::ToString;
use std::time::Duration;
use std::{fs, thread};
/// Nuget packages manager
#[derive(Parser, Debug)]
#[derive(Parser, Debug, Clone)]
#[command(version, about, long_about = None)]
struct CliArgs {
/// Path to .nupkg file to send
@@ -30,7 +31,7 @@ struct CliArgs {
command: Option<Commands>,
}
#[derive(Subcommand, Debug)]
#[derive(Subcommand, Debug, Clone)]
enum Commands {
/// does testing things
Test {
@@ -44,20 +45,25 @@ enum Commands {
},
/// Forget personal nuget api key
Logout,
/// Show current configuration
ShowCfg,
/// Check newer packets versions
Check,
Publish,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Configuration {
key: Option<String>,
packets: Vec<Packet>,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Packet {
key: String,
version: Version,
path: String,
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
struct Version {
major: u32,
minor: u32,
@@ -79,7 +85,13 @@ impl Version {
let c = u32::from_str(parts.pop().unwrap()).unwrap();
let b = u32::from_str(parts.pop().unwrap()).unwrap();
let a = u32::from_str(parts.pop().unwrap()).unwrap();
Version::new(c, b, a)
Version::new(a, b, c)
}
}
impl Display for Version {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}
@@ -113,7 +125,7 @@ impl Packet {
let a = u32::from_str(parts.pop().unwrap()).unwrap();
let name = parts.join(".").to_string();
println!("{}", name);
let dir = path.parent().unwrap().to_str().unwrap();
Packet {
key: name,
@@ -164,14 +176,15 @@ fn try_remember_packet(path: &Path, config: &mut Configuration) {
{
Some(packet) => {
let version = Version::from_name(&path.file_name().unwrap().to_str().unwrap());
if packet.version < version {
let more = packet.version < version;
if more {
packet.version = version;
}
}
None => config.packets.push(Packet::new(path)),
}
println!("{:?}", config)
write_config(config.clone());
}
fn conv(str: &mut String) {
@@ -179,10 +192,73 @@ fn conv(str: &mut String) {
str.insert(0, last);
}
async fn send_packet(path: &String, args: CliArgs) -> bool {
let api_key: String;
let config = &mut read_config();
if let Some(key) = args.key {
api_key = key;
} else if let Some(key) = &config.key {
api_key = (*key).clone();
} else {
println!("Api key is not defined! Use -k [key]");
exit(4)
}
let form = multipart::Form::new().file("", path.clone()).await.unwrap();
let req_thread = tokio::spawn(async {
let client = Client::new();
let response_process = client
.put("https://www.nuget.org/api/v2/package/")
.header("X-NuGet-ApiKey", api_key)
.header("X-NuGet-Client-Version", "4.1.0")
.multipart(form)
.send();
response_process.await
});
let mut ind = "---->---->---->".to_string();
print!(
"{} [{}] https://www.nuget.org/api/v2/package/{}",
path,
ind,
"\x08".repeat(54)
);
while !req_thread.is_finished() {
print!("{}", ind);
stdout().flush().unwrap();
print!("\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08");
stdout().flush().unwrap();
conv(&mut ind);
tokio::time::sleep(Duration::from_millis(100)).await;
}
println!();
let response = req_thread.await.unwrap();
match response {
Ok(response) => {
let status = response.status();
println!("Response: [{}] {}", status, response.text().await.unwrap());
if !status.is_success() {
// return false;
}
if !args.overlook {
try_remember_packet(&Path::new(&path), config);
}
true
}
Err(error) => {
println!("Error: {:?}", error.status());
false
}
}
}
#[tokio::main]
async fn main() {
let args = CliArgs::parse();
let args_clone = args.clone();
let config_folder = config_dir()
.expect("Could not determine config directory")
.join("numan");
@@ -191,7 +267,62 @@ async fn main() {
fs::create_dir_all(&config_folder).unwrap()
}
if let Some(path) = args.path {
if let Some(_) = args.path {
single_packet(args_clone).await;
return;
}
match &args.command {
Some(Commands::Test { list }) => {
if *list {
println!("Printing testing lists...");
} else {
println!("Not printing testing lists...");
}
}
Some(Commands::Auth { key }) => {
regular_key(key);
}
Some(Commands::ShowCfg) => {
show_config();
}
Some(Commands::Logout) => {
logout();
}
Some(Commands::Check) => check(),
Some(Commands::Publish) => {
publish(&args_clone).await;
}
None => {}
}
}
async fn publish(args: &CliArgs) {
println!("Publishing newer versions");
let config = read_config();
for packet in config.packets {
let path = packet.path;
print!("{} ({}) Current: [{}]", packet.key, path, packet.version);
let versions = find_packets(Path::new(&path));
if let Some(newer) = versions.iter().find(|v| v.version > packet.version) {
println!(" -> [{}]", newer.version);
println!("{:?}", newer);
send_packet(
&format!("{}/{}.{}.nupkg", newer.path, newer.key, newer.version),
args.clone(),
)
.await;
} else {
println!()
}
}
}
async fn single_packet(args: CliArgs) {
let args_clone = args.clone();
let path = args_clone.clone().path.unwrap();
let package = Path::new(&path);
if !package.exists() {
println!("File {} does not exist", path);
@@ -207,38 +338,12 @@ async fn main() {
exit(3);
}
let api_key: String;
send_packet(&path, args_clone).await;
let mut config = read_config();
if let Some(key) = args.key {
api_key = key;
} else if let Some(key) = &config.key {
api_key = (*key).clone();
} else {
println!("Api key is not defined! Use -k [key]");
exit(4)
}
let success = send_packet(&path, api_key).await;
if !args.overlook && success{
try_remember_packet(&Path::new(&path), &mut config);
}
exit(0);
}
match &args.command {
Some(Commands::Test { list }) => {
if *list {
println!("Printing testing lists...");
} else {
println!("Not printing testing lists...");
}
}
Some(Commands::Auth { key }) => {
fn regular_key(key: &String) {
println!("Authenticating... {}", key);
let mut current = read_config();
@@ -247,7 +352,7 @@ async fn main() {
write_config(current);
}
Some(Commands::ShowCfg) => {
fn show_config() {
let config = config_path();
let mut ctg = String::new();
@@ -263,52 +368,54 @@ async fn main() {
}
println!("{}", ctg);
}
Some(Commands::Logout) => {
fn logout() {
let mut config = read_config();
config.key = None;
write_config(config);
}
None => {}
fn find_packets(path: &Path) -> Vec<Packet> {
let mut packets: Vec<Packet> = Vec::new();
for r in path.read_dir().unwrap() {
if let Ok(entry) = r {
if !entry.path().is_file() {
continue;
}
let name = entry.file_name().to_str().unwrap().to_string();
if !name.ends_with(".nupkg") {
continue;
}
packets.push(Packet::new(entry.path().as_path()))
}
}
async fn send_packet(path: &String, api_key: String) -> bool {
let form = multipart::Form::new().file("", path.clone()).await.unwrap();
let req_thread = tokio::spawn(async {
let client = Client::new();
packets
}
let response_process = client
.put("https://www.nuget.org/api/v2/package/")
.header("X-NuGet-ApiKey", api_key)
.header("X-NuGet-Client-Version", "4.1.0")
.multipart(form)
.send();
response_process.await
});
let mut ind = "---->---->---->".to_string();
fn check() {
println!("Checking for newer versions");
let config = read_config();
println!("Found {} packets", config.packets.len());
let mut updates = 0_u32;
for packet in config.packets {
let path = packet.path;
print!("{} ({}) Current: [{}]", packet.key, path, packet.version);
let versions = find_packets(Path::new(&path));
if let Some(newer) = versions.iter().find(|v| v.version > packet.version) {
println!(" -> [{}]", newer.version);
updates += 1;
} else {
println!(" Lastest")
}
}
print!("{} [{}] https://www.nuget.org/api/v2/package/{}", path, ind, "\x08".repeat(54));
while !req_thread.is_finished() {
print!("{}", ind);
stdout().flush().unwrap();
print!("\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08");
stdout().flush().unwrap();
conv(&mut ind);
tokio::time::sleep(Duration::from_millis(100)).await;
}
let response = req_thread.await.unwrap();
match response {
Ok(response) => {
let status = response.status();
println!("Response: [{}] {:?}", status, response.text().await);
if !status.is_success() {
return false;
}
true
}
Err(error) => {
println!("Error: {:?}", error.status().unwrap());
false
if updates > 0 {
println!("{} Updates found", updates);
}
if updates == 0 {
println!("No newer versions found.");
}
}