diff --git a/tofu/tofu.go b/tofu/tofu.go index 0fd7d26..7c7e04a 100644 --- a/tofu/tofu.go +++ b/tofu/tofu.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "os" + "sort" "strconv" "strings" "sync" @@ -45,6 +46,21 @@ func (k *KnownHosts) Lookup(hostname string) (Host, bool) { return c, ok } +// Hosts returns the known hosts sorted by hostname. +func (k *KnownHosts) Hosts() []Host { + keys := make([]string, 0, len(k.hosts)) + for key := range k.hosts { + keys = append(keys, key) + } + sort.Strings(keys) + + hosts := make([]Host, 0, len(k.hosts)) + for _, key := range keys { + hosts = append(hosts, k.hosts[key]) + } + return hosts +} + // WriteTo writes the list of known hosts to the provided io.Writer. func (k *KnownHosts) WriteTo(w io.Writer) (int64, error) { k.mu.RLock()