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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
mod functions;

use self::functions::*;
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;

#[derive(Debug, Parser, Clone)]
#[command(author, version, about, long_about=None)]
pub struct Commands {
    #[command(subcommand)]
    command: CommandType,
}

#[derive(Debug, Subcommand, Clone)]
enum CommandType {
    /// Create vm with a sized image
    Create {
        /// Name of VM
        name: String,
        /// Size in GB of VM image
        size: usize,
    },
    /// Delete existing vm
    Delete {
        /// Name of VM
        name: String,
    },
    /// Open standard input to a port on the VM
    NC {
        /// Name of VM
        name: String,
        /// Port of VM
        port: u16,
    },
    /// Uses ssh_port configuration variable to SSH into the host
    SSH {
        /// Name of VM
        name: String,
    },
    /// Configure supervision of an already existing VM
    Supervise {
        /// ISO of CD-ROM image -- will be embedded into supervision
        #[arg(short = 'c')]
        cdrom: Option<PathBuf>,
        /// Name of VM
        name: String,
    },
    /// Remove supervision of an already existing vm
    Unsupervise {
        /// Name of VM
        name: String,
    },
    /// Just run a pre-created VM; no systemd involved
    Run {
        /// Run without a video window
        #[arg(short = 'e', long, default_value = "false")]
        headless: bool,
        /// Do not wait for qemu to exit
        #[arg(short, long, default_value = "false")]
        detach: bool,
        /// ISO of CD-ROM image
        #[arg(short, long)]
        cdrom: Option<PathBuf>,
        /// Supply an extra ISO image (useful for windows installations)
        #[arg(long = "extra")]
        extra_disk: Option<PathBuf>,
        /// Name of VM
        name: String,
    },
    /// Gracefully shutdown a pre-created VM
    Shutdown {
        /// Name of VM
        name: String,
    },
    /// Issue QMP commands to the guest
    QMP {
        /// Name of VM
        name: String,
        /// Command to issue
        command: String,
        /// Arguments to send for command, JSON literal in single argument
        arguments: Option<String>,
    },
    /// Yield a list of VMs, one on each line
    List,
    /// Yield a list of supervised VMs, one on each line
    Supervised,
    /// Clone one VM to another
    Clone {
        /// VM to clone from
        from: String,
        /// VM to clone to
        to: String,
    },
    /// Import a VM from a VM image file
    Import {
        /// Format of incoming image
        #[arg(short, long, required = true)]
        format: String,
        /// VM to import to
        name: String,
        /// VM image to import from
        from_file: PathBuf,
    },
    /// Show and manipulate VM configuration
    #[command(subcommand)]
    Config(ConfigSubcommand),
}

#[derive(Debug, Subcommand, Clone)]
enum ConfigSubcommand {
    /// Show the written+inferred configuration for a VM
    Show {
        /// Name of VM
        name: String,
    },
    /// Set a value int the configuration; type-safe
    Set {
        /// Name of VM
        name: String,
        /// Name of key to set
        key: String,
        /// Value of key to set
        value: String,
    },
    /// Adjust port mappings
    #[command(subcommand)]
    Port(ConfigPortSubcommand),
}

#[derive(Debug, Subcommand, Clone)]
enum ConfigPortSubcommand {
    /// Add a port mapping from localhost:<HOSTPORT> -> <GUEST IP>:<GUESTPORT>
    Map {
        /// Name of VM
        name: String,
        /// Port on localhost to map to guest remote IP
        hostport: u16,
        /// Port on guest to expose
        guestport: u16,
    },
    /// Undo a port mapping
    Unmap {
        /// Name of VM
        name: String,
        /// Port on localhost mapped to guest
        hostport: u16,
    },
}

impl Commands {
    pub async fn evaluate() -> Result<()> {
        let args = Self::parse();

        match args.command {
            CommandType::Config(sub) => match sub {
                ConfigSubcommand::Set { name, key, value } => config_set(&name, &key, &value),
                ConfigSubcommand::Show { name } => show_config(&name),
                ConfigSubcommand::Port(sub) => match sub {
                    ConfigPortSubcommand::Map {
                        name,
                        hostport,
                        guestport,
                    } => port_map(&name, hostport, guestport),
                    ConfigPortSubcommand::Unmap { name, hostport } => port_unmap(&name, hostport),
                },
            },
            CommandType::NC { name, port } => nc(&name, port).await,
            CommandType::SSH { name } => ssh(&name),
            CommandType::Create { name, size } => create(&name, size),
            CommandType::Delete { name } => delete(&name),
            CommandType::Supervise { cdrom, name } => supervise(&name, cdrom),
            CommandType::Unsupervise { name } => unsupervise(&name),
            CommandType::Run {
                headless,
                detach,
                cdrom,
                extra_disk,
                name,
            } => run(&name, cdrom, extra_disk, detach, headless),
            CommandType::List => list(),
            CommandType::Shutdown { name } => shutdown(&name),
            CommandType::QMP {
                name,
                command,
                arguments,
            } => qmp(&name, &command, arguments.as_deref()),
            CommandType::Supervised => supervised(),
            CommandType::Clone { from, to } => clone(&from, &to),
            CommandType::Import {
                format,
                name,
                from_file,
            } => import(&name, from_file, &format),
        }
    }
}