]> git.proxmox.com Git - pve-installer.git/commitdiff
tui: allow one-part FQDNs/domains
authorChristoph Heiss <c.heiss@proxmox.com>
Wed, 21 Jun 2023 09:38:00 +0000 (11:38 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 21 Jun 2023 10:20:41 +0000 (12:20 +0200)
Otherwise, the user would be blocked from continuing the the installer,
instead just being presented with an error.

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
proxmox-tui-installer/src/setup.rs
proxmox-tui-installer/src/utils.rs

index 2ebf71c0cbe7ab83fd637a7fe36fd1885a28a5e7..7f70b57cc646532f127726a3c92c2baf2eddb84b 100644 (file)
@@ -171,8 +171,8 @@ impl From<InstallerOptions> for InstallConfig {
 
             mngmt_nic: options.network.ifname,
 
-            hostname: options.network.fqdn.host().to_owned(),
-            domain: options.network.fqdn.domain().to_owned(),
+            hostname: options.network.fqdn.host().unwrap_or("pve").to_owned(),
+            domain: options.network.fqdn.domain(),
             cidr: options.network.address,
             gateway: options.network.gateway,
             dns: options.network.dns_server,
index 8a6b22bbbaced45b6309697414144d969df14278..c4b7c030df7d8f10c56613e6923bccc78ef58574 100644 (file)
@@ -112,33 +112,42 @@ fn mask_limit(addr: &IpAddr) -> usize {
 
 #[derive(Clone, Debug)]
 pub struct Fqdn {
-    host: String,
-    domain: String,
+    parts: Vec<String>,
 }
 
 impl Fqdn {
     pub fn from(fqdn: &str) -> Result<Self, ()> {
-        let (host, domain) = fqdn.split_once('.').ok_or(())?;
+        let parts = fqdn
+            .split('.')
+            .map(ToOwned::to_owned)
+            .collect::<Vec<String>>();
 
-        if !Self::validate_single(host) || !domain.split('.').all(Self::validate_single) {
+        if !parts.iter().all(&Self::validate_single) {
             Err(())
         } else {
-            Ok(Self {
-                host: host.to_owned(),
-                domain: domain.to_owned(),
-            })
+            Ok(Self { parts })
         }
     }
 
-    pub fn host(&self) -> &str {
-        &self.host
+    pub fn host(&self) -> Option<&str> {
+        self.has_host().then_some(&self.parts[0])
     }
 
-    pub fn domain(&self) -> &str {
-        &self.domain
+    pub fn domain(&self) -> String {
+        let parts = if self.has_host() {
+            &self.parts[1..]
+        } else {
+            &self.parts
+        };
+
+        parts.join(".")
+    }
+
+    fn has_host(&self) -> bool {
+        self.parts.len() > 1
     }
 
-    fn validate_single(s: &str) -> bool {
+    fn validate_single(s: &String) -> bool {
         !s.is_empty()
             && s.chars()
                 .next()
@@ -165,7 +174,7 @@ impl FromStr for Fqdn {
 
 impl fmt::Display for Fqdn {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "{}.{}", self.host, self.domain)
+        write!(f, "{}", self.parts.join("."))
     }
 }
 
@@ -201,7 +210,7 @@ mod tests {
     #[test]
     fn fqdn_parts() {
         let fqdn = Fqdn::from("pve.example.com").unwrap();
-        assert_eq!(fqdn.host(), "pve");
+        assert_eq!(fqdn.host().unwrap(), "pve");
         assert_eq!(fqdn.domain(), "example.com");
     }