summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: d55df8e0edcde14edbc78997a4e9828d2d5c3cd2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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() {
    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(())
}