| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use super::json; |
| 6 | | use super::open; |
| 7 | | use super::ui; |
| 8 | | use anyhow::Result; |
| 9 | | use cylinder::{Kind, Spec}; |
| 10 | | use std::path::Path; |
| 11 | |
|
| 12 | |
|
| 13 | 2× | fn pairs(list: &[String], what: &str) -> Result<std::collections::BTreeMap<String, String>> { |
| 14 | 2× | list.iter() |
| 15 | 2× | .map(|p| { |
| 16 | 1× | p.split_once('=') |
| 17 | 1× | .map(|(k, v)| (k.to_string(), v.to_string())) |
| 18 | 1× | .ok_or_else(|| anyhow::anyhow!("{what} {p:?}: write it KEY=VALUE")) |
| 19 | 1× | }) |
| 20 | 2× | .collect() |
| 21 | 2× | } |
| 22 | |
|
| 23 | 0× | fn summary(k: &cylinder::Kept) -> String { |
| 24 | 0× | let a = &k.artifact; |
| 25 | 0× | let mut parts = vec![a.kind.as_str().to_string()]; |
| 26 | 0× | parts.extend(a.os.clone()); |
| 27 | 0× | parts.extend(a.arch.clone()); |
| 28 | 0× | format!( |
| 29 | | "{} {} {}", |
| 30 | 0× | ui::hash(&k.name[..12]), |
| 31 | 0× | parts.join(" "), |
| 32 | 0× | ui::dim(format!("{} bytes", a.total_size)) |
| 33 | | ) |
| 34 | 0× | } |
| 35 | |
|
| 36 | 2× | fn as_json(k: &cylinder::Kept, files: usize) -> String { |
| 37 | 2× | let a = &k.artifact; |
| 38 | 4× | let text = |v: &Option<String>| v.as_deref().map(json::s).unwrap_or_else(|| "null".into()); |
| 39 | 4× | let map = |m: &std::collections::BTreeMap<String, String>| { |
| 40 | 4× | let inner: Vec<String> = m |
| 41 | 4× | .iter() |
| 42 | 4× | .map(|(k, v)| format!("{}:{}", json::s(k), json::s(v))) |
| 43 | 4× | .collect(); |
| 44 | 4× | format!("{{{}}}", inner.join(",")) |
| 45 | 4× | }; |
| 46 | 2× | json::obj([ |
| 47 | 2× | ("name", json::s(&k.name)), |
| 48 | 2× | ("kind", json::s(a.kind.as_str())), |
| 49 | 2× | ("arch", text(&a.arch)), |
| 50 | 2× | ("os", text(&a.os)), |
| 51 | 2× | ("tree", json::s(&a.tree.to_hex())), |
| 52 | 2× | ("files", json::n(files as u64)), |
| 53 | 2× | ("total_size", json::n(a.total_size)), |
| 54 | 2× | ("env", map(&a.env)), |
| 55 | 2× | ("labels", map(&a.labels)), |
| 56 | 2× | ]) |
| 57 | 2× | } |
| 58 | |
|
| 59 | | #[allow(clippy::too_many_arguments)] |
| 60 | 2× | pub fn pack( |
| 61 | 2× | dir: &Path, |
| 62 | 2× | kind: &str, |
| 63 | 2× | arch: Option<String>, |
| 64 | 2× | os: Option<String>, |
| 65 | 2× | entrypoint: Vec<String>, |
| 66 | 2× | env: &[String], |
| 67 | 2× | labels: &[String], |
| 68 | 2× | as_json_out: bool, |
| 69 | 2× | ) -> Result<i32> { |
| 70 | 2× | let repo = open()?; |
| 71 | 2× | let mut spec = Spec::new(Kind::parse(kind)?); |
| 72 | 1× | spec.arch = arch; |
| 73 | 1× | spec.os = os; |
| 74 | 1× | spec.entrypoint = (!entrypoint.is_empty()).then_some(entrypoint); |
| 75 | 1× | spec.env = pairs(env, "env")?; |
| 76 | 1× | spec.labels = pairs(labels, "label")?; |
| 77 | 1× | let sp = ui::spinner("Packing…"); |
| 78 | 1× | let k = cylinder::pack(&repo, dir, &spec)?; |
| 79 | 1× | sp.clear(); |
| 80 | 1× | let files = cylinder::tree(&repo, &k.artifact)?.entries.len(); |
| 81 | 1× | if as_json_out { |
| 82 | 1× | println!("{}", as_json(&k, files)); |
| 83 | 1× | } else { |
| 84 | 0× | println!( |
| 85 | 0× | "{} {} as {}", |
| 86 | 0× | ui::done("Packed"), |
| 87 | 0× | ui::path(dir.display()), |
| 88 | 0× | summary(&k) |
| 89 | 0× | ); |
| 90 | 0× | println!("{}", ui::kv("name", &k.name)); |
| 91 | 0× | println!("{}", ui::kv("files", files.to_string())); |
| 92 | 0× | } |
| 93 | 1× | Ok(0) |
| 94 | 2× | } |
| 95 | |
|
| 96 | 1× | pub fn list(as_json_out: bool) -> Result<i32> { |
| 97 | 1× | let repo = open()?; |
| 98 | 1× | let names = cylinder::list(&repo)?; |
| 99 | 1× | let kept: Vec<cylinder::Kept> = names |
| 100 | 1× | .iter() |
| 101 | 1× | .map(|n| cylinder::open(&repo, n)) |
| 102 | 1× | .collect::<std::result::Result<_, _>>()?; |
| 103 | 1× | if as_json_out { |
| 104 | 1× | let items: Vec<String> = kept |
| 105 | 1× | .iter() |
| 106 | 1× | .map(|k| { |
| 107 | 1× | let files = cylinder::tree(&repo, &k.artifact) |
| 108 | 1× | .map(|t| t.entries.len()) |
| 109 | 1× | .unwrap_or(0); |
| 110 | 1× | as_json(k, files) |
| 111 | 1× | }) |
| 112 | 1× | .collect(); |
| 113 | 1× | println!("[{}]", items.join(",")); |
| 114 | 1× | return Ok(0); |
| 115 | 0× | } |
| 116 | 0× | if kept.is_empty() { |
| 117 | 0× | println!("No artifacts yet: oxy artifact pack DIR --kind archive"); |
| 118 | 0× | } |
| 119 | 0× | for k in &kept { |
| 120 | 0× | println!("{}", summary(k)); |
| 121 | 0× | } |
| 122 | 0× | Ok(0) |
| 123 | 1× | } |
| 124 | |
|
| 125 | 0× | pub fn show(name: &str, as_json_out: bool) -> Result<i32> { |
| 126 | 0× | let repo = open()?; |
| 127 | 0× | let k = cylinder::open(&repo, name)?; |
| 128 | 0× | let t = cylinder::tree(&repo, &k.artifact)?; |
| 129 | 0× | if as_json_out { |
| 130 | 0× | println!("{}", as_json(&k, t.entries.len())); |
| 131 | 0× | return Ok(0); |
| 132 | 0× | } |
| 133 | 0× | println!("{}", summary(&k)); |
| 134 | 0× | println!("{}", ui::kv("name", &k.name)); |
| 135 | 0× | if let Some(e) = &k.artifact.entrypoint { |
| 136 | 0× | println!("{}", ui::kv("entrypoint", e.join(" "))); |
| 137 | 0× | } |
| 138 | 0× | for (key, v) in &k.artifact.labels { |
| 139 | 0× | println!("{}", ui::kv("label", format!("{key}={v}"))); |
| 140 | 0× | } |
| 141 | 0× | for (key, v) in &k.artifact.env { |
| 142 | 0× | println!("{}", ui::kv("env", format!("{key}={v}"))); |
| 143 | 0× | } |
| 144 | 0× | for e in &t.entries { |
| 145 | 0× | let mode = match e.mode { |
| 146 | 0× | cylinder::MODE_EXEC => "x", |
| 147 | 0× | cylinder::MODE_LINK => "l", |
| 148 | 0× | _ => " ", |
| 149 | | }; |
| 150 | 0× | println!(" {mode} {:>10} {}", e.size, e.path); |
| 151 | | } |
| 152 | 0× | Ok(0) |
| 153 | 0× | } |
| 154 | |
|
| 155 | 1× | pub fn unpack(name: &str, dest: &Path) -> Result<i32> { |
| 156 | 1× | let repo = open()?; |
| 157 | 1× | let k = cylinder::open(&repo, name)?; |
| 158 | 1× | let n = cylinder::unpack(&repo, &k, dest)?; |
| 159 | 1× | println!( |
| 160 | | "{} {} files of {} into {}", |
| 161 | 1× | ui::done("Unpacked"), |
| 162 | | n, |
| 163 | 1× | ui::hash(&k.name[..12]), |
| 164 | 1× | ui::path(dest.display()) |
| 165 | | ); |
| 166 | 1× | Ok(0) |
| 167 | 1× | } |
| 168 | |
|
| 169 | 1× | pub fn verify(name: &str) -> Result<i32> { |
| 170 | 1× | let repo = open()?; |
| 171 | 1× | let k = cylinder::open(&repo, name)?; |
| 172 | 1× | let n = cylinder::verify(&repo, &k)?; |
| 173 | 1× | println!( |
| 174 | | "{} {}: its name, tree and {} files check out", |
| 175 | 1× | ui::done("Verified"), |
| 176 | 1× | ui::hash(&k.name[..12]), |
| 177 | | n |
| 178 | | ); |
| 179 | 1× | Ok(0) |
| 180 | 1× | } |