This commit is contained in:
Leonard Steppy 2024-12-11 10:42:44 +01:00
commit fa7caf87e2
4 changed files with 112 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "plugin_uploader"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = { version = "4.5.23", features = ["derive"] }

1
rustfmt.toml Normal file
View File

@ -0,0 +1 @@
tab_spaces = 2

103
src/main.rs Normal file
View File

@ -0,0 +1,103 @@
use clap::Parser;
use std::env;
use std::path::PathBuf;
const SERVERS_ENV_VAR: &str = "PLUGIN_UPLOADER_SERVERS";
/// Uploads a plugin to one or multiple configured servers.
///
/// By default, older versions of the plugin will be deleted.
///
/// --- Server configuration ---
///
/// Servers can be configured via environment variable `PLUGIN_UPLOADER_SERVERS`.
/// The value should be a string which contains the ssh name and the server directory path.
///
/// --- Example ---
///
/// export PLUGIN_UPLOADER_SERVERS="crea:server/creative2,city:city2,lobby:"
#[derive(Parser, Debug)]
#[command(version, about, long_about)]
struct Args {
/// The plugin file to upload
plugin: PathBuf,
/// The ssh names of the servers to upload to
#[arg(num_args = 1..)]
servers: Vec<String>,
/// Whether not to delete, but to rename the previous plugin
#[arg(short, long, value_name = "archive", default_value = "false", default_missing_values = ["true"])]
archive_old_versions: bool,
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
struct Server {
pub ssh_name: String,
pub server_directory_path: PathBuf,
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
struct PluginInfo {
pub name: String,
pub version: Option<String>,
}
fn main() -> Result<(), String> {
let args = Args::parse();
dbg!(&args);
let configured_servers = parse_server_configuration_from_env()?;
dbg!(&configured_servers);
let servers = args
.servers
.iter()
.map(|server_name| {
configured_servers
.iter()
.find(|server| server.ssh_name == *server_name)
.ok_or(format!("no server with the name '{server_name}' has been configured"))
})
.collect::<Result<Vec<_>, _>>()?;
if !args.plugin.is_file() {
return Err("plugin is not a file".to_string())
}
//args.plugin.canonicalize()
Ok(())
}
fn parse_server_configuration_from_env() -> Result<Vec<Server>, String> {
env::var(SERVERS_ENV_VAR)
.map_err(|_| format!("Missing environment variable {}", SERVERS_ENV_VAR))
.and_then(|value| parse_server_configuration(&value))
}
fn parse_server_configuration(config_str: &str) -> Result<Vec<Server>, String> {
config_str.split(',').map(|server_entry| {
let (name, server_directory) = server_entry.split_once(':').ok_or(format!("Server entry doesn't specify a server directory (separate name and directory via double colon) for entry '{}'", server_entry))?;
Ok(Server {
ssh_name: name.to_string(),
server_directory_path: PathBuf::from(server_directory),
})
}).collect()
}
#[cfg(test)]
mod test {
use crate::{parse_server_configuration, Server};
use std::path::PathBuf;
#[test]
fn test_parse_server_configuration() {
let servers = parse_server_configuration("foo:bar,fizz:buzz/bizz").expect("valid server configuration");
assert_eq!(vec![Server {
ssh_name: "foo".to_string(),
server_directory_path: PathBuf::from("bar"),
}, Server {
ssh_name: "fizz".to_string(),
server_directory_path: PathBuf::from("buzz/bizz"),
}], servers);
}
}