A lightweight CLI tool that connects to a remote server over SSH and executes PM2 process manager commands.

Merge pull request #2 from tsirysndr/env-options

feat: add support for environment variable options

authored by tsiry-sandratraina.com and committed by

GitHub ad04543b d1954a38

+38 -9
.github/assets/preview.png

This is a binary file and will not be displayed.

+1 -1
Cargo.lock
··· 343 343 344 344 [[package]] 345 345 name = "pm22" 346 - version = "0.2.0" 346 + version = "0.3.0" 347 347 dependencies = [ 348 348 "anyhow", 349 349 "base64",
+1 -1
Cargo.toml
··· 1 1 [package] 2 2 name = "pm22" 3 - version = "0.2.0" 3 + version = "0.3.0" 4 4 edition = "2024" 5 5 description = "A lightweight CLI tool that connects to a remote server over SSH and executes PM2 process manager commands." 6 6 license = "MIT"
+36 -7
src/main.rs
··· 1 1 use crate::pm2::run_pm2_command; 2 2 use clap::{Arg, Command as ClapCommand}; 3 + use owo_colors::OwoColorize; 4 + use std::env; 3 5 4 6 pub mod pm2; 5 7 6 8 fn main() -> Result<(), Box<dyn std::error::Error>> { 9 + let banner = format!( 10 + "{}\nConnects to a remote server via SSH and executes PM2 commands", 11 + r#" 12 + ██████ ███ ███ ██████ ██████ 13 + ██ ██ ████ ████ ██ ██ 14 + ██████ ██ ████ ██ █████ █████ 15 + ██ ██ ██ ██ ██ ██ 16 + ██ ██ ██ ███████ ███████ 17 + "# 18 + .cyan() 19 + ); 7 20 let matches = ClapCommand::new("pm22") 8 - .about("Connects to a remote server via SSH and executes PM2 commands") 9 - .version("0.1.0") 21 + .about(&banner) 22 + .version(env!("CARGO_PKG_VERSION")) 10 23 .author("Tsiry Sandratraina <tsiry.sndr@rocksky.app>") 11 24 .arg( 12 25 Arg::new("host") 13 - .required(true) 26 + .short('h') 27 + .long("host") 28 + .default_value("PM22_HOST") 14 29 .help("Host to connect to, with username (e.g., user@host)"), 15 30 ) 16 31 .arg( ··· 51 66 let verbose = matches.get_flag("verbose"); 52 67 init_logger(verbose); 53 68 54 - let host = matches.get_one::<String>("host").unwrap(); 55 - let port = matches.get_one::<String>("port").unwrap(); 56 - let key = matches.get_one::<String>("key").unwrap(); 69 + let host = env::var("PM22_HOST") 70 + .unwrap_or_else(|_| matches.get_one::<String>("host").unwrap().to_string()); 71 + let host = match host.as_str() { 72 + "PM22_HOST" => env::var("PM22_HOST")?, 73 + _ => host, 74 + }; 75 + let port = env::var("PM22_PORT") 76 + .unwrap_or_else(|_| matches.get_one::<String>("port").unwrap().to_string()); 77 + matches.get_one::<String>("port").unwrap(); 78 + let key = env::var("PM22_KEY") 79 + .unwrap_or_else(|_| matches.get_one::<String>("key").unwrap().to_string()); 57 80 let cmd = matches.get_one::<String>("cmd").unwrap(); 58 81 let args: Vec<&String> = matches 59 82 .get_many::<String>("args") 60 83 .map(|vals| vals.collect()) 61 84 .unwrap_or_default(); 62 85 63 - run_pm2_command(host, port.parse().unwrap_or(22), key, cmd, args)?; 86 + run_pm2_command( 87 + host.trim(), 88 + port.parse().unwrap_or(22), 89 + key.trim(), 90 + cmd, 91 + args, 92 + )?; 64 93 Ok(()) 65 94 } 66 95