Browse code

daemon/command: slight cleanup of normalizeHosts

Use the slices package to remove duplicates.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2026/01/23 23:19:01
Showing 1 changed files
... ...
@@ -10,7 +10,7 @@ import (
10 10
 	"os"
11 11
 	"path/filepath"
12 12
 	"runtime"
13
-	"sort"
13
+	"slices"
14 14
 	"strings"
15 15
 	"sync"
16 16
 	"time"
... ...
@@ -718,38 +718,34 @@ func loadDaemonCliConfig(opts *daemonOptions) (*config.Config, error) {
718 718
 	return conf, nil
719 719
 }
720 720
 
721
-// normalizeHosts normalizes the configured config.Hosts and remove duplicates.
721
+// normalizeHosts normalizes the configured config.Hosts and removes duplicates.
722 722
 // It returns an error if it fails to parse a host.
723 723
 func normalizeHosts(cfg *config.Config) error {
724
-	if len(cfg.Hosts) == 0 {
724
+	hosts := slices.Clone(cfg.Hosts)
725
+	if len(hosts) == 0 {
725 726
 		// if no hosts are configured, create a single entry slice, so that the
726 727
 		// default is used.
727 728
 		//
728 729
 		// TODO(thaJeztah) implement a cleaner way for this; this depends on a
729 730
 		//                 side-effect of how we parse empty/partial hosts.
730
-		cfg.Hosts = make([]string, 1)
731
+		hosts = make([]string, 1)
731 732
 	}
732
-	hosts := make([]string, 0, len(cfg.Hosts))
733
-	seen := make(map[string]struct{}, len(cfg.Hosts))
734 733
 
735 734
 	useTLS := DefaultTLSValue
736 735
 	if cfg.TLS != nil {
737 736
 		useTLS = *cfg.TLS
738 737
 	}
739 738
 
740
-	for _, h := range cfg.Hosts {
741
-		host, err := dopts.ParseHost(useTLS, honorXDG, h)
739
+	for i, h := range hosts {
740
+		var err error
741
+		hosts[i], err = dopts.ParseHost(useTLS, honorXDG, h)
742 742
 		if err != nil {
743 743
 			return err
744 744
 		}
745
-		if _, ok := seen[host]; ok {
746
-			continue
747
-		}
748
-		seen[host] = struct{}{}
749
-		hosts = append(hosts, host)
750 745
 	}
751
-	sort.Strings(hosts)
752
-	cfg.Hosts = hosts
746
+
747
+	slices.Sort(hosts)
748
+	cfg.Hosts = slices.Compact(hosts)
753 749
 	return nil
754 750
 }
755 751