diff options
author | Cara Salter <cara@devcara.com> | 2022-06-09 15:03:13 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-06-09 15:03:13 -0400 |
commit | 0b82b7b47ac34cb9a14613a83ce6eb386ae21086 (patch) | |
tree | 5d41dea764838f004431c5d8bb755eaa28d2ee67 /src/main.rs | |
parent | 7f2bb3c132c9ef65aeb878cf71c14db12c7b801f (diff) | |
download | solarctl-0b82b7b47ac34cb9a14613a83ce6eb386ae21086.tar.gz solarctl-0b82b7b47ac34cb9a14613a83ce6eb386ae21086.zip |
state: Add shutdown/start/pause manipulation
issue #1
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 126 |
1 files changed, 125 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index e7a11a9..d55df8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,127 @@ +use clap::Parser; +mod errors; +use errors::CliError; +use tabular::{row, Table}; +use reqwest::{blocking::Client, StatusCode}; +use solarlib::planet::Planet; + +/// Manage solard and homeworld instances +#[derive(Parser)] +#[clap(author, version, about)] +struct Args { + /// The solard server to connect to + server: String, + /// The action to be taken + #[clap(subcommand)] + action: Action +} + +#[derive(clap::Subcommand)] +enum Action { + /// List planets on the server + List, + /// Shuts down a virtual machine + Stop { + /// The UUID of the machine to stop + uuid: String, + }, + Start { + /// The UUID of the machine to start + uuid: String, + }, + Pause { + /// The UUID of the machine to pause + uuid: String, + }, +} + fn main() { - println!("Hello, world!"); + color_eyre::install().unwrap(); + let args = Args::parse(); + let client = reqwest::blocking::Client::new(); + + let root = args.server.clone(); + + match args.action { + Action::List => { + list(args, client).unwrap(); + }, + Action::Stop { uuid } => { + stop(root, client, uuid).unwrap(); + }, + Action::Start { uuid } => { + start(root, client, uuid).unwrap(); + }, + Action::Pause { uuid } => { + pause(root, client, uuid).unwrap(); + } + }; +} + + +fn list(a: Args, c: Client) -> Result<(), CliError> { + let res: Vec<Planet> = c.get(format!("http://{}/planets/list", a.server)).send()?.json()?; + + let mut table = Table::new("{:<} {:<} {:<}"); + + table.add_row(row!( + "name", "uuid", "status") + ); + + for p in res { + table.add_row(row!( + &p.name, + &p.uuid, + if p.orbiting { "Running" } else { "Not running" }, + )); + } + + println!("{}", table); + + Ok(()) +} + +fn stop(server: String, c: Client, u: String) -> Result<(), CliError> { + let res = c.post(format!("http://{}/planets/{}/shutdown", server, u)).send()?; + + match res.status() { + StatusCode::OK => { + println!("Stopped."); + }, + _ => { + return Err(CliError::Cli(format!("Could not stop VM: {}", res.text()?))); + }, + }; + + Ok(()) +} + +fn start(server: String, c: Client, u: String) -> Result<(), CliError> { + let res = c.post(format!("http://{}/planets/{}/start", server, u)).send()?; + + match res.status() { + StatusCode::OK => { + println!("Started."); + }, + _ => { + return Err(CliError::Cli(format!("Could not start VM: {}", res.text()?))); + }, + }; + + Ok(()) +} + +fn pause(server: String, c: Client, u: String) -> Result<(), CliError> { + let res = c.post(format!("http://{}/planets/{}/pause", server, u)).send()?; + + match res.status() { + StatusCode::OK => { + println!("Paused."); + }, + _ => { + return Err(CliError::Cli(format!("Could not pause VM: {}", res.text()?))); + }, + }; + + Ok(()) } |