1use serde::{Deserialize, Serialize};
2use std::collections::BTreeMap;
3use std::io;
4
5use super::{lxc_output, Location};
6
7#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
8pub struct Image {
10 pub auto_update: bool,
11 pub properties: BTreeMap<String, String>,
12 pub public: bool,
13 pub aliases: Vec<BTreeMap<String, String>>,
14 pub architecture: String,
15 pub cached: bool,
16 pub filename: String,
17 pub fingerprint: String,
18 pub size: u64,
19 #[serde(default = "BTreeMap::new")]
20 pub update_source: BTreeMap<String, String>,
21 pub created_at: String,
22 pub expires_at: String,
23 pub last_used_at: String,
24 pub uploaded_at: String,
25}
26
27impl Image {
28 pub fn all(location: Location) -> io::Result<Vec<Self>> {
50 let json = match location {
51 Location::Local => lxc_output(&["image", "list", "--format", "json"])?,
52 Location::Remote(remote) => lxc_output(&["image", "list", &format!("{}:", remote), "--format", "json"])?
53 };
54
55 serde_json::from_slice::<Vec<Self>>(&json).map_err(|err| {
56 io::Error::new(
57 io::ErrorKind::Other,
58 format!("LXD image: failed to parse json: {}", err)
59 )
60 })
61 }
62
63 pub fn new(location: Location, name: &str) -> io::Result<Self> {
79 let json = match location {
80 Location::Local => lxc_output(&["image", "list", name, "--format", "json"])?,
81 Location::Remote(remote) => lxc_output(&["image", "list", &format!("{}:", remote), name, "--format", "json"])?
82 };
83
84 match serde_json::from_slice::<Vec<Self>>(&json) {
85 Ok(mut list) => if list.len() == 1 {
86 Ok(list.remove(0))
87 } else {
88 Err(io::Error::new(
89 io::ErrorKind::NotFound,
90 format!("LXD image: {} not found", name)
91 ))
92 },
93 Err(err) => {
94 Err(io::Error::new(
95 io::ErrorKind::Other,
96 format!("LXD image: failed to parse json: {}", err)
97 ))
98 }
99 }
100 }
101}