| 1 | // Copyright 2026 Deno Land Inc. Apache-2.0 license. |
| 2 | |
| 3 | // Operator commands read the process environment for their fleet settings. |
| 4 | #![allow(clippy::disallowed_methods)] |
| 5 | |
| 6 | //! The flags every operator command shares. |
| 7 | //! |
| 8 | //! `--bucket`, `--endpoint` and `--region` name the same fleet whichever |
| 9 | //! command reads them, and four commands parsed them separately before this |
| 10 | //! module existed. That is how `--bucket gs://name` came to be handled in |
| 11 | //! one place and not another: a rule spread across four loops is a rule |
| 12 | //! four authors have to remember. |
| 13 | //! |
| 14 | //! The environment fallbacks live here too, so `CELLD_BUCKET`, |
| 15 | //! `S3_ENDPOINT`, `AWS_REGION` and `AWS_DEFAULT_REGION` mean one thing |
| 16 | //! across the CLI. |
| 17 | |
| 18 | use anyhow::Context; |
| 19 | |
| 20 | /// A non-empty environment variable, or nothing. |
| 21 | /// |
| 22 | /// An empty variable is how a shell spells "unset" in a script that always |
| 23 | /// exports, so it must not beat a flag or a default. |
| 24 | fn env(name: &str) -> Option<String> { |
| 25 | std::env::var(name) |
| 26 | .ok() |
| 27 | .filter(|value| !value.trim().is_empty()) |
| 28 | } |
| 29 | |
| 30 | pub(crate) fn bucket_from_environment() -> Option<String> { |
| 31 | env("CELLD_BUCKET").map(|value| value.trim().to_string()) |
| 32 | } |
| 33 | |
| 34 | /// Which fleet a command acts on. |
| 35 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 36 | pub struct FleetFlags { |
| 37 | pub bucket: Option<String>, |
| 38 | pub endpoint: Option<String>, |
| 39 | pub region: Option<String>, |
| 40 | } |
| 41 | |
| 42 | /// A fleet's resolved storage location: everything [`crate::fleet`] needs to |
| 43 | /// open a bucket. |
| 44 | /// |
| 45 | /// Returned as one value because a bucket without its region opens against |
| 46 | /// the wrong endpoint, and separate accessors let a caller take one and |
| 47 | /// forget the other. |
| 48 | pub struct Storage { |
| 49 | pub bucket: String, |
| 50 | pub endpoint: Option<String>, |
| 51 | pub region: String, |
| 52 | } |
| 53 | |
| 54 | impl FleetFlags { |
| 55 | /// Consume one fleet flag, or report that it is not one. |
| 56 | pub fn consume( |
| 57 | &mut self, |
| 58 | argument: &str, |
| 59 | value: &mut impl FnMut(&str) -> anyhow::Result<String>, |
| 60 | ) -> anyhow::Result<bool> { |
| 61 | match argument { |
| 62 | // `Bucket::open` is the one parser for the complete spec. Keeping |
| 63 | // the spelling intact here prevents CLI paths from disagreeing |
| 64 | // about scheme case or changing where the prefix begins. |
| 65 | "--bucket" => self.bucket = Some(value("--bucket")?), |
| 66 | "--endpoint" => self.endpoint = Some(value("--endpoint")?), |
| 67 | "--region" => self.region = Some(value("--region")?), |
| 68 | _ => return Ok(false), |
| 69 | } |
| 70 | Ok(true) |
| 71 | } |
| 72 | |
| 73 | /// Fill anything the flags left unset from the environment. |
| 74 | pub fn with_environment(mut self) -> Self { |
| 75 | if self.bucket.is_none() { |
| 76 | self.bucket = bucket_from_environment(); |
| 77 | } |
| 78 | if self.endpoint.is_none() { |
| 79 | self.endpoint = env("S3_ENDPOINT"); |
| 80 | } |
| 81 | if self.region.is_none() { |
| 82 | self.region = env("AWS_REGION").or_else(|| env("AWS_DEFAULT_REGION")); |
| 83 | } |
| 84 | self |
| 85 | } |
| 86 | |
| 87 | /// The region a bucket opens against, whether or not one was named. |
| 88 | fn region(&self) -> String { |
| 89 | self.region |
| 90 | .clone() |
| 91 | .unwrap_or_else(|| "us-east-1".to_string()) |
| 92 | } |
| 93 | |
| 94 | /// Resolve to a complete storage location, or explain what is missing. |
| 95 | /// |
| 96 | /// `command` names the caller so the message says which invocation to |
| 97 | /// fix, rather than making an operator guess which of several commands |
| 98 | /// in a script wanted a bucket. |
| 99 | pub fn resolve(self, command: &str) -> anyhow::Result<Storage> { |
| 100 | let resolved = self.with_environment(); |
| 101 | let region = resolved.region(); |
| 102 | let bucket = resolved |
| 103 | .bucket |
| 104 | .filter(|bucket| !bucket.is_empty()) |
| 105 | .with_context(|| { |
| 106 | format!( |
| 107 | "{command} requires --bucket s3://NAME, gs://NAME or az://CONTAINER \ |
| 108 | (or CELLD_BUCKET)" |
| 109 | ) |
| 110 | })?; |
| 111 | Ok(Storage { |
| 112 | bucket, |
| 113 | endpoint: resolved.endpoint, |
| 114 | region, |
| 115 | }) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl Storage { |
| 120 | /// Open the fleet bucket and prove it is reachable. |
| 121 | pub async fn open(&self) -> anyhow::Result<crate::bucket::Bucket> { |
| 122 | let store = |
| 123 | crate::fleet::bucket_client(&self.bucket, self.endpoint.as_deref(), &self.region)?; |
| 124 | crate::fleet::validate_bucket(&store).await?; |
| 125 | Ok(store) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// The shared fleet-flag help, so every command documents them identically. |
| 130 | pub const FLEET_HELP: &str = " --bucket NAME Fleet bucket (or CELLD_BUCKET) |
| 131 | --endpoint URL Optional S3-compatible endpoint (or S3_ENDPOINT) |
| 132 | --region REGION Storage region (default: AWS_REGION or us-east-1)"; |
| 133 | |
| 134 | /// The shared listing help, for a command that bounds its output. |
| 135 | pub const LISTING_HELP: &str = " --limit N Maximum rows to print (default: 1000) |
| 136 | --after CURSOR Resume after this row |
| 137 | --all Print every row; this reads the whole listing |
| 138 | --json Print one JSON object per line instead of text"; |