Add KnownHosts.Hosts function

This commit is contained in:
Adnan Maolood 2021-01-14 18:50:03 -05:00
parent 1a3974b3a3
commit e701ceff71

View File

@ -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()